blob: d60e95ef200ff58d6f3bb081c47ebeac69aa78b2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010019#if (defined(UNIX) || defined(VMS)) && defined(FEAT_EVAL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020020# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010023#if defined(VMS) && defined(HAVE_XOS_R_H)
24# include <x11/xos_r.h>
25#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar217e1b82019-12-01 21:41:28 +010027// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000028#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020030#if defined(__hpux) && !defined(HAVE_DIRFD)
31# define dirfd(x) ((x)->__dd_fd)
32# define HAVE_DIRFD
33#endif
34
Bram Moolenaarf077db22019-08-13 00:18:24 +020035static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010036#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020040static 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 +000041#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000044
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020045#ifdef FEAT_EVAL
46static int readdirex_sort;
47#endif
48
Bram Moolenaar473952e2019-09-28 16:30:04 +020049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010050filemess(
51 buf_T *buf,
52 char_u *name,
53 char_u *s,
54 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055{
56 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020057 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59 if (msg_silent != 0)
60 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010061 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020062
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020064 if (STRLEN(IObuff) > IOSIZE - 100)
65 IObuff[IOSIZE - 100] = NUL;
66
67 // Avoid an over-long translation to cause trouble.
68 STRNCAT(IObuff, s, 99);
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 /*
71 * For the first message may have to start a new line.
72 * For further ones overwrite the previous one, reset msg_scroll before
73 * calling filemess().
74 */
75 msg_scroll_save = msg_scroll;
76 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
77 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 check_for_delay(FALSE);
80 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020081 if (prev_msg_col != 0 && msg_col == 0)
82 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 msg_scroll = msg_scroll_save;
84 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010085 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
87 msg_clr_eos();
88 out_flush();
89 msg_scrolled_ign = FALSE;
90}
91
92/*
93 * Read lines from file "fname" into the buffer after line "from".
94 *
95 * 1. We allocate blocks with lalloc, as big as possible.
96 * 2. Each block is filled with characters from the file with a single read().
97 * 3. The lines are inserted in the buffer with ml_append().
98 *
99 * (caller must check that fname != NULL, unless READ_STDIN is used)
100 *
101 * "lines_to_skip" is the number of lines that must be skipped
102 * "lines_to_read" is the number of lines that are appended
103 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
104 *
105 * flags:
106 * READ_NEW starting to edit a new buffer
107 * READ_FILTER reading filter output
108 * READ_STDIN read from stdin instead of a file
109 * READ_BUFFER read from curbuf instead of a file (converting after reading
110 * stdin)
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100111 * READ_NOFILE do not read a file, only trigger BufReadCmd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200113 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 */
118 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100119readfile(
120 char_u *fname,
121 char_u *sfname,
122 linenr_T from,
123 linenr_T lines_to_skip,
124 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100125 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100126 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100128 int retval = FAIL; // jump to "theend" instead of returning
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 int fd = 0;
130 int newfile = (flags & READ_NEW);
131 int check_readonly;
132 int filtering = (flags & READ_FILTER);
133 int read_stdin = (flags & READ_STDIN);
134 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000136 int set_options = newfile || read_buffer
137 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100138 linenr_T read_buf_lnum = 1; // next line to read from curbuf
139 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 char_u c;
141 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100142 char_u *ptr = NULL; // pointer into read buffer
143 char_u *buffer = NULL; // read buffer
144 char_u *new_buffer = NULL; // init to shut up gcc
145 char_u *line_start = NULL; // init to shut up gcc
146 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 colnr_T len;
148 long size = 0;
149 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200150 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 int skip_read = FALSE;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200152 off_T filesize_disk = 0; // file size read from disk
153 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#ifdef FEAT_CRYPT
155 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200156 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200158#ifdef FEAT_PERSISTENT_UNDO
159 context_sha256_T sha_ctx;
160 int read_undo_file = FALSE;
161#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100162 int split = 0; // number of split lines
163#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100165 int error = FALSE; // errors encountered
166 int ff_error = EOL_UNKNOWN; // file format with errors
167 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#ifdef UNIX
169 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100170 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#else
172 int perm;
173#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100174 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200176 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 int file_readonly;
178 linenr_T skip_count = 0;
179 linenr_T read_count = 0;
180 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100181 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
182 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100183 int try_mac;
184 int try_dos;
185 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100188 linenr_T conv_error = 0; // line nr with conversion error
189 linenr_T illegal_byte = 0; // line nr with illegal byte
190 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
191 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000192 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100193 // BAD_KEEP, BAD_DROP or character to
194 // replace with
195 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100197 char_u *fenc; // fileencoding to use
198 int fenc_alloced; // fenc_next is in allocated memory
199 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 int advance_fenc = FALSE;
201 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100202#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100203 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100204# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100205 int did_iconv = FALSE; // TRUE when iconv() failed and trying
206 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100208#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100209 int converted = FALSE; // TRUE if conversion done
210 int notconverted = FALSE; // TRUE if conversion wanted but it
211 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100213 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100214 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200215 buf_T *old_curbuf;
216 char_u *old_b_ffname;
217 char_u *old_b_fname;
218 int using_b_ffname;
219 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200220 static char *msg_is_a_directory = N_("is a directory");
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100221 int eof;
Christian Brabandtaae58342023-04-23 17:50:22 +0100222#ifdef FEAT_SODIUM
223 int may_need_lseek = FALSE;
224#endif
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200225
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100226 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200227
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100228 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
230 /*
231 * If there is no file name yet, use the one for the read file.
232 * BF_NOTEDITED is set to reflect this.
233 * Don't do this for a read from a filter.
234 * Only do this when 'cpoptions' contains the 'f' flag.
235 */
236 if (curbuf->b_ffname == NULL
237 && !filtering
238 && fname != NULL
239 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
240 && !(flags & READ_DUMMY))
241 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000242 if (set_rw_fname(fname, sfname) == FAIL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100243 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100246 // Remember the initial values of curbuf, curbuf->b_ffname and
247 // curbuf->b_fname to detect whether they are altered as a result of
248 // executing nasty autocommands. Also check if "fname" and "sfname"
249 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200250 old_curbuf = curbuf;
251 old_b_ffname = curbuf->b_ffname;
252 old_b_fname = curbuf->b_fname;
253 using_b_ffname = (fname == curbuf->b_ffname)
254 || (sfname == curbuf->b_ffname);
255 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200256
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100257 // After reading a file the cursor line changes but we don't want to
258 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000259 ex_no_reprint = TRUE;
260
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100261 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000262 need_fileinfo = FALSE;
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 /*
265 * For Unix: Use the short file name whenever possible.
266 * Avoids problems with networks and when directory names are changed.
267 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
268 * another directory, which we don't detect.
269 */
270 if (sfname == NULL)
271 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200272#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 fname = sfname;
274#endif
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 /*
277 * The BufReadCmd and FileReadCmd events intercept the reading process by
278 * executing the associated commands instead.
279 */
280 if (!filtering && !read_stdin && !read_buffer)
281 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100282 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100284 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
286 curbuf->b_op_start.col = 0;
287
288 if (newfile)
289 {
290 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
291 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200292 {
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100293 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200295 if (aborting())
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100296 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200298 // The BufReadCmd code usually uses ":read" to get the text and
299 // perhaps ":file" to change the buffer name. But we should
300 // consider this to work like ":edit", thus reset the
301 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
302 // same file.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100303 if (retval == OK)
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200304 curbuf->b_flags &= ~BF_NOTEDITED;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100305 goto theend;
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 }
308 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
309 FALSE, NULL, eap))
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_EVAL
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100312 retval = aborting() ? FAIL : OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#else
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100314 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100316 goto theend;
317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100319 curbuf->b_op_start = orig_start;
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100320
321 if (flags & READ_NOFILE)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100322 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100323 // Return NOTDONE instead of FAIL so that BufEnter can be triggered
324 // and other operations don't fail.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100325 retval = NOTDONE;
326 goto theend;
327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329
330 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100331 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100333 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000335 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200337 size_t namelen = STRLEN(fname);
338
339 // If the name is too long we might crash further on, quit here.
340 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000341 {
342 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
343 msg_end();
344 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100345 goto theend;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000346 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200347
348 // If the name ends in a path separator, we can't open it. Check here,
349 // because reading the file may actually work, but then creating the
350 // swap file may destroy it! Reported on MS-DOS and Win 95.
351 if (after_pathsep(fname, fname + namelen))
352 {
353 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
354 msg_end();
355 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100356 retval = NOTDONE;
357 goto theend;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200361 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100363#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200364 /*
365 * On Unix it is possible to read a directory, so we have to
366 * check for it before the mch_open().
367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100369 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
370 && !S_ISFIFO(perm) // ... or fifo
371 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000372# ifdef OPEN_CHR_FILES
373 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100374 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 )
377 {
378 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100379 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200380 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100381 retval = NOTDONE;
382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 else
384 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
385 msg_end();
386 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100387 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200389#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100390#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000391 /*
392 * MS-Windows allows opening a device, but we will probably get stuck
393 * trying to read it.
394 */
395 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
396 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000397 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000398 msg_end();
399 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100400 goto theend;
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000401 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000402#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200403 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000404
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200406 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408 /*
409 * When opening a new file we take the readonly flag from the file.
410 * Default is r/w, can be set to r/o below.
411 * Don't reset it when in readonly mode
412 * Only set/reset b_p_ro when BF_CHECK_RO is set.
413 */
414 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000415 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 curbuf->b_p_ro = FALSE;
417
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200418 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100420 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 if (mch_stat((char *)fname, &st) >= 0)
422 {
423 buf_store_time(curbuf, &st, fname);
424 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100425 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200426 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#ifdef UNIX
428 /*
429 * Use the protection bits of the original file for the swap file.
430 * This makes it possible for others to read the name of the
431 * edited file from the swapfile, but only if they can read the
432 * edited file.
433 * Remove the "write" and "execute" bits for group and others
434 * (they must not write the swapfile).
435 * Add the "read" and "write" bits for the user, otherwise we may
436 * not be able to write to the file ourselves.
437 * Setting the bits is done below, after creating the swap file.
438 */
439 swap_mode = (st.st_mode & 0644) | 0600;
440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef VMS
442 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000443 curbuf->b_fab_rat = st.st_fab_rat;
444 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#endif
446 }
447 else
448 {
449 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100450 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100452 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 curbuf->b_orig_size = 0;
454 curbuf->b_orig_mode = 0;
455 }
456
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100457 // Reset the "new file" flag. It will be set again below when the
458 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
460 }
461
462/*
463 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100464 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
466 file_readonly = FALSE;
467 if (read_stdin)
468 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100469#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100470 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 setmode(0, O_BINARY);
472#endif
473 }
474 else if (!read_buffer)
475 {
476#ifdef USE_MCH_ACCESS
477 if (
478# ifdef UNIX
479 !(perm & 0222) ||
480# endif
481 mch_access((char *)fname, W_OK))
482 file_readonly = TRUE;
483 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
484#else
485 if (!newfile
486 || readonlymode
487 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
488 {
489 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
492 }
493#endif
494 }
495
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
498#ifndef UNIX
499 int isdir_f;
500#endif
501 msg_scroll = msg_save;
502#ifndef UNIX
503 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100504 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 */
506 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100507 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (isdir_f)
509 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200510 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100511 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
513 else
514#endif
515 if (newfile)
516 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200517 if (perm < 0
518#ifdef ENOENT
519 && errno == ENOENT
520#endif
521 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 /*
524 * Set the 'new-file' flag, so that when the file has
525 * been created by someone else, a ":w" will complain.
526 */
527 curbuf->b_flags |= BF_NEW;
528
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // Create a swap file now, so that other Vims are warned
530 // that we are editing this file. Don't do this for a
531 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000536 if (curbuf != old_curbuf
537 || (using_b_ffname
538 && (old_b_ffname != curbuf->b_ffname))
539 || (using_b_fname
540 && (old_b_fname != curbuf->b_fname)))
541 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000542 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100543 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000544 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000545 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000546 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200547 filemess(curbuf, sfname,
548 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000549 else
550 filemess(curbuf, sfname,
551 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100553 // Even though this is a new file, it might have been
554 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 check_marks_read();
556#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100557 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200558 if (eap != NULL)
559 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
561 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100562 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 save_file_ff(curbuf);
564
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100565#if defined(FEAT_EVAL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100566 if (!aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100568 retval = OK; // a new file is not an error
569 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571 else
572 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000573 filemess(curbuf, sfname, (char_u *)(
574# ifdef EFBIG
575 (errno == EFBIG) ? _("[File too big]") :
576# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200577# ifdef EOVERFLOW
578 (errno == EOVERFLOW) ? _("[File too big]") :
579# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000580 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100581 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583 }
584
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100585 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587
588 /*
589 * Only set the 'ro' flag for readonly files the first time they are
590 * loaded. Help files always get readonly mode
591 */
592 if ((check_readonly && file_readonly) || curbuf->b_help)
593 curbuf->b_p_ro = TRUE;
594
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000595 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100597 // Don't change 'eol' if reading from buffer as it will already be
598 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000599 if (!read_buffer)
600 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100601 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100602 curbuf->b_start_eof = FALSE;
603 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000604 curbuf->b_start_eol = TRUE;
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000607 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
609
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100610 // Create a swap file now, so that other Vims are warned that we are
611 // editing this file.
612 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000616 if (!read_stdin && (curbuf != old_curbuf
617 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
618 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
619 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000620 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000621 if (!read_buffer)
622 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100623 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000627 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
628 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100629 {
630 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
631
632 /*
633 * If the group-read bit is set but not the world-read bit, then
634 * the group must be equal to the group of the original file. If
635 * we can't make that happen then reset the group-read bit. This
636 * avoids making the swap file readable to more users when the
637 * primary group of the user is too permissive.
638 */
639 if ((swap_mode & 044) == 040)
640 {
641 stat_T swap_st;
642
643 if (mch_stat((char *)swap_fname, &swap_st) >= 0
644 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200645# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100646 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200647 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200648# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200649 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100650 swap_mode &= 0600;
651 }
652
653 (void)mch_setperm(swap_fname, (long)swap_mode);
654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#endif
656 }
657
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200658 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (swap_exists_action == SEA_QUIT)
660 {
661 if (!read_buffer && !read_stdin)
662 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100663 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 /*
669 * Set '[ mark to the line above where the lines go (line 1 if zero).
670 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100671 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
673 curbuf->b_op_start.col = 0;
674
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100675 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
676 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
677 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (!read_buffer)
680 {
681 int m = msg_scroll;
682 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 /*
685 * The file must be closed again, the autocommands may want to change
686 * the file before reading it.
687 */
688 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /*
692 * The output from the autocommands should not overwrite anything and
693 * should not be overwritten: Set msg_scroll, restore its value if no
694 * output was done.
695 */
696 msg_scroll = TRUE;
697 if (filtering)
698 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
699 FALSE, curbuf, eap);
700 else if (read_stdin)
701 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
702 FALSE, curbuf, eap);
703 else if (newfile)
704 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
705 FALSE, curbuf, eap);
706 else
707 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
708 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100709 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100710 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
711 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
712 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100713 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (msg_scrolled == n)
716 msg_scroll = m;
717
718#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100719 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
721 --no_wait_return;
722 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100723 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100724 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 }
726#endif
727 /*
728 * Don't allow the autocommands to change the current buffer.
729 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000730 *
731 * Don't allow the autocommands to change the buffer name either
732 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
734 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000735 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
736 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
738 {
739 --no_wait_return;
740 msg_scroll = msg_save;
741 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000742 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000744 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100745 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100746 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100750 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
752
753 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
754 {
755 /*
756 * Show the user that we are busy reading the input. Sometimes this
757 * may take a while. When reading from stdin another program may
758 * still be running, don't move the cursor to the last line, unless
759 * always using the GUI.
760 */
761 if (read_stdin)
762 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100763 if (!is_not_a_term())
764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200766# ifdef VIMDLL
767 if (!gui.in_use)
768# endif
769 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100773 if (gui.in_use && !gui.dying && !gui.starting)
774 {
Amon Sha10197932022-02-21 15:07:12 +0000775 // make a copy, gui_write() may try to change it
776 p = vim_strsave((char_u *)_("Reading from stdin..."));
777 if (p != NULL)
778 {
779 gui_write(p, (int)STRLEN(p));
780 vim_free(p);
781 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 else if (!read_buffer)
787 filemess(curbuf, sfname, (char_u *)"", 0);
788 }
789
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /*
793 * Set linecnt now, before the "retry" caused by a wrong guess for
794 * fileformat, and after the autocommands, which may change them.
795 */
796 linecnt = curbuf->b_ml.ml_line_count;
797
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000799 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000800 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000801 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000802 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000803 curbuf->b_bad_char = eap->bad_char;
804 }
805 else
806 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000809 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 */
811 if (eap != NULL && eap->force_enc != 0)
812 {
813 fenc = enc_canonize(eap->cmd + eap->force_enc);
814 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000815 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817 else if (curbuf->b_p_bin)
818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100819 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 fenc_alloced = FALSE;
821 }
822 else if (curbuf->b_help)
823 {
824 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000825 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100827 // Help files are either utf-8 or latin1. Try utf-8 first, if this
828 // fails it must be latin1.
829 // Always do this when 'encoding' is "utf-8". Otherwise only do
830 // this when needed to avoid [converted] remarks all the time.
831 // It is needed when the first line contains non-ASCII characters.
832 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 fenc = (char_u *)"latin1";
834 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000835 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000837 fc = fname[STRLEN(fname) - 1];
838 if (TOLOWER_ASC(fc) == 'x')
839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Read the first line (and a bit more). Immediately rewind to
841 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100842 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200843 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000844 for (p = firstline; p < firstline + len; ++p)
845 if (*p >= 0x80)
846 {
847 c = TRUE;
848 break;
849 }
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852
853 if (c)
854 {
855 fenc_next = fenc;
856 fenc = (char_u *)"utf-8";
857
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // When the file is utf-8 but a character doesn't fit in
859 // 'encoding' don't retry. In help text editing utf-8 bytes
860 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000861 if (!enc_utf8)
862 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 fenc_alloced = FALSE;
865 }
866 else if (*p_fencs == NUL)
867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 fenc_alloced = FALSE;
870 }
871 else
872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200874 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
877 /*
878 * Jump back here to retry reading the file in different ways.
879 * Reasons to retry:
880 * - encoding conversion failed: try another one from "fenc_next"
881 * - BOM detected and fenc was set, need to setup conversion
882 * - "fileformat" check failed: try another
883 *
884 * Variables set for special retry actions:
885 * "file_rewind" Rewind the file to start reading it again.
886 * "advance_fenc" Advance "fenc" using "fenc_next".
887 * "skip_read" Re-use already read bytes (BOM detected).
888 * "did_iconv" iconv() conversion failed, try 'charconvert'.
889 * "keep_fileformat" Don't reset "fileformat".
890 *
891 * Other status indicators:
892 * "tmpname" When != NULL did conversion with 'charconvert'.
893 * Output file has to be deleted afterwards.
894 * "iconv_fd" When != -1 did conversion with iconv().
895 */
896retry:
897
898 if (file_rewind)
899 {
900 if (read_buffer)
901 {
902 read_buf_lnum = 1;
903 read_buf_col = 0;
904 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200905 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 error = TRUE;
909 goto failed;
910 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100911 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200913 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000915 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000918 curbuf->b_start_bomb = FALSE;
919 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000920 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922
923 /*
924 * When retrying with another "fenc" and the first time "fileformat"
925 * will be reset.
926 */
927 if (keep_fileformat)
928 keep_fileformat = FALSE;
929 else
930 {
931 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000934 try_unix = try_dos = try_mac = FALSE;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100937 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100939 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100941 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943
Bram Moolenaar13505972019-01-24 15:04:48 +0100944#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (iconv_fd != (iconv_t)-1)
946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 iconv_close(iconv_fd);
949 iconv_fd = (iconv_t)-1;
950 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 if (advance_fenc)
954 {
955 /*
956 * Try the next entry in 'fileencodings'.
957 */
958 advance_fenc = FALSE;
959
960 if (eap != NULL && eap->force_enc != 0)
961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 // Conversion given with "++cc=" wasn't possible, read
963 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000965 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (fenc_alloced)
967 vim_free(fenc);
968 fenc = (char_u *)"";
969 fenc_alloced = FALSE;
970 }
971 else
972 {
973 if (fenc_alloced)
974 vim_free(fenc);
975 if (fenc_next != NULL)
976 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200977 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 else
980 {
981 fenc = (char_u *)"";
982 fenc_alloced = FALSE;
983 }
984 }
985 if (tmpname != NULL)
986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100988 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 }
991
992 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000993 * Conversion may be required when the encoding of the file is different
994 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 */
996 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000997 converted = need_conversion(fenc);
998 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
1000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // "ucs-bom" means we need to check the first bytes of the file
1002 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1004 fio_flags = FIO_UCSBOM;
1005
1006 /*
1007 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1008 * done. This is handled below after read(). Prepare the
1009 * fio_flags to avoid having to parse the string each time.
1010 * Also check for Unicode to Latin1 conversion, because iconv()
1011 * appears not to handle this correctly. This works just like
1012 * conversion to UTF-8 except how the resulting character is put in
1013 * the buffer.
1014 */
1015 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1016 fio_flags = get_fio_flags(fenc);
1017
Bram Moolenaar4f974752019-02-17 17:44:42 +01001018#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /*
1020 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1021 * is handled with MultiByteToWideChar().
1022 */
1023 if (fio_flags == 0)
1024 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaar13505972019-01-24 15:04:48 +01001027#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001028 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (fio_flags == 0)
1030 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaar13505972019-01-24 15:04:48 +01001033#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 /*
1035 * Try using iconv() if we can't convert internally.
1036 */
1037 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001038# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 )
1042 iconv_fd = (iconv_t)my_iconv_open(
1043 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 /*
1048 * Use the 'charconvert' expression when conversion is required
1049 * and we can't do it internally or with iconv().
1050 */
1051 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001052 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001053# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 )
1057 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001058# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001060# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001061 // Skip conversion when it's already done (retry for wrong
1062 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (tmpname == NULL)
1064 {
1065 tmpname = readfile_charconvert(fname, fenc, &fd);
1066 if (tmpname == NULL)
1067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 advance_fenc = TRUE;
1070 if (fd < 0)
1071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001073 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 error = TRUE;
1075 goto failed;
1076 }
1077 goto retry;
1078 }
1079 }
1080 }
1081 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001085#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 )
1089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001090 // Conversion wanted but we can't.
1091 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 advance_fenc = TRUE;
1093 goto retry;
1094 }
1095 }
1096 }
1097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001098 // Set "can_retry" when it's possible to rewind the file and try with
1099 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1100 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001101 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 if (!skip_read)
1104 {
1105 linerest = 0;
1106 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001107 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 skip_count = lines_to_skip;
1109 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001111#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001112 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1113 && curbuf->b_ffname != NULL
1114 && curbuf->b_p_udf
1115 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001116 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001117 && !read_stdin
1118 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001119 if (read_undo_file)
1120 sha256_start(&sha_ctx);
1121#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001122#ifdef FEAT_CRYPT
1123 if (curbuf->b_cryptstate != NULL)
1124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Need to free the state, but keep the key, don't want to ask for
1126 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001127 crypt_free_state(curbuf->b_cryptstate);
1128 curbuf->b_cryptstate = NULL;
1129 }
1130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132
1133 while (!error && !got_int)
1134 {
1135 /*
1136 * We allocate as much space for the file as we can get, plus
1137 * space for the old line plus room for one terminating NUL.
1138 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001139 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001141 if (!skip_read)
1142 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001143#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001145#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 // Use buffer >= 64K. Add linerest to double the size if the
1147 // line gets very long, to avoid a lot of copying. But don't
1148 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001149 size = 0x10000L + linerest;
1150 if (size > 0x100000L)
1151 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001152#endif
1153 }
1154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001156 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001159 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 size = 1;
1161 }
1162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164 if (!skip_read)
1165 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001166 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001168 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 FALSE)) != NULL)
1170 break;
1171 }
1172 if (new_buffer == NULL)
1173 {
1174 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1175 error = TRUE;
1176 break;
1177 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001178 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1180 vim_free(buffer);
1181 buffer = new_buffer;
1182 ptr = buffer + linerest;
1183 line_start = buffer;
1184
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001185 // May need room to translate into.
1186 // For iconv() we don't really know the required space, use a
1187 // factor ICONV_MULT.
1188 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1189 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1190 // become up to 4 bytes, size must be multiple of 2
1191 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1192 // multiple of 2
1193 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1194 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001195 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001196#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (iconv_fd != (iconv_t)-1)
1198 size = size / ICONV_MULT;
1199 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (fio_flags & FIO_LATIN1)
1202 size = size / 2;
1203 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1204 size = (size * 2 / 3) & ~1;
1205 else if (fio_flags & FIO_UCS4)
1206 size = (size * 2 / 3) & ~3;
1207 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001208 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001209#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001211 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001212#endif
1213#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#endif
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (conv_restlen > 0)
1219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(ptr, conv_rest, conv_restlen);
1222 ptr += conv_restlen;
1223 size -= conv_restlen;
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 if (read_buffer)
1227 {
1228 /*
1229 * Read bytes from curbuf. Used for converting text read
1230 * from stdin.
1231 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001232 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (read_buf_lnum > from)
1234 size = 0;
1235 else
1236 {
1237 int n, ni;
1238 long tlen;
1239
1240 tlen = 0;
1241 for (;;)
1242 {
1243 p = ml_get(read_buf_lnum) + read_buf_col;
1244 n = (int)STRLEN(p);
1245 if ((int)tlen + n + 1 > size)
1246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001247 // Filled up to "size", append partial line.
1248 // Change NL to NUL to reverse the effect done
1249 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001250 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 for (ni = 0; ni < n; ++ni)
1252 {
1253 if (p[ni] == NL)
1254 ptr[tlen++] = NUL;
1255 else
1256 ptr[tlen++] = p[ni];
1257 }
1258 read_buf_col += n;
1259 break;
1260 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001261
1262 // Append whole line and new-line. Change NL
1263 // to NUL to reverse the effect done below.
1264 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001266 if (p[ni] == NL)
1267 ptr[tlen++] = NUL;
1268 else
1269 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001271 ptr[tlen++] = NL;
1272 read_buf_col = 0;
1273 if (++read_buf_lnum > from)
1274 {
1275 // When the last line didn't have an
1276 // end-of-line don't add it now either.
1277 if (!curbuf->b_p_eol)
1278 --tlen;
1279 size = tlen;
1280 eof = TRUE;
1281 break;
1282 }
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285 }
1286 }
1287 else
1288 {
1289 /*
1290 * Read bytes from the file.
1291 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001292# ifdef FEAT_SODIUM
1293 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001294 //
1295 // Sodium encryption requires a fixed block size to
1296 // successfully decrypt. However, unfortunately the file
1297 // header size changes between xchacha20 and xchacha20v2 by
1298 // 'add_len' bytes.
1299 // So we will now read the maximum header size + encryption
1300 // metadata, but after determining to read an xchacha20
1301 // encrypted file, we have to rewind the file descriptor by
1302 // 'add_len' bytes in the second round.
1303 //
1304 // Be careful with changing it, it needs to stay the same
1305 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001306 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001307 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001308 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001309 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001310 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1311 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001312 may_need_lseek = TRUE;
1313 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001314
Christian Brabandtaae58342023-04-23 17:50:22 +01001315 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1316 && crypt_method_is_sodium(
1317 curbuf->b_cryptstate->method_nr)))
1318 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001319 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001320 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1321 // description above)
1322 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1323 && !eof && may_need_lseek)
1324 {
1325 lseek(fd, crypt_get_header_len(
1326 curbuf->b_cryptstate->method_nr)
1327 - crypt_get_max_header_len(), SEEK_CUR);
1328 may_need_lseek = FALSE;
1329 }
1330 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001331# endif
1332 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001333 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001334 filesize_count += size;
1335 // hit end of file
1336 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001339#ifdef FEAT_CRYPT
1340 /*
1341 * At start of file: Check for magic number of encryption.
1342 */
1343 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001344 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001345 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1346 &filesize, newfile, sfname,
1347 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001348# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001349 if (curbuf->b_cryptstate != NULL
1350 && !crypt_works_inplace(curbuf->b_cryptstate))
1351 // reading undo file requires crypt_decode_inplace()
1352 read_undo_file = FALSE;
1353# endif
1354 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001355 /*
1356 * Decrypt the read bytes. This is done before checking for
1357 * EOF because the crypt layer may be buffering.
1358 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001359 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1360 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001361 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001362# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001363 if (crypt_works_inplace(curbuf->b_cryptstate))
1364 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001365# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001366 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1367 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001368# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001369 }
1370 else
1371 {
1372 char_u *newptr = NULL;
1373 int decrypted_size;
1374
1375 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001376 curbuf->b_cryptstate, ptr, size,
1377 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001378
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001379 if (decrypted_size < 0)
1380 {
1381 // error message already given
1382 error = TRUE;
1383 vim_free(newptr);
1384 break;
1385 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001386 // If the crypt layer is buffering, not producing
1387 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001388 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001389 continue;
1390
1391 if (linerest == 0)
1392 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001393 // Simple case: reuse returned buffer (may be
1394 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001395 new_buffer = newptr;
1396 }
1397 else
1398 {
1399 long_u new_size;
1400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001401 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001402 new_size = (long_u)(decrypted_size + linerest + 1);
1403 new_buffer = lalloc(new_size, FALSE);
1404 if (new_buffer == NULL)
1405 {
1406 do_outofmem_msg(new_size);
1407 error = TRUE;
1408 break;
1409 }
1410
1411 mch_memmove(new_buffer, buffer, linerest);
1412 if (newptr != NULL)
1413 mch_memmove(new_buffer + linerest, newptr,
1414 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001415 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001416 }
1417
1418 if (new_buffer != NULL)
1419 {
1420 vim_free(buffer);
1421 buffer = new_buffer;
1422 new_buffer = NULL;
1423 line_start = buffer;
1424 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001425 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001426 }
1427 size = decrypted_size;
1428 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001429# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001430 }
1431#endif
1432
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (size <= 0)
1434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001438 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001439 /*
1440 * Reached end-of-file but some trailing bytes could
1441 * not be converted. Truncated file?
1442 */
1443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001444 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001445 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001446#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001447 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001448#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001449 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001450 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001451 if (can_retry)
1452 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001453 if (conv_error == 0)
1454 conv_error = curbuf->b_ml.ml_line_count
1455 - linecnt + 1;
1456 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001457 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001458 else if (illegal_byte == 0)
1459 illegal_byte = curbuf->b_ml.ml_line_count
1460 - linecnt + 1;
1461 if (bad_char_behavior == BAD_DROP)
1462 {
1463 *(ptr - conv_restlen) = NUL;
1464 conv_restlen = 0;
1465 }
1466 else
1467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 // Replace the trailing bytes with the replacement
1469 // character if we were converting; if we weren't,
1470 // leave the UTF8 checking code to do it, as it
1471 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001472 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001473#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001474 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001475#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001476 ))
1477 {
1478 while (conv_restlen > 0)
1479 {
1480 *(--ptr) = bad_char_behavior;
1481 --conv_restlen;
1482 }
1483 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001485#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001486 if (iconv_fd != (iconv_t)-1)
1487 {
1488 iconv_close(iconv_fd);
1489 iconv_fd = (iconv_t)-1;
1490 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001491#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001492 }
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 skip_read = FALSE;
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /*
1499 * At start of file (or after crypt magic number): Check for BOM.
1500 * Also check for a BOM for other Unicode encodings, but not after
1501 * converting with 'charconvert' or when a BOM has already been
1502 * found.
1503 */
1504 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001505#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001506 || (cryptkey != NULL
1507 && filesize == crypt_get_header_len(
1508 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 )
1511 && (fio_flags == FIO_UCSBOM
1512 || (!curbuf->b_p_bomb
1513 && tmpname == NULL
1514 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1515 {
1516 char_u *ccname;
1517 int blen;
1518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (size < 2 || curbuf->b_p_bin)
1521 ccname = NULL;
1522 else
1523 ccname = check_for_bom(ptr, size, &blen,
1524 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1525 if (ccname != NULL)
1526 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 filesize += blen;
1529 size -= blen;
1530 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001531 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001534 curbuf->b_start_bomb = TRUE;
1535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537
1538 if (fio_flags == FIO_UCSBOM)
1539 {
1540 if (ccname == NULL)
1541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001542 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 advance_fenc = TRUE;
1544 }
1545 else
1546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001547 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (fenc_alloced)
1549 vim_free(fenc);
1550 fenc = ccname;
1551 fenc_alloced = FALSE;
1552 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 skip_read = TRUE;
1555 goto retry;
1556 }
1557 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001559 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001560 ptr -= conv_restlen;
1561 size += conv_restlen;
1562 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 /*
1564 * Break here for a read error or end-of-file.
1565 */
1566 if (size <= 0)
1567 break;
1568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar13505972019-01-24 15:04:48 +01001570#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (iconv_fd != (iconv_t)-1)
1572 {
1573 /*
1574 * Attempt conversion of the read bytes to 'encoding' using
1575 * iconv().
1576 */
1577 const char *fromp;
1578 char *top;
1579 size_t from_size;
1580 size_t to_size;
1581
1582 fromp = (char *)ptr;
1583 from_size = size;
1584 ptr += size;
1585 top = (char *)ptr;
1586 to_size = real_size - size;
1587
1588 /*
1589 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001590 * another conversion. Except for when there is no
1591 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001593 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1594 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1596 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001597 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001598 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001599 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 if (conv_error == 0)
1601 conv_error = readfile_linenr(linecnt,
1602 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001604 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001605 ++fromp;
1606 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001607 if (bad_char_behavior == BAD_KEEP)
1608 {
1609 *top++ = *(fromp - 1);
1610 --to_size;
1611 }
1612 else if (bad_char_behavior != BAD_DROP)
1613 {
1614 *top++ = bad_char_behavior;
1615 --to_size;
1616 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 if (from_size > 0)
1620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001621 // Some remaining characters, keep them for the next
1622 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1624 conv_restlen = (int)from_size;
1625 }
1626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001627 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 line_start = ptr - linerest;
1629 mch_memmove(line_start, buffer, (size_t)linerest);
1630 size = (long)((char_u *)top - ptr);
1631 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar4f974752019-02-17 17:44:42 +01001634#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (fio_flags & FIO_CODEPAGE)
1636 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001638 WCHAR ucs2buf[3];
1639 int ucs2len;
1640 int codepage = FIO_GET_CP(fio_flags);
1641 int bytelen;
1642 int found_bad;
1643 char replstr[2];
1644
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 /*
1646 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001647 * a codepage, using standard MS-Windows functions. This
1648 * requires two steps:
1649 * 1. convert from 'fileencoding' to ucs-2
1650 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001652 * Because there may be illegal bytes AND an incomplete byte
1653 * sequence at the end, we may have to do the conversion one
1654 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001657 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001658 if (bad_char_behavior > 0)
1659 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001661 replstr[0] = '?';
1662 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665 * Move the bytes to the end of the buffer, so that we have
1666 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001668 src = ptr + real_size - size;
1669 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001671 /*
1672 * Do the conversion.
1673 */
1674 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001675 while (size > 0)
1676 {
1677 found_bad = FALSE;
1678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001680 if (codepage == CP_UTF8)
1681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // Handle CP_UTF8 input ourselves to be able to handle
1683 // trailing bytes properly.
1684 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001685 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001686 if (bytelen > size)
1687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 // Only got some bytes of a character. Normally
1689 // it's put in "conv_rest", but if it's too long
1690 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001691 if (bytelen <= CONV_RESTLEN)
1692 break;
1693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001694 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001695 bytelen = size;
1696 found_bad = TRUE;
1697 }
1698 else
1699 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001700 int u8c = utf_ptr2char(src);
1701
Bram Moolenaar86e01082005-12-29 22:45:34 +00001702 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001703 found_bad = TRUE;
1704 ucs2buf[0] = u8c;
1705 ucs2len = 1;
1706 }
1707 }
1708 else
1709# endif
1710 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001711 // We don't know how long the byte sequence is, try
1712 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001713 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1714 ++bytelen)
1715 {
1716 ucs2len = MultiByteToWideChar(codepage,
1717 MB_ERR_INVALID_CHARS,
1718 (LPCSTR)src, bytelen,
1719 ucs2buf, 3);
1720 if (ucs2len > 0)
1721 break;
1722 }
1723 if (ucs2len == 0)
1724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001725 // If we have only one byte then it's probably an
1726 // incomplete byte sequence. Otherwise discard
1727 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001728 if (size == 1)
1729 break;
1730 found_bad = TRUE;
1731 bytelen = 1;
1732 }
1733 }
1734
1735 if (!found_bad)
1736 {
1737 int i;
1738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001739 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001740 if (enc_utf8)
1741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001743 for (i = 0; i < ucs2len; ++i)
1744 dst += utf_char2bytes(ucs2buf[i], dst);
1745 }
1746 else
1747 {
1748 BOOL bad = FALSE;
1749 int dstlen;
1750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 // From UCS-2 to "enc_codepage". If the
1752 // conversion uses the default character "?",
1753 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001754 dstlen = WideCharToMultiByte(enc_codepage, 0,
1755 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001756 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001757 replstr, &bad);
1758 if (bad)
1759 found_bad = TRUE;
1760 else
1761 dst += dstlen;
1762 }
1763 }
1764
1765 if (found_bad)
1766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001768 if (can_retry)
1769 goto rewind_retry;
1770 if (conv_error == 0)
1771 conv_error = readfile_linenr(linecnt, ptr, dst);
1772 if (bad_char_behavior != BAD_DROP)
1773 {
1774 if (bad_char_behavior == BAD_KEEP)
1775 {
1776 mch_memmove(dst, src, bytelen);
1777 dst += bytelen;
1778 }
1779 else
1780 *dst++ = bad_char_behavior;
1781 }
1782 }
1783
1784 src += bytelen;
1785 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001787
1788 if (size > 0)
1789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001790 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001791 mch_memmove(conv_rest, src, size);
1792 conv_restlen = size;
1793 }
1794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001796 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001799#endif
1800#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (fio_flags & FIO_MACROMAN)
1802 {
1803 /*
1804 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001805 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001807 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (fio_flags != 0)
1813 {
1814 int u8c;
1815 char_u *dest;
1816 char_u *tail = NULL;
1817
1818 /*
1819 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1820 * "enc_utf8" not set: Convert Unicode to Latin1.
1821 * Go from end to start through the buffer, because the number
1822 * of bytes may increase.
1823 * "dest" points to after where the UTF-8 bytes go, "p" points
1824 * to after the next character to convert.
1825 */
1826 dest = ptr + real_size;
1827 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1828 {
1829 p = ptr + size;
1830 if (fio_flags == FIO_UTF8)
1831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 tail = ptr + size - 1;
1834 while (tail > ptr && (*tail & 0xc0) == 0x80)
1835 --tail;
1836 if (tail + utf_byte2len(*tail) <= ptr + size)
1837 tail = NULL;
1838 else
1839 p = tail;
1840 }
1841 }
1842 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001844 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 p = ptr + (size & ~1);
1846 if (size & 1)
1847 tail = p;
1848 if ((fio_flags & FIO_UTF16) && p > ptr)
1849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001850 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (fio_flags & FIO_ENDIAN_L)
1852 {
1853 u8c = (*--p << 8);
1854 u8c += *--p;
1855 }
1856 else
1857 {
1858 u8c = *--p;
1859 u8c += (*--p << 8);
1860 }
1861 if (u8c >= 0xd800 && u8c <= 0xdbff)
1862 tail = p;
1863 else
1864 p += 2;
1865 }
1866 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001867 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001869 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 p = ptr + (size & ~3);
1871 if (size & 3)
1872 tail = p;
1873 }
1874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001875 // If there is a trailing incomplete sequence move it to
1876 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (tail != NULL)
1878 {
1879 conv_restlen = (int)((ptr + size) - tail);
1880 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1881 size -= conv_restlen;
1882 }
1883
1884
1885 while (p > ptr)
1886 {
1887 if (fio_flags & FIO_LATIN1)
1888 u8c = *--p;
1889 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1890 {
1891 if (fio_flags & FIO_ENDIAN_L)
1892 {
1893 u8c = (*--p << 8);
1894 u8c += *--p;
1895 }
1896 else
1897 {
1898 u8c = *--p;
1899 u8c += (*--p << 8);
1900 }
1901 if ((fio_flags & FIO_UTF16)
1902 && u8c >= 0xdc00 && u8c <= 0xdfff)
1903 {
1904 int u16c;
1905
1906 if (p == ptr)
1907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001908 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (can_retry)
1910 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001911 if (conv_error == 0)
1912 conv_error = readfile_linenr(linecnt,
1913 ptr, p);
1914 if (bad_char_behavior == BAD_DROP)
1915 continue;
1916 if (bad_char_behavior != BAD_KEEP)
1917 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920 // found second word of double-word, get the first
1921 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (fio_flags & FIO_ENDIAN_L)
1923 {
1924 u16c = (*--p << 8);
1925 u16c += *--p;
1926 }
1927 else
1928 {
1929 u16c = *--p;
1930 u16c += (*--p << 8);
1931 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001932 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1933 + (u8c & 0x3ff);
1934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001935 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (u16c < 0xd800 || u16c > 0xdbff)
1937 {
1938 if (can_retry)
1939 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001940 if (conv_error == 0)
1941 conv_error = readfile_linenr(linecnt,
1942 ptr, p);
1943 if (bad_char_behavior == BAD_DROP)
1944 continue;
1945 if (bad_char_behavior != BAD_KEEP)
1946 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 }
1950 else if (fio_flags & FIO_UCS4)
1951 {
1952 if (fio_flags & FIO_ENDIAN_L)
1953 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001954 u8c = (unsigned)*--p << 24;
1955 u8c += (unsigned)*--p << 16;
1956 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 u8c += *--p;
1958 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001962 u8c += (unsigned)*--p << 8;
1963 u8c += (unsigned)*--p << 16;
1964 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 if (*--p < 0x80)
1970 u8c = *p;
1971 else
1972 {
1973 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001974 p -= len;
1975 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (len == 0)
1977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 // Not a valid UTF-8 character, retry with
1979 // another fenc when possible, otherwise just
1980 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (can_retry)
1982 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001983 if (conv_error == 0)
1984 conv_error = readfile_linenr(linecnt,
1985 ptr, p);
1986 if (bad_char_behavior == BAD_DROP)
1987 continue;
1988 if (bad_char_behavior != BAD_KEEP)
1989 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 dest -= utf_char2len(u8c);
1996 (void)utf_char2bytes(u8c, dest);
1997 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 --dest;
2001 if (u8c >= 0x100)
2002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // character doesn't fit in latin1, retry with
2004 // another fenc when possible, otherwise just
2005 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002006 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002008 if (conv_error == 0)
2009 conv_error = readfile_linenr(linecnt, ptr, p);
2010 if (bad_char_behavior == BAD_DROP)
2011 ++dest;
2012 else if (bad_char_behavior == BAD_KEEP)
2013 *dest = u8c;
2014 else if (eap != NULL && eap->bad_char != 0)
2015 *dest = bad_char_behavior;
2016 else
2017 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019 else
2020 *dest = u8c;
2021 }
2022 }
2023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 line_start = dest - linerest;
2026 mch_memmove(line_start, buffer, (size_t)linerest);
2027 size = (long)((ptr + real_size) - dest);
2028 ptr = dest;
2029 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002030 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002032 int incomplete_tail = FALSE;
2033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002035 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002037 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002038 int l;
2039
2040 if (todo <= 0)
2041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (*p >= 0x80)
2043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002044 // A length of 1 means it's an illegal byte. Accept
2045 // an incomplete character at the end though, the next
2046 // read() will get the next bytes, we'll check it
2047 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002048 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002049 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // Avoid retrying with a different encoding when
2052 // a truncated file is more likely, or attempting
2053 // to read the rest of an incomplete sequence when
2054 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002055 if (p > ptr || filesize > 0)
2056 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // Incomplete byte sequence, move it to conv_rest[]
2058 // and try to read the rest of it, unless we've
2059 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002060 if (p > ptr)
2061 {
2062 conv_restlen = todo;
2063 mch_memmove(conv_rest, p, conv_restlen);
2064 size -= conv_restlen;
2065 break;
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002068 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // Illegal byte. If we can try another encoding
2071 // do that, unless at EOF where a truncated
2072 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002073 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002074 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002075#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002077 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2078 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002079#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002081 if (conv_error == 0 && illegal_byte == 0)
2082 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002085 if (bad_char_behavior == BAD_DROP)
2086 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002087 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002088 --p;
2089 --size;
2090 }
2091 else if (bad_char_behavior != BAD_KEEP)
2092 *p = bad_char_behavior;
2093 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002094 else
2095 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002098 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002103#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002104 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002106 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002108#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002110 advance_fenc = TRUE;
2111 file_rewind = TRUE;
2112 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 filesize += size;
2118
2119 /*
2120 * when reading the first part of a file: guess EOL type
2121 */
2122 if (fileformat == EOL_UNKNOWN)
2123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (try_dos || try_unix)
2126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002128 if (try_mac)
2129 try_mac = 1;
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 for (p = ptr; p < ptr + size; ++p)
2132 {
2133 if (*p == NL)
2134 {
2135 if (!try_unix
2136 || (try_dos && p > ptr && p[-1] == CAR))
2137 fileformat = EOL_DOS;
2138 else
2139 fileformat = EOL_UNIX;
2140 break;
2141 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002142 else if (*p == CAR && try_mac)
2143 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (fileformat == EOL_UNIX && try_mac)
2148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 try_mac = 1;
2151 try_unix = 1;
2152 for (; p >= ptr && *p != CAR; p--)
2153 ;
2154 if (p >= ptr)
2155 {
2156 for (p = ptr; p < ptr + size; ++p)
2157 {
2158 if (*p == NL)
2159 try_unix++;
2160 else if (*p == CAR)
2161 try_mac++;
2162 }
2163 if (try_mac > try_unix)
2164 fileformat = EOL_MAC;
2165 }
2166 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002167 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002168 // Looking for CR but found no end-of-line markers at
2169 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002170 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002173 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (fileformat == EOL_UNKNOWN && try_mac)
2175 fileformat = EOL_MAC;
2176
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002177 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (fileformat == EOL_UNKNOWN)
2179 fileformat = default_fileformat();
2180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002181 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 set_fileformat(fileformat, OPT_LOCAL);
2184 }
2185 }
2186
2187 /*
2188 * This loop is executed once for every character read.
2189 * Keep it fast!
2190 */
2191 if (fileformat == EOL_MAC)
2192 {
2193 --ptr;
2194 while (++ptr, --size >= 0)
2195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if ((c = *ptr) != NUL && c != CAR && c != NL)
2198 continue;
2199 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else
2204 {
2205 if (skip_count == 0)
2206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002207 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 len = (colnr_T) (ptr - line_start + 1);
2209 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2210 {
2211 error = TRUE;
2212 break;
2213 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002214#ifdef FEAT_PERSISTENT_UNDO
2215 if (read_undo_file)
2216 sha256_update(&sha_ctx, line_start, len);
2217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ++lnum;
2219 if (--read_count == 0)
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 error = TRUE; // break loop
2222 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 break;
2224 }
2225 }
2226 else
2227 --skip_count;
2228 line_start = ptr + 1;
2229 }
2230 }
2231 }
2232 else
2233 {
2234 --ptr;
2235 while (++ptr, --size >= 0)
2236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002237 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 continue;
2239 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 else
2242 {
2243 if (skip_count == 0)
2244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 len = (colnr_T)(ptr - line_start + 1);
2247 if (fileformat == EOL_DOS)
2248 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002249 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 ptr[-1] = NUL;
2253 --len;
2254 }
2255 /*
2256 * Reading in Dos format, but no CR-LF found!
2257 * When 'fileformats' includes "unix", delete all
2258 * the lines read so far and start all over again.
2259 * Otherwise give an error message later.
2260 */
2261 else if (ff_error != EOL_DOS)
2262 {
2263 if ( try_unix
2264 && !read_stdin
2265 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002266 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2267 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 set_fileformat(EOL_UNIX, OPT_LOCAL);
2272 file_rewind = TRUE;
2273 keep_fileformat = TRUE;
2274 goto retry;
2275 }
2276 ff_error = EOL_DOS;
2277 }
2278 }
2279 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2280 {
2281 error = TRUE;
2282 break;
2283 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002284#ifdef FEAT_PERSISTENT_UNDO
2285 if (read_undo_file)
2286 sha256_update(&sha_ctx, line_start, len);
2287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ++lnum;
2289 if (--read_count == 0)
2290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 error = TRUE; // break loop
2292 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 break;
2294 }
2295 }
2296 else
2297 --skip_count;
2298 line_start = ptr + 1;
2299 }
2300 }
2301 }
2302 linerest = (long)(ptr - line_start);
2303 ui_breakcheck();
2304 }
2305
2306failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (error && read_count == 0)
2309 error = FALSE;
2310
K.Takata3af98212022-11-01 20:36:19 +00002311 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2312 // In old days the file length was in sector count and the CTRL-Z the
2313 // marker where the file really ended. Assuming we write it to a file
2314 // system that keeps file length properly the CTRL-Z should be dropped.
2315 // Set the 'endoffile' option so the user can decide what to write later.
2316 // In Unix format the CTRL-Z is just another character.
2317 if (linerest != 0
2318 && !curbuf->b_p_bin
2319 && fileformat == EOL_DOS
2320 && ptr[-1] == Ctrl_Z)
2321 {
2322 ptr--;
2323 linerest--;
2324 if (set_options)
2325 curbuf->b_p_eof = TRUE;
2326 }
2327
2328 // If we get EOF in the middle of a line, note the fact by resetting
2329 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (!error
2331 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002332 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002335 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 curbuf->b_p_eol = FALSE;
2337 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002338 len = (colnr_T)(ptr - line_start + 1);
2339 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 error = TRUE;
2341 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002342 {
2343#ifdef FEAT_PERSISTENT_UNDO
2344 if (read_undo_file)
2345 sha256_update(&sha_ctx, line_start, len);
2346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002351 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002355 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002356 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002357 crypt_free_state(curbuf->b_cryptstate);
2358 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002359 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002360 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2361 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // Don't set cryptkey to NULL, it's used below as a flag that
2363 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#endif
2365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // If editing a new file: set 'fenc' for the current buffer.
2367 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002368 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002370 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (fenc_alloced)
2372 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002373#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#endif
2377
2378 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002379 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002380#ifdef HAVE_FD_CLOEXEC
2381 else
2382 {
2383 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002384
Bram Moolenaarf05da212009-11-17 16:13:15 +00002385 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002386 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002387 }
2388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 vim_free(buffer);
2390
2391#ifdef HAVE_DUP
2392 if (read_stdin)
2393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002394 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002396 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398#endif
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (tmpname != NULL)
2401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002402 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 vim_free(tmpname);
2404 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 /*
2408 * In recovery mode everything but autocommands is skipped.
2409 */
2410 if (!recoverymode)
2411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002412 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2414 {
2415#ifdef FEAT_NETBEANS_INTG
2416 netbeansFireChanges = 0;
2417#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002418 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#ifdef FEAT_NETBEANS_INTG
2420 netbeansFireChanges = 1;
2421#endif
2422 --linecnt;
2423 }
2424 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2425 if (filesize == 0)
2426 linecnt = 0;
2427 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002428 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002429 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002430#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // After reading the text into the buffer the diff info needs to
2432 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002433 diff_invalidate(curbuf);
2434#endif
2435#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002436 // All folds in the window are invalid now. Mark them for update
2437 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002438 foldUpdateAll(curwin);
2439#endif
2440 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002441 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 appended_lines_mark(from, linecnt);
2443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#ifndef ALWAYS_USE_GUI
2445 /*
2446 * If we were reading from the same terminal as where messages go,
2447 * the screen will have been messed up.
2448 * Switch on raw mode now and clear the screen.
2449 */
2450 if (read_stdin)
2451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002452 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 starttermcap();
2454 screenclear();
2455 }
2456#endif
2457
2458 if (got_int)
2459 {
2460 if (!(flags & READ_DUMMY))
2461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002462 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466 msg_scroll = msg_save;
2467#ifdef FEAT_VIMINFO
2468 check_marks_read();
2469#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002470 retval = OK; // an interrupt isn't really an error
2471 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473
2474 if (!filtering && !(flags & READ_DUMMY))
2475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002476 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 c = FALSE;
2478
2479#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002480 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 STRCAT(IObuff, _("[fifo]"));
2483 c = TRUE;
2484 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002485 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 STRCAT(IObuff, _("[socket]"));
2488 c = TRUE;
2489 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002490# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002491 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002492 {
2493 STRCAT(IObuff, _("[character special]"));
2494 c = TRUE;
2495 }
2496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#endif
2498 if (curbuf->b_p_ro)
2499 {
2500 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2501 c = TRUE;
2502 }
2503 if (read_no_eol_lnum)
2504 {
2505 msg_add_eol();
2506 c = TRUE;
2507 }
2508 if (ff_error == EOL_DOS)
2509 {
2510 STRCAT(IObuff, _("[CR missing]"));
2511 c = TRUE;
2512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (split)
2514 {
2515 STRCAT(IObuff, _("[long lines split]"));
2516 c = TRUE;
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if (notconverted)
2519 {
2520 STRCAT(IObuff, _("[NOT converted]"));
2521 c = TRUE;
2522 }
2523 else if (converted)
2524 {
2525 STRCAT(IObuff, _("[converted]"));
2526 c = TRUE;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#ifdef FEAT_CRYPT
2529 if (cryptkey != NULL)
2530 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002531 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 c = TRUE;
2533 }
2534#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002535 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002537 sprintf((char *)IObuff + STRLEN(IObuff),
2538 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 c = TRUE;
2540 }
2541 else if (illegal_byte > 0)
2542 {
2543 sprintf((char *)IObuff + STRLEN(IObuff),
2544 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2545 c = TRUE;
2546 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002547 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 STRCAT(IObuff, _("[READ ERRORS]"));
2550 c = TRUE;
2551 }
2552 if (msg_add_fileformat(fileformat))
2553 c = TRUE;
2554#ifdef FEAT_CRYPT
2555 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002556 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002557 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 else
2559#endif
2560 msg_add_lines(c, (long)linecnt, filesize);
2561
Bram Moolenaard23a8232018-02-10 18:45:26 +01002562 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 msg_scrolled_ign = TRUE;
2564#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002565 // Don't show the message when reading stdin, it would end up in a
2566 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (read_stdin || read_buffer)
2568 p = msg_may_trunc(FALSE, IObuff);
2569 else
2570#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002571 {
2572 if (msg_col > 0)
2573 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002574 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002577 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002578 // Need to repeat the message after redrawing when:
2579 // - When reading from stdin (the screen will be cleared next).
2580 // - When restart_edit is set (otherwise there will be a delay
2581 // before redrawing).
2582 // - When the screen was scrolled but there is no wait-return
2583 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002584 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 msg_scrolled_ign = FALSE;
2586 }
2587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002590 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002591 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 curbuf->b_p_ro = TRUE;
2593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002594 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 /*
2597 * In Ex mode: cursor at last new line.
2598 * Otherwise: cursor at first new line.
2599 */
2600 if (exmode_active)
2601 curwin->w_cursor.lnum = from + linecnt;
2602 else
2603 curwin->w_cursor.lnum = from + 1;
2604 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Bram Moolenaare1004402020-10-24 20:49:43 +02002607 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002608 {
2609 // Set '[ and '] marks to the newly read lines.
2610 curbuf->b_op_start.lnum = from + 1;
2611 curbuf->b_op_start.col = 0;
2612 curbuf->b_op_end.lnum = from + linecnt;
2613 curbuf->b_op_end.col = 0;
2614 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002615
Bram Moolenaar4f974752019-02-17 17:44:42 +01002616#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002617 /*
2618 * Work around a weird problem: When a file has two links (only
2619 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002620 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002621 * It's correct again after reading the file, thus reset the timestamp
2622 * here.
2623 */
2624 if (newfile && !read_stdin && !read_buffer
2625 && mch_stat((char *)fname, &st) >= 0)
2626 {
2627 buf_store_time(curbuf, &st, fname);
2628 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002629 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002630 }
2631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633 msg_scroll = msg_save;
2634
2635#ifdef FEAT_VIMINFO
2636 /*
2637 * Get the marks before executing autocommands, so they can be used there.
2638 */
2639 check_marks_read();
2640#endif
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002643 * We remember if the last line of the read didn't have
2644 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2645 * or writing the read again with 'binary' on. The latter is required
2646 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002648 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 // When reloading a buffer put the cursor at the first line that is
2651 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002652 if (flags & READ_KEEP_UNDO)
2653 u_find_first_changed();
2654
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002655#ifdef FEAT_PERSISTENT_UNDO
2656 /*
2657 * When opening a new file locate undo info and read it.
2658 */
2659 if (read_undo_file)
2660 {
2661 char_u hash[UNDO_HASH_SIZE];
2662
2663 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002664 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002665 }
2666#endif
2667
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002668 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 int m = msg_scroll;
2671 int n = msg_scrolled;
2672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002673 // Save the fileformat now, otherwise the buffer will be considered
2674 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002675 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 save_file_ff(curbuf);
2677
2678 /*
2679 * The output from the autocommands should not overwrite anything and
2680 * should not be overwritten: Set msg_scroll, restore its value if no
2681 * output was done.
2682 */
2683 msg_scroll = TRUE;
2684 if (filtering)
2685 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2686 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002687 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2690 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002691 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2692 /*
2693 * EVENT_FILETYPE was not triggered but the buffer already has a
2694 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2695 */
2696 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2697 TRUE, curbuf);
2698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 else
2700 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2701 FALSE, NULL, eap);
2702 if (msg_scrolled == n)
2703 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002704# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002705 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002706 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002710 if (!(recoverymode && error))
2711 retval = OK;
2712
2713theend:
2714 if (curbuf->b_ml.ml_mfp != NULL
2715 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2716 // OK to sync the swap file now
2717 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2718
2719 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720}
2721
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002722#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002723/*
2724 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2725 * which is the name of files used for process substitution output by
2726 * some shells on some operating systems, e.g., bash on SunOS.
2727 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2728 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002729 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002730is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002731{
2732 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2733 && VIM_ISDIGIT(fname[8])
2734 && *skipdigits(fname + 9) == NUL
2735 && (fname[9] != NUL
2736 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2737}
2738#endif
2739
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002740/*
2741 * From the current line count and characters read after that, estimate the
2742 * line number where we are now.
2743 * Used for error messages that include a line number.
2744 */
2745 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002746readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002747 linenr_T linecnt, // line count before reading more bytes
2748 char_u *p, // start of more bytes read
2749 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002750{
2751 char_u *s;
2752 linenr_T lnum;
2753
2754 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2755 for (s = p; s < endp; ++s)
2756 if (*s == '\n')
2757 ++lnum;
2758 return lnum;
2759}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761/*
Rob Pilling8196e942022-02-11 15:12:10 +00002762 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002763 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * Returns OK or FAIL.
2765 */
2766 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002767prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
Bram Moolenaar13505972019-01-24 15:04:48 +01002769 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (eap->cmd == NULL)
2771 return FAIL;
2772
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002773 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2774 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002775 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002776 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002777
2778 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002779 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002780 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 return OK;
2782}
2783
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002784/*
2785 * Set default or forced 'fileformat' and 'binary'.
2786 */
2787 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002788set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002789{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002790 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002791 if (set_options)
2792 {
2793 if (eap != NULL && eap->force_ff != 0)
2794 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2795 else if (*p_ffs != NUL)
2796 set_fileformat(default_fileformat(), OPT_LOCAL);
2797 }
2798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002799 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002800 if (eap != NULL && eap->force_bin != 0)
2801 {
2802 int oldval = curbuf->b_p_bin;
2803
2804 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2805 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2806 }
2807}
2808
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002809/*
2810 * Set forced 'fileencoding'.
2811 */
2812 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002813set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002814{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002815 if (eap->force_enc == 0)
2816 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002817
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002818 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2819
2820 if (fenc != NULL)
2821 set_string_option_direct((char_u *)"fenc", -1,
2822 fenc, OPT_FREE|OPT_LOCAL, 0);
2823 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002824}
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826/*
2827 * Find next fileencoding to use from 'fileencodings'.
2828 * "pp" points to fenc_next. It's advanced to the next item.
2829 * When there are no more items, an empty string is returned and *pp is set to
2830 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002831 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2832 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 */
2834 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002835next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 char_u *p;
2838 char_u *r;
2839
Bram Moolenaarf077db22019-08-13 00:18:24 +02002840 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (**pp == NUL)
2842 {
2843 *pp = NULL;
2844 return (char_u *)"";
2845 }
2846 p = vim_strchr(*pp, ',');
2847 if (p == NULL)
2848 {
2849 r = enc_canonize(*pp);
2850 *pp += STRLEN(*pp);
2851 }
2852 else
2853 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002854 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 *pp = p + 1;
2856 if (r != NULL)
2857 {
2858 p = enc_canonize(r);
2859 vim_free(r);
2860 r = p;
2861 }
2862 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002863 if (r != NULL)
2864 *alloced = TRUE;
2865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002867 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 r = (char_u *)"";
2869 *pp = NULL;
2870 }
2871 return r;
2872}
2873
Bram Moolenaar13505972019-01-24 15:04:48 +01002874#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875/*
2876 * Convert a file with the 'charconvert' expression.
2877 * This closes the file which is to be read, converts it and opens the
2878 * resulting file for reading.
2879 * Returns name of the resulting converted file (the caller should delete it
2880 * after reading it).
2881 * Returns NULL if the conversion failed ("*fdp" is not set) .
2882 */
2883 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002884readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002885 char_u *fname, // name of input file
2886 char_u *fenc, // converted from
2887 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
2889 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002890 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002892 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002894 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 else
2896 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002897 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 *fdp = -1;
2899 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2900 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002901 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2903 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002904 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907 if (errmsg != NULL)
2908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002909 // Don't use emsg(), it breaks mappings, the retry with
2910 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002911 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 if (tmpname != NULL)
2913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002914 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002915 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
2918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002919 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (*fdp < 0)
2921 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2922
2923 return tmpname;
2924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
2926
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002927#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002929 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2931 * *filesizep are updated.
2932 * Return the (new) encryption key, NULL for no encryption.
2933 */
2934 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002935check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002936 char_u *cryptkey, // previous encryption key or NULL
2937 char_u *ptr, // pointer to read bytes
2938 long *sizep, // length of read bytes
2939 off_T *filesizep, // nr of bytes used from file
2940 int newfile, // editing a new buffer
2941 char_u *fname, // file name to display
2942 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002944 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002945 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002946
2947 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002949 // Mark the buffer as read-only until the decryption has taken place.
2950 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002951 curbuf->b_p_ro = TRUE;
2952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002953 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002954 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002955 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002960 crypt_check_swapfile_curbuf();
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else
2963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 // When newfile is TRUE, store the typed key in the 'key'
2965 // option and don't free it. bf needs hash of the key saved.
2966 // Don't ask for the key again when first time Enter was hit.
2967 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002969 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002970 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002971 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002972 *did_ask = TRUE;
2973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002974 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (cryptkey != NULL && *cryptkey == NUL)
2976 {
2977 if (cryptkey != curbuf->b_p_key)
2978 vim_free(cryptkey);
2979 cryptkey = NULL;
2980 }
2981 }
2982 }
2983
2984 if (cryptkey != NULL)
2985 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002986 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002987
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002988 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002989 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002990 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002991 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002992
2993 curbuf->b_cryptstate = crypt_create_from_header(
2994 method, cryptkey, ptr);
2995 crypt_set_cm_option(curbuf, method);
2996
2997 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002998 *filesizep += header_len;
2999 *sizep -= header_len;
3000 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003002 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003003 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003006 // When starting to edit a new file which does not have encryption, clear
3007 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003008 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003009 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 return cryptkey;
3012}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003016 * Return TRUE if a file appears to be read-only from the file permissions.
3017 */
3018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003019check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003020 char_u *fname, // full path to file
3021 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003022{
3023#ifndef USE_MCH_ACCESS
3024 int fd = 0;
3025#endif
3026
3027 return (
3028#ifdef USE_MCH_ACCESS
3029# ifdef UNIX
3030 (perm & 0222) == 0 ||
3031# endif
3032 mch_access((char *)fname, W_OK)
3033#else
3034 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3035 ? TRUE : (close(fd), FALSE)
3036#endif
3037 );
3038}
3039
Bram Moolenaara7870192019-02-14 12:56:36 +01003040#if defined(HAVE_FSYNC) || defined(PROTO)
3041/*
3042 * Call fsync() with Mac-specific exception.
3043 * Return fsync() result: zero for success.
3044 */
3045 int
3046vim_fsync(int fd)
3047{
3048 int r;
3049
3050# ifdef MACOS_X
3051 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003052 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003053# endif
3054 r = fsync(fd);
3055 return r;
3056}
3057#endif
3058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003060 * Set the name of the current buffer. Use when the buffer doesn't have a
3061 * name and a ":r" or ":w" command with a file name is used.
3062 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003063 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003064set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003065{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003066 buf_T *buf = curbuf;
3067
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003068 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003069 if (curbuf->b_p_bl)
3070 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3071 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003072#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003073 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003074 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003075#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003076 if (curbuf != buf)
3077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003078 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003079 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003080 return FAIL;
3081 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003082
3083 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3084 curbuf->b_flags |= BF_NOTEDITED;
3085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003086 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003087 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3088 if (curbuf->b_p_bl)
3089 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003090#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003091 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003092 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003093#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003095 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003096 if (*curbuf->b_p_ft == NUL)
3097 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003098 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003099 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003100 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003101 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003102
3103 return OK;
3104}
3105
3106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Put file name into IObuff with quotes.
3108 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003109 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 if (fname == NULL)
3113 fname = (char_u *)"-stdin-";
3114 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3115 IObuff[0] = '"';
3116 STRCAT(IObuff, "\" ");
3117}
3118
3119/*
3120 * Append message for text mode to IObuff.
3121 * Return TRUE if something appended.
3122 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003123 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003124msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126#ifndef USE_CRNL
3127 if (eol_type == EOL_DOS)
3128 {
3129 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3130 return TRUE;
3131 }
3132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (eol_type == EOL_MAC)
3134 {
3135 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3136 return TRUE;
3137 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003138#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (eol_type == EOL_UNIX)
3140 {
3141 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3142 return TRUE;
3143 }
3144#endif
3145 return FALSE;
3146}
3147
3148/*
3149 * Append line and character count to IObuff.
3150 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152msg_add_lines(
3153 int insert_space,
3154 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003155 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 char_u *p;
3158
3159 p = IObuff + STRLEN(IObuff);
3160
3161 if (insert_space)
3162 *p++ = ' ';
3163 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003164 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003165 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else
3167 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003168 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003170 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003171 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003172 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174}
3175
3176/*
3177 * Append message for missing line separator to IObuff.
3178 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3183}
3184
Bram Moolenaar473952e2019-09-28 16:30:04 +02003185 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003186time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
ichizokdef69df2021-10-15 17:23:12 +01003188 return
3189#ifdef ST_MTIM_NSEC
3190 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
ichizokdef69df2021-10-15 17:23:12 +01003192#if defined(__linux__) || defined(MSWIN)
3193 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3194 // the seconds. Since the roundoff is done when flushing the inode, the
3195 // time may change unexpectedly by one second!!!
3196 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3197#else
3198 (long)st->st_mtime != mtime
3199#endif
3200 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003204 * Return TRUE if file encoding "fenc" requires conversion from or to
3205 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003207 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003208need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003210 int same_encoding;
3211 int enc_flags;
3212 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003214 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003215 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003216 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003217 fenc_flags = 0;
3218 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003219 else
3220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003221 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3222 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003223 enc_flags = get_fio_flags(p_enc);
3224 fenc_flags = get_fio_flags(fenc);
3225 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3226 }
3227 if (same_encoding)
3228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 // Specified encoding matches with 'encoding'. This requires
3230 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003231 return enc_unicode != 0;
3232 }
3233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234 // Encodings differ. However, conversion is not needed when 'enc' is any
3235 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003236 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237}
3238
3239/*
3240 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3241 * internal conversion.
3242 * if "ptr" is an empty string, use 'encoding'.
3243 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003245get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int prop;
3248
3249 if (*ptr == NUL)
3250 ptr = p_enc;
3251
3252 prop = enc_canon_props(ptr);
3253 if (prop & ENC_UNICODE)
3254 {
3255 if (prop & ENC_2BYTE)
3256 {
3257 if (prop & ENC_ENDIAN_L)
3258 return FIO_UCS2 | FIO_ENDIAN_L;
3259 return FIO_UCS2;
3260 }
3261 if (prop & ENC_4BYTE)
3262 {
3263 if (prop & ENC_ENDIAN_L)
3264 return FIO_UCS4 | FIO_ENDIAN_L;
3265 return FIO_UCS4;
3266 }
3267 if (prop & ENC_2WORD)
3268 {
3269 if (prop & ENC_ENDIAN_L)
3270 return FIO_UTF16 | FIO_ENDIAN_L;
3271 return FIO_UTF16;
3272 }
3273 return FIO_UTF8;
3274 }
3275 if (prop & ENC_LATIN1)
3276 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003277 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 return 0;
3279}
3280
Bram Moolenaar473952e2019-09-28 16:30:04 +02003281#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
3283 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3284 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3285 * Used for conversion between 'encoding' and 'fileencoding'.
3286 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003287 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003288get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 int cp;
3291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003292 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (!enc_utf8 && enc_codepage <= 0)
3294 return 0;
3295
3296 cp = encname2codepage(ptr);
3297 if (cp == 0)
3298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003299# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (STRCMP(ptr, "utf-8") == 0)
3301 cp = CP_UTF8;
3302 else
3303# endif
3304 return 0;
3305 }
3306 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3307}
3308#endif
3309
Bram Moolenaar473952e2019-09-28 16:30:04 +02003310#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311/*
3312 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3313 * needed for the internal conversion to/from utf-8 or latin1.
3314 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003315 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003316get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3319 && (enc_canon_props(ptr) & ENC_MACROMAN))
3320 return FIO_MACROMAN;
3321 return 0;
3322}
3323#endif
3324
3325/*
3326 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3327 * "size" must be at least 2.
3328 * Return the name of the encoding and set "*lenp" to the length.
3329 * Returns NULL when no BOM found.
3330 */
3331 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003332check_for_bom(
3333 char_u *p,
3334 long size,
3335 int *lenp,
3336 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 char *name = NULL;
3339 int len = 2;
3340
3341 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003342 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003344 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 len = 3;
3346 }
3347 else if (p[0] == 0xff && p[1] == 0xfe)
3348 {
3349 if (size >= 4 && p[2] == 0 && p[3] == 0
3350 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003352 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 len = 4;
3354 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003355 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003357 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003358 // utf-16le is preferred, it also works for ucs-2le text
3359 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else if (p[0] == 0xfe && p[1] == 0xff
3362 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003364 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003365 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003366 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003367 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003368 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3371 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003373 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 len = 4;
3375 }
3376
3377 *lenp = len;
3378 return (char_u *)name;
3379}
3380
3381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 * Try to find a shortname by comparing the fullname with the current
3383 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003384 * Returns "full_path" or pointer into "full_path" if shortened.
3385 */
3386 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003387shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003388{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003389 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003390 char_u *p = full_path;
3391
Bram Moolenaard9462e32011-04-11 21:35:11 +02003392 dirname = alloc(MAXPATHL);
3393 if (dirname == NULL)
3394 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003395 if (mch_dirname(dirname, MAXPATHL) == OK)
3396 {
3397 p = shorten_fname(full_path, dirname);
3398 if (p == NULL || *p == NUL)
3399 p = full_path;
3400 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003401 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003402 return p;
3403}
3404
3405/*
3406 * Try to find a shortname by comparing the fullname with the current
3407 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 * Returns NULL if not shorter name possible, pointer into "full_path"
3409 * otherwise.
3410 */
3411 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 int len;
3415 char_u *p;
3416
3417 if (full_path == NULL)
3418 return NULL;
3419 len = (int)STRLEN(dir_name);
3420 if (fnamencmp(dir_name, full_path, len) == 0)
3421 {
3422 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003423#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003425 * MS-Windows: when a file is in the root directory, dir_name will end
3426 * in a slash, since C: by itself does not define a specific dir. In
3427 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
3429 if (!((len > 2) && (*(p - 2) == ':')))
3430#endif
3431 {
3432 if (vim_ispathsep(*p))
3433 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003434#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 else
3436 p = NULL;
3437#endif
3438 }
3439 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003440#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /*
3442 * When using a file in the current drive, remove the drive name:
3443 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3444 * a floppy from "A:\dir" to "B:\dir".
3445 */
3446 else if (len > 3
3447 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3448 && full_path[1] == ':'
3449 && vim_ispathsep(full_path[2]))
3450 p = full_path + 2;
3451#endif
3452 else
3453 p = NULL;
3454 return p;
3455}
3456
3457/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003458 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 * When "force" is TRUE: Use full path from now on for files currently being
3460 * edited, both for file name and swap file name. Try to shorten the file
3461 * names a bit, if safe to do so.
3462 * When "force" is FALSE: Only try to shorten absolute file names.
3463 * For buffers that have buftype "nofile" or "scratch": never change the file
3464 * name.
3465 */
3466 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003467shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3468{
3469 char_u *p;
3470
3471 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003472 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003473 && !path_with_url(buf->b_fname)
3474 && (force
3475 || buf->b_sfname == NULL
3476 || mch_isFullName(buf->b_sfname)))
3477 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003478 if (buf->b_sfname != buf->b_ffname)
3479 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003480 p = shorten_fname(buf->b_ffname, dirname);
3481 if (p != NULL)
3482 {
3483 buf->b_sfname = vim_strsave(p);
3484 buf->b_fname = buf->b_sfname;
3485 }
3486 if (p == NULL || buf->b_fname == NULL)
3487 buf->b_fname = buf->b_ffname;
3488 }
3489}
3490
3491/*
3492 * Shorten filenames for all buffers.
3493 */
3494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003495shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 char_u dirname[MAXPATHL];
3498 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003501 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003503 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003504
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003505 // Always make the swap file name a full path, a "nofile" buffer may
3506 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003507 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003510 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003511#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003512 popup_update_preview_title();
3513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
3516#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3517 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003518 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 || defined(PROTO)
3520/*
3521 * Shorten all filenames in "fnames[count]" by current directory.
3522 */
3523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003524shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 int i;
3527 char_u dirname[MAXPATHL];
3528 char_u *p;
3529
3530 if (fnames == NULL || count < 1)
3531 return;
3532 mch_dirname(dirname, sizeof(dirname));
3533 for (i = 0; i < count; ++i)
3534 {
3535 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 // shorten_fname() returns pointer in given "fnames[i]". If free
3538 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3539 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 p = vim_strsave(p);
3541 vim_free(fnames[i]);
3542 fnames[i] = p;
3543 }
3544 }
3545}
3546#endif
3547
3548/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003549 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 * fo_o_h.ext for MSDOS or when shortname option set.
3551 *
3552 * Assumed that fname is a valid name found in the filesystem we assure that
3553 * the return value is a different name and ends in 'ext'.
3554 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3555 * characters otherwise.
3556 * Space for the returned name is allocated, must be freed later.
3557 * Returns NULL when out of memory.
3558 */
3559 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003560modname(
3561 char_u *fname,
3562 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003563 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003565 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 fname, ext, prepend_dot);
3567}
3568
3569 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003570buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003571 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572 char_u *fname,
3573 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003574 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 char_u *retval;
3577 char_u *s;
3578 char_u *e;
3579 char_u *ptr;
3580 int fnamelen, extlen;
3581
3582 extlen = (int)STRLEN(ext);
3583
3584 /*
3585 * If there is no file name we must get the name of the current directory
3586 * (we need the full path in case :cd is used).
3587 */
3588 if (fname == NULL || *fname == NUL)
3589 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003590 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (retval == NULL)
3592 return NULL;
3593 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3594 (fnamelen = (int)STRLEN(retval)) == 0)
3595 {
3596 vim_free(retval);
3597 return NULL;
3598 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003599 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 retval[fnamelen++] = PATHSEP;
3602 retval[fnamelen] = NUL;
3603 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003604 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606 else
3607 {
3608 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003609 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (retval == NULL)
3611 return NULL;
3612 STRCPY(retval, fname);
3613#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003614 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615#endif
3616 }
3617
3618 /*
3619 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3620 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3621 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3622 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3623 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003624 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003626 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003627 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003630 {
3631 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003636 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3638 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 s = ptr + STRLEN(ptr);
3641
3642 /*
3643 * For 8.3 file names we may have to reduce the length.
3644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 /*
3648 * If there is no file name, or the file name ends in '/', and the
3649 * extension starts with '.', put a '_' before the dot, because just
3650 * ".ext" is invalid.
3651 */
3652 if (fname == NULL || *fname == NUL
3653 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 *s++ = '_';
3657 }
3658 /*
3659 * If the extension starts with '.', truncate the base name at 8
3660 * characters
3661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003664 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 s = ptr + 8;
3667 *s = '\0';
3668 }
3669 }
3670 /*
3671 * If the extension doesn't start with '.', and the file name
3672 * doesn't have an extension yet, append a '.'
3673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else if ((e = vim_strchr(ptr, '.')) == NULL)
3675 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 /*
3677 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003678 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 */
3680 else if ((int)STRLEN(e) + extlen > 4)
3681 s = e + 4 - extlen;
3682 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003683#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * If there is no file name, and the extension starts with '.', put a
3686 * '_' before the dot, because just ".ext" may be invalid if it's on a
3687 * FAT partition, and on HPFS it doesn't matter.
3688 */
3689 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3690 *s++ = '_';
3691#endif
3692
3693 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003694 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 * ext can start with '.' and cannot exceed 3 more characters.
3696 */
3697 STRCPY(s, ext);
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 /*
3700 * Prepend the dot.
3701 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003702 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003704 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
3709 * Check that, after appending the extension, the file name is really
3710 * different.
3711 */
3712 if (fname != NULL && STRCMP(fname, retval) == 0)
3713 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 while (--s >= ptr)
3716 {
3717 if (*s != '_')
3718 {
3719 *s = '_';
3720 break;
3721 }
3722 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003723 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 *ptr = 'v';
3725 }
3726 return retval;
3727}
3728
3729/*
3730 * Like fgets(), but if the file line is too long, it is truncated and the
3731 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003732 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 */
3734 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003735vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 char *eof;
3738#define FGETS_SIZE 200
3739 char tbuf[FGETS_SIZE];
3740
3741 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3744 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003745 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003747 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 do
3749 {
3750 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003751 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3753 }
3754 return (eof == NULL);
3755}
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757/*
3758 * rename() only works if both files are on the same file system, this
3759 * function will (attempts to?) copy the file across if rename fails -- webb
3760 * Return -1 for failure, 0 for success.
3761 */
3762 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003763vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 int fd_in;
3766 int fd_out;
3767 int n;
3768 char *errmsg = NULL;
3769 char *buffer;
3770#ifdef AMIGA
3771 BPTR flock;
3772#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003773 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003774 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003775#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003776 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003777#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003778 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003781 * When the names are identical, there is nothing to do. When they refer
3782 * to the same file (ignoring case and slash/backslash differences) but
3783 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 */
3785 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003786 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003787 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003788 use_tmp_file = TRUE;
3789 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003790 return 0;
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /*
3794 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3795 */
3796 if (mch_stat((char *)from, &st) < 0)
3797 return -1;
3798
Bram Moolenaar3576da72008-12-30 15:15:57 +00003799#ifdef UNIX
3800 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003801 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003803 // It's possible for the source and destination to be the same file.
3804 // This happens when "from" and "to" differ in case and are on a FAT32
3805 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003806 if (mch_stat((char *)to, &st_to) >= 0
3807 && st.st_dev == st_to.st_dev
3808 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003809 use_tmp_file = TRUE;
3810 }
3811#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003812#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003813 {
3814 BY_HANDLE_FILE_INFORMATION info1, info2;
3815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003816 // It's possible for the source and destination to be the same file.
3817 // In that case go through a temp file name. This makes rename("foo",
3818 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003819 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3820 && win32_fileinfo(to, &info2) == FILEINFO_OK
3821 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3822 && info1.nFileIndexHigh == info2.nFileIndexHigh
3823 && info1.nFileIndexLow == info2.nFileIndexLow)
3824 use_tmp_file = TRUE;
3825 }
3826#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003827
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003828 if (use_tmp_file)
3829 {
3830 char tempname[MAXPATHL + 1];
3831
3832 /*
3833 * Find a name that doesn't exist and is in the same directory.
3834 * Rename "from" to "tempname" and then rename "tempname" to "to".
3835 */
3836 if (STRLEN(from) >= MAXPATHL - 5)
3837 return -1;
3838 STRCPY(tempname, from);
3839 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003840 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003841 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3842 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003843 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003844 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003845 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003846 if (mch_rename(tempname, (char *)to) == 0)
3847 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003848 // Strange, the second step failed. Try moving the
3849 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003850 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003851 return -1;
3852 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 // If it fails for one temp name it will most likely fail
3854 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003855 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003856 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003857 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003858 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003859 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 /*
3862 * Delete the "to" file, this is required on some systems to make the
3863 * mch_rename() work, on other systems it makes sure that we don't have
3864 * two files when the mch_rename() fails.
3865 */
3866
3867#ifdef AMIGA
3868 /*
3869 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3870 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003871 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 * deleting the "from" file (horror!) we lock it during the remove.
3873 *
3874 * When used for making a backup before writing the file: This should not
3875 * happen with ":w", because startscript() should detect this problem and
3876 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3877 * name. This problem does exist with ":w filename", but then the
3878 * original file will be somewhere else so the backup isn't really
3879 * important. If autoscripting is off the rename may fail.
3880 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003881 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#endif
3883 mch_remove(to);
3884#ifdef AMIGA
3885 if (flock)
3886 UnLock(flock);
3887#endif
3888
3889 /*
3890 * First try a normal rename, return if it works.
3891 */
3892 if (mch_rename((char *)from, (char *)to) == 0)
3893 return 0;
3894
3895 /*
3896 * Rename() failed, try copying the file.
3897 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003898 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003899#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003900 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003901 acl = mch_get_acl(from);
3902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3904 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003905 {
3906#ifdef HAVE_ACL
3907 mch_free_acl(acl);
3908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003910 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003912 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003913 fd_out = mch_open((char *)to,
3914 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (fd_out == -1)
3916 {
3917 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003918#ifdef HAVE_ACL
3919 mch_free_acl(acl);
3920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return -1;
3922 }
3923
Bram Moolenaar473952e2019-09-28 16:30:04 +02003924 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (buffer == NULL)
3926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003928 close(fd_in);
3929#ifdef HAVE_ACL
3930 mch_free_acl(acl);
3931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return -1;
3933 }
3934
Bram Moolenaar473952e2019-09-28 16:30:04 +02003935 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003936 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003938 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 break;
3940 }
3941
3942 vim_free(buffer);
3943 close(fd_in);
3944 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003945 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (n < 0)
3947 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003948 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 to = from;
3950 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003952 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003953#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003954#ifdef HAVE_ACL
3955 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003956 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003957#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003958#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003959 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (errmsg != NULL)
3962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003963 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return -1;
3965 }
3966 mch_remove(from);
3967 return 0;
3968}
3969
3970static int already_warned = FALSE;
3971
3972/*
3973 * Check if any not hidden buffer has been changed.
3974 * Postpone the check if there are characters in the stuff buffer, a global
3975 * command is being executed, a mapping is being executed or an autocommand is
3976 * busy.
3977 * Returns TRUE if some message was written (screen should be redrawn and
3978 * cursor positioned).
3979 */
3980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003982 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 buf_T *buf;
3985 int didit = 0;
3986 int n;
3987
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003988 // Don't check timestamps while system() or another low-level function may
3989 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (no_check_timestamps > 0)
3991 return FALSE;
3992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3994 // event and we would keep on checking if the file is steadily growing.
3995 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (focus && did_check_timestamps)
3997 {
3998 need_check_timestamps = TRUE;
3999 return FALSE;
4000 }
4001
4002 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004003 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004004 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 else
4006 {
4007 ++no_wait_return;
4008 did_check_timestamps = TRUE;
4009 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004010 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (buf->b_nwindows > 0)
4014 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004015 bufref_T bufref;
4016
4017 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 n = buf_check_timestamp(buf, focus);
4019 if (didit < n)
4020 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004021 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004023 // Autocommands have removed the buffer, start at the
4024 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 buf = firstbuf;
4026 continue;
4027 }
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 --no_wait_return;
4031 need_check_timestamps = FALSE;
4032 if (need_wait_return && didit == 2)
4033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004034 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004035 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 out_flush();
4037 }
4038 }
4039 return didit;
4040}
4041
4042/*
4043 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4044 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4045 * empty.
4046 */
4047 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004048move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 buf_T *tbuf = curbuf;
4051 int retval = OK;
4052 linenr_T lnum;
4053 char_u *p;
4054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004055 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 curbuf = tobuf;
4057 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4058 {
4059 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4060 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4061 {
4062 vim_free(p);
4063 retval = FAIL;
4064 break;
4065 }
4066 vim_free(p);
4067 }
4068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004069 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (retval != FAIL)
4071 {
4072 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004073 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004074 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 // Oops! We could try putting back the saved lines, but that
4077 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 retval = FAIL;
4079 break;
4080 }
4081 }
4082
4083 curbuf = tbuf;
4084 return retval;
4085}
4086
4087/*
4088 * Check if buffer "buf" has been changed.
4089 * Also check if the file for a new buffer unexpectedly appeared.
4090 * return 1 if a changed buffer was found.
4091 * return 2 if a message has been displayed.
4092 * return 0 otherwise.
4093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004095buf_check_timestamp(
4096 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004097 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004099 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 int stat_res;
4101 int retval = 0;
4102 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004103 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004105 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004107 enum {
4108 RELOAD_NONE,
4109 RELOAD_NORMAL,
4110 RELOAD_DETECT
4111 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004112 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4114 int can_reload = FALSE;
4115#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004116 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 int orig_mode = buf->b_orig_mode;
4118#ifdef FEAT_GUI
4119 int save_mouse_correct = need_mouse_correct;
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004122 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004123#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004124 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004125#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004126 bufref_T bufref;
4127
4128 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004130 // If there is no file name, the buffer is not loaded, 'buftype' is
4131 // set, we are in the middle of a save or being called recursively: ignore
4132 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (buf->b_ffname == NULL
4134 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004135 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004138#ifdef FEAT_NETBEANS_INTG
4139 || isNetbeansBuffer(buf)
4140#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004141#ifdef FEAT_TERMINAL
4142 || buf->b_term != NULL
4143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 )
4145 return 0;
4146
4147 if ( !(buf->b_flags & BF_NOTEDITED)
4148 && buf->b_mtime != 0
4149 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004150 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004151 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#ifdef HAVE_ST_MODE
4153 || (int)st.st_mode != buf->b_orig_mode
4154#else
4155 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4156#endif
4157 ))
4158 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004159 long prev_b_mtime = buf->b_mtime;
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 retval = 1;
4162
Bram Moolenaar386bc822018-07-07 18:34:12 +02004163 // set b_mtime to stop further warnings (e.g., when executing
4164 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (stat_res < 0)
4166 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004167 // Check the file again later to see if it re-appears.
4168 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 buf->b_orig_size = 0;
4170 buf->b_orig_mode = 0;
4171 }
4172 else
4173 buf_store_time(buf, &st, buf->b_ffname);
4174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004175 // Don't do anything for a directory. Might contain the file
4176 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (mch_isdir(buf->b_fname))
4178 ;
4179
4180 /*
4181 * If 'autoread' is set, the buffer has no changes and the file still
4182 * exists, reload the buffer. Use the buffer-local option value if it
4183 * was set, the global option value otherwise.
4184 */
4185 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4186 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004187 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
4189 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004190 if (stat_res < 0)
4191 reason = "deleted";
4192 else if (bufIsChanged(buf))
4193 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004194 /*
4195 * Check if the file contents really changed to avoid giving a
4196 * warning when only the timestamp was set (e.g., checked out of
4197 * CVS). Always warn when the buffer was changed.
4198 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004199 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4200 reason = "changed";
4201 else if (orig_mode != buf->b_orig_mode)
4202 reason = "mode";
4203 else
4204 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 /*
4207 * Only give the warning if there are no FileChangedShell
4208 * autocommands.
4209 * Avoid being called recursively by setting "busy".
4210 */
4211 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004212#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004213 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4214 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004215#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004216 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4218 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004219 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 busy = FALSE;
4221 if (n)
4222 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004223 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004224 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004225#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004226 s = get_vim_var_str(VV_FCS_CHOICE);
4227 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004228 reload = RELOAD_NORMAL;
4229 else if (STRCMP(s, "edit") == 0)
4230 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004231 else if (STRCMP(s, "ask") == 0)
4232 n = FALSE;
4233 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004234#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004235 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004239 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004240 {
4241 // Only give the message once.
4242 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004243 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 else
4246 {
4247 helpmesg = TRUE;
4248#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4249 can_reload = TRUE;
4250#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004251 if (reason[2] == 'n')
4252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004254 mesg2 = _("See \":help W12\" for more info.");
4255 }
4256 else if (reason[1] == 'h')
4257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004259 mesg2 = _("See \":help W11\" for more info.");
4260 }
4261 else if (*reason == 'm')
4262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004264 mesg2 = _("See \":help W16\" for more info.");
4265 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004266 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004268 // Only timestamp changed, store it to avoid a warning
4269 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004270 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004271 buf->b_mtime_read_ns = buf->b_mtime_ns;
4272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275 }
4276
4277 }
4278 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4279 && vim_fexists(buf->b_ffname))
4280 {
4281 retval = 1;
4282 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4283 buf->b_flags |= BF_NEW_W;
4284#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4285 can_reload = TRUE;
4286#endif
4287 }
4288
4289 if (mesg != NULL)
4290 {
4291 path = home_replace_save(buf, buf->b_fname);
4292 if (path != NULL)
4293 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004294 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004296 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004297 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004298#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 // Set warningmsg here, before the unimportant and output-specific
4300 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004301 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4304 if (can_reload)
4305 {
4306 if (*mesg2 != NUL)
4307 {
4308 STRCAT(tbuf, "\n");
4309 STRCAT(tbuf, mesg2);
4310 }
Rob Pilling8196e942022-02-11 15:12:10 +00004311 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4312 (char_u *)tbuf,
4313 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4314 1, NULL, TRUE))
4315 {
4316 case 2:
4317 reload = RELOAD_NORMAL;
4318 break;
4319 case 3:
4320 reload = RELOAD_DETECT;
4321 break;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else
4325#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004326 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4327 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 if (*mesg2 != NUL)
4330 {
4331 STRCAT(tbuf, "; ");
4332 STRCAT(tbuf, mesg2);
4333 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004334 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 retval = 2;
4336 }
4337 else
4338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004342 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004344 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 msg_clr_eos();
4346 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004347 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004350#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004352#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004354 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004356 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 redraw_cmdline = FALSE;
4358 }
4359 }
4360 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362
4363 vim_free(path);
4364 vim_free(tbuf);
4365 }
4366 }
4367
Rob Pilling8196e942022-02-11 15:12:10 +00004368 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004369 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004371 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004372#ifdef FEAT_PERSISTENT_UNDO
4373 if (buf->b_p_udf && buf->b_ffname != NULL)
4374 {
4375 char_u hash[UNDO_HASH_SIZE];
4376 buf_T *save_curbuf = curbuf;
4377
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004379 curbuf = buf;
4380 u_compute_hash(hash);
4381 u_write_undo(NULL, FALSE, buf, hash);
4382 curbuf = save_curbuf;
4383 }
4384#endif
4385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004388 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004389 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4390 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 // restore this in case an autocommand has set it; it would break
4393 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 need_mouse_correct = save_mouse_correct;
4395#endif
4396
4397 return retval;
4398}
4399
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400/*
4401 * Reload a buffer that is already loaded.
4402 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004403 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4404 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 */
4406 void
Rob Pilling8196e942022-02-11 15:12:10 +00004407buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004408{
4409 exarg_T ea;
4410 pos_T old_cursor;
4411 linenr_T old_topline;
4412 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004413 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004414 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004415 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004416 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004417 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004418 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004419
Bram Moolenaare76062c2022-11-28 18:51:43 +00004420 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004421 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004422 if (curbuf != buf)
4423 {
4424 // Failed to find a window for "buf", it is dangerous to continue,
4425 // better bail out.
4426 return;
4427 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004428
Rob Pilling8196e942022-02-11 15:12:10 +00004429 // Unless reload_options is set, we only want to read the text from the
4430 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4431 // Force the fileformat and encoding to be the same.
4432 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004433 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004434 else
4435 prepped = prep_exarg(&ea, buf);
4436
4437 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004438 {
4439 old_cursor = curwin->w_cursor;
4440 old_topline = curwin->w_topline;
4441
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004442 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004443 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // Save all the text, so that the reload can be undone.
4445 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004446 u_sync(FALSE);
4447 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4448 flags |= READ_KEEP_UNDO;
4449 }
4450
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004451 /*
4452 * To behave like when a new file is edited (matters for
4453 * BufReadPost autocommands) we first need to delete the current
4454 * buffer contents. But if reading the file fails we should keep
4455 * the old contents. Can't use memory only, the file might be
4456 * too big. Use a hidden buffer to move the buffer contents to.
4457 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004458 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004459 savebuf = NULL;
4460 else
4461 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004463 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004464 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004465 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004466 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004468 curbuf = savebuf;
4469 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004470 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004471 curbuf = buf;
4472 curwin->w_buffer = buf;
4473 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004474 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004475 || move_lines(buf, savebuf) == FAIL)
4476 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004477 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004478 saved = FAIL;
4479 }
4480 }
4481
4482 if (saved == OK)
4483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4485 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004486 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4487 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004488 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004489 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004490#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004491 if (!aborting())
4492#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004493 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004494 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // Put the text back from the save buffer. First
4497 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004498 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004499 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004500 break;
4501 (void)move_lines(savebuf, buf);
4502 }
4503 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004506 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004507 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004508 if ((flags & READ_KEEP_UNDO) == 0)
4509 {
4510 u_blockfree(buf);
4511 u_clearall(buf);
4512 }
4513 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004516 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004517 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004518 }
4519 }
4520 vim_free(ea.cmd);
4521
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004522 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004523 wipe_buffer(savebuf, FALSE);
4524
4525#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004526 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004527 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004528#endif
4529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 // Restore the topline and cursor position and check it (lines may
4531 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004532 if (old_topline > curbuf->b_ml.ml_line_count)
4533 curwin->w_topline = curbuf->b_ml.ml_line_count;
4534 else
4535 curwin->w_topline = old_topline;
4536 curwin->w_cursor = old_cursor;
4537 check_cursor();
4538 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004539 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004540#ifdef FEAT_FOLDING
4541 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004542 win_T *wp;
4543 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004546 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004547 if (wp->w_buffer == curwin->w_buffer
4548 && !foldmethodIsManual(wp))
4549 foldUpdateAll(wp);
4550 }
4551#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 // If the mode didn't change and 'readonly' was set, keep the old
4553 // value; the user probably used the ":view" command. But don't
4554 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004555 if (orig_mode == curbuf->b_orig_mode)
4556 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004559 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004560 }
4561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004562 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004563 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004564 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004565}
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004568buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
4570 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004571#ifdef ST_MTIM_NSEC
4572 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4573#else
4574 buf->b_mtime_ns = 0;
4575#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004576 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#ifdef HAVE_ST_MODE
4578 buf->b_orig_mode = (int)st->st_mode;
4579#else
4580 buf->b_orig_mode = mch_getperm(fname);
4581#endif
4582}
4583
4584/*
4585 * Adjust the line with missing eol, used for the next write.
4586 * Used for do_filter(), when the input lines for the filter are deleted.
4587 */
4588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004591 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004592 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593}
4594
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004595// Subfuncions for readdirex()
4596#ifdef FEAT_EVAL
4597# ifdef MSWIN
4598 static char_u *
4599getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4600{
4601 stat_T st;
4602 unsigned short st_mode;
4603 DWORD flag = wfd->dwFileAttributes;
4604 WCHAR *wp;
4605
4606 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4607 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4608 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4609 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4610
4611 wp = wcsrchr(wfd->cFileName, L'.');
4612 if (wp != NULL)
4613 {
4614 if (_wcsicmp(wp, L".exe") == 0 ||
4615 _wcsicmp(wp, L".com") == 0 ||
4616 _wcsicmp(wp, L".cmd") == 0 ||
4617 _wcsicmp(wp, L".bat") == 0)
4618 st_mode |= _S_IEXEC;
4619 }
4620
4621 // Copy user bits to group/other.
4622 st_mode |= (st_mode & 0700) >> 3;
4623 st_mode |= (st_mode & 0700) >> 6;
4624
4625 st.st_mode = st_mode;
4626 return getfpermst(&st, perm);
4627}
4628
4629 static char_u *
4630getftypewfd(WIN32_FIND_DATAW *wfd)
4631{
4632 DWORD flag = wfd->dwFileAttributes;
4633 DWORD tag = wfd->dwReserved0;
4634
4635 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4636 {
4637 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4638 return (char_u*)"junction";
4639 else if (tag == IO_REPARSE_TAG_SYMLINK)
4640 {
4641 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4642 return (char_u*)"linkd";
4643 else
4644 return (char_u*)"link";
4645 }
4646 return (char_u*)"reparse"; // unknown reparse point type
4647 }
4648 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4649 return (char_u*)"dir";
4650 else
4651 return (char_u*)"file";
4652}
4653
4654 static dict_T *
4655create_readdirex_item(WIN32_FIND_DATAW *wfd)
4656{
4657 dict_T *item;
4658 char_u *p;
4659 varnumber_T size, time;
4660 char_u permbuf[] = "---------";
4661
4662 item = dict_alloc();
4663 if (item == NULL)
4664 return NULL;
4665 item->dv_refcount++;
4666
4667 p = utf16_to_enc(wfd->cFileName, NULL);
4668 if (p == NULL)
4669 goto theend;
4670 if (dict_add_string(item, "name", p) == FAIL)
4671 {
4672 vim_free(p);
4673 goto theend;
4674 }
4675 vim_free(p);
4676
4677 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4678 if (dict_add_number(item, "size", size) == FAIL)
4679 goto theend;
4680
4681 // Convert FILETIME to unix time.
4682 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4683 wfd->ftLastWriteTime.dwLowDateTime)
4684 - 116444736000000000) / 10000000;
4685 if (dict_add_number(item, "time", time) == FAIL)
4686 goto theend;
4687
4688 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4689 goto theend;
4690 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4691 goto theend;
4692
4693 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4694 goto theend;
4695 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4696 goto theend;
4697
4698 return item;
4699
4700theend:
4701 dict_unref(item);
4702 return NULL;
4703}
4704# else
4705 static dict_T *
4706create_readdirex_item(char_u *path, char_u *name)
4707{
4708 dict_T *item;
4709 char *p;
4710 size_t len;
4711 stat_T st;
4712 int ret, link = FALSE;
4713 varnumber_T size;
4714 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004715 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004716 struct passwd *pw;
4717 struct group *gr;
4718
4719 item = dict_alloc();
4720 if (item == NULL)
4721 return NULL;
4722 item->dv_refcount++;
4723
4724 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4725 p = alloc(len);
4726 if (p == NULL)
4727 goto theend;
4728 vim_snprintf(p, len, "%s/%s", path, name);
4729 ret = mch_lstat(p, &st);
4730 if (ret >= 0 && S_ISLNK(st.st_mode))
4731 {
4732 link = TRUE;
4733 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004734 if (ret < 0)
4735 q = (char_u*)"link";
4736
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004737 }
4738 vim_free(p);
4739
4740 if (dict_add_string(item, "name", name) == FAIL)
4741 goto theend;
4742
4743 if (ret >= 0)
4744 {
4745 size = (varnumber_T)st.st_size;
4746 if (S_ISDIR(st.st_mode))
4747 size = 0;
4748 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004749 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004750 size = -2;
4751 if (dict_add_number(item, "size", size) == FAIL)
4752 goto theend;
4753 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4754 goto theend;
4755
4756 if (link)
4757 {
4758 if (S_ISDIR(st.st_mode))
4759 q = (char_u*)"linkd";
4760 else
4761 q = (char_u*)"link";
4762 }
4763 else
4764 q = getftypest(&st);
4765 if (dict_add_string(item, "type", q) == FAIL)
4766 goto theend;
4767 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4768 goto theend;
4769
4770 pw = getpwuid(st.st_uid);
4771 if (pw == NULL)
4772 q = (char_u*)"";
4773 else
4774 q = (char_u*)pw->pw_name;
4775 if (dict_add_string(item, "user", q) == FAIL)
4776 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004777# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004778 gr = getgrgid(st.st_gid);
4779 if (gr == NULL)
4780 q = (char_u*)"";
4781 else
4782 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004783# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004784 if (dict_add_string(item, "group", q) == FAIL)
4785 goto theend;
4786 }
4787 else
4788 {
4789 if (dict_add_number(item, "size", -1) == FAIL)
4790 goto theend;
4791 if (dict_add_number(item, "time", -1) == FAIL)
4792 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004793 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004794 goto theend;
4795 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4796 goto theend;
4797 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4798 goto theend;
4799 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4800 goto theend;
4801 }
4802 return item;
4803
4804theend:
4805 dict_unref(item);
4806 return NULL;
4807}
4808# endif
4809
4810 static int
4811compare_readdirex_item(const void *p1, const void *p2)
4812{
4813 char_u *name1, *name2;
4814
Bram Moolenaard61efa52022-07-23 09:52:04 +01004815 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4816 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004817 if (readdirex_sort == READDIR_SORT_BYTE)
4818 return STRCMP(name1, name2);
4819 else if (readdirex_sort == READDIR_SORT_IC)
4820 return STRICMP(name1, name2);
4821 else
4822 return STRCOLL(name1, name2);
4823}
4824
4825 static int
4826compare_readdir_item(const void *s1, const void *s2)
4827{
4828 if (readdirex_sort == READDIR_SORT_BYTE)
4829 return STRCMP(*(char **)s1, *(char **)s2);
4830 else if (readdirex_sort == READDIR_SORT_IC)
4831 return STRICMP(*(char **)s1, *(char **)s2);
4832 else
4833 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004834}
4835#endif
4836
Bram Moolenaarda440d22016-01-16 21:27:23 +01004837#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4838/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004839 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004840 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004841 * If "withattr" is TRUE, retrieve the names and their attributes.
4842 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004843 * Return OK for success, FAIL for failure.
4844 */
4845 int
4846readdir_core(
4847 garray_T *gap,
4848 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004849 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004850 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004851 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004852 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004853{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004854 int failed = FALSE;
4855 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004856# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004857 char_u *buf;
4858 int ok;
4859 HANDLE hFind = INVALID_HANDLE_VALUE;
4860 WIN32_FIND_DATAW wfd;
4861 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004862# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004863 DIR *dirp;
4864 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004865# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004866
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004867 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004868
4869# ifdef FEAT_EVAL
4870# define FREE_ITEM(item) do { \
4871 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004872 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004873 else \
4874 vim_free(item); \
4875 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004876
4877 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004878# else
4879# define FREE_ITEM(item) vim_free(item)
4880# endif
4881
4882# ifdef MSWIN
4883 buf = alloc(MAXPATHL);
4884 if (buf == NULL)
4885 return FAIL;
4886 STRNCPY(buf, path, MAXPATHL-5);
4887 p = buf + STRLEN(buf);
4888 MB_PTR_BACK(buf, p);
4889 if (*p == '\\' || *p == '/')
4890 *p = NUL;
4891 STRCAT(p, "\\*");
4892
4893 wn = enc_to_utf16(buf, NULL);
4894 if (wn != NULL)
4895 hFind = FindFirstFileW(wn, &wfd);
4896 ok = (hFind != INVALID_HANDLE_VALUE);
4897 if (!ok)
4898 {
4899 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004900 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004901 }
4902 else
4903 {
4904 while (ok)
4905 {
4906 int ignore;
4907 void *item;
4908 WCHAR *wp;
4909
4910 wp = wfd.cFileName;
4911 ignore = wp[0] == L'.' &&
4912 (wp[1] == NUL ||
4913 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004914 if (ignore)
4915 {
4916 ok = FindNextFileW(hFind, &wfd);
4917 continue;
4918 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004919# ifdef FEAT_EVAL
4920 if (withattr)
4921 item = (void*)create_readdirex_item(&wfd);
4922 else
4923# endif
4924 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4925 if (item == NULL)
4926 {
4927 failed = TRUE;
4928 break;
4929 }
4930
4931 if (!ignore && checkitem != NULL)
4932 {
4933 int r = checkitem(context, item);
4934
4935 if (r < 0)
4936 {
4937 FREE_ITEM(item);
4938 break;
4939 }
4940 if (r == 0)
4941 ignore = TRUE;
4942 }
4943
4944 if (!ignore)
4945 {
4946 if (ga_grow(gap, 1) == OK)
4947 ((void**)gap->ga_data)[gap->ga_len++] = item;
4948 else
4949 {
4950 failed = TRUE;
4951 FREE_ITEM(item);
4952 break;
4953 }
4954 }
4955 else
4956 FREE_ITEM(item);
4957
4958 ok = FindNextFileW(hFind, &wfd);
4959 }
4960 FindClose(hFind);
4961 }
4962
4963 vim_free(buf);
4964 vim_free(wn);
4965# else // MSWIN
4966 dirp = opendir((char *)path);
4967 if (dirp == NULL)
4968 {
4969 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004970 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004971 }
4972 else
4973 {
4974 for (;;)
4975 {
4976 int ignore;
4977 void *item;
4978
4979 dp = readdir(dirp);
4980 if (dp == NULL)
4981 break;
4982 p = (char_u *)dp->d_name;
4983
4984 ignore = p[0] == '.' &&
4985 (p[1] == NUL ||
4986 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004987 if (ignore)
4988 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004989# ifdef FEAT_EVAL
4990 if (withattr)
4991 item = (void*)create_readdirex_item(path, p);
4992 else
4993# endif
4994 item = (void*)vim_strsave(p);
4995 if (item == NULL)
4996 {
4997 failed = TRUE;
4998 break;
4999 }
5000
Bram Moolenaarfe154992022-03-22 20:42:12 +00005001 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005002 {
5003 int r = checkitem(context, item);
5004
5005 if (r < 0)
5006 {
5007 FREE_ITEM(item);
5008 break;
5009 }
5010 if (r == 0)
5011 ignore = TRUE;
5012 }
5013
5014 if (!ignore)
5015 {
5016 if (ga_grow(gap, 1) == OK)
5017 ((void**)gap->ga_data)[gap->ga_len++] = item;
5018 else
5019 {
5020 failed = TRUE;
5021 FREE_ITEM(item);
5022 break;
5023 }
5024 }
5025 else
5026 FREE_ITEM(item);
5027 }
5028
5029 closedir(dirp);
5030 }
5031# endif // MSWIN
5032
5033# undef FREE_ITEM
5034
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005035 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005036 {
5037# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005038 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005039 if (withattr)
5040 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5041 compare_readdirex_item);
5042 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005043 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5044 compare_readdir_item);
5045# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005046 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005047# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005048 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005049
5050 return failed ? FAIL : OK;
5051}
5052
5053/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005054 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005055 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005056 */
5057 int
5058delete_recursive(char_u *name)
5059{
5060 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005061 int i;
5062 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005063 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005064
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005065 // A symbolic link to a directory itself is deleted, not the directory it
5066 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005067 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005068# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005069 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005070# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005071 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005072# endif
5073 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005074 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005075 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005076 if (exp == NULL)
5077 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005078 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005079 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005080 for (i = 0; i < ga.ga_len; ++i)
5081 {
5082 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5083 ((char_u **)ga.ga_data)[i]);
5084 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005085 // Remember the failure but continue deleting any further
5086 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005087 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005088 }
5089 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005090 if (mch_rmdir(exp) != 0)
5091 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005092 }
5093 else
5094 result = -1;
5095 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005096 }
5097 else
5098 result = mch_remove(name) == 0 ? 0 : -1;
5099
5100 return result;
5101}
5102#endif
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005105static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005107# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5108/*
5109 * Open temporary directory and take file lock to prevent
5110 * to be auto-cleaned.
5111 */
5112 static void
5113vim_opentempdir(void)
5114{
5115 DIR *dp = NULL;
5116
5117 if (vim_tempdir_dp != NULL)
5118 return;
5119
5120 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005121 if (dp == NULL)
5122 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005123
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005124 vim_tempdir_dp = dp;
5125 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005126}
5127
5128/*
5129 * Close temporary directory - it automatically release file lock.
5130 */
5131 static void
5132vim_closetempdir(void)
5133{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005134 if (vim_tempdir_dp == NULL)
5135 return;
5136
5137 closedir(vim_tempdir_dp);
5138 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005139}
5140# endif
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142/*
5143 * Delete the temp directory and all files it contains.
5144 */
5145 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005146vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005148 if (vim_tempdir == NULL)
5149 return;
5150
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005151# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005152 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005153# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005154 // remove the trailing path separator
5155 gettail(vim_tempdir)[-1] = NUL;
5156 delete_recursive(vim_tempdir);
5157 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005161 * Directory "tempdir" was created. Expand this name to a full path and put
5162 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5163 * "tempdir" must be no longer than MAXPATHL.
5164 */
5165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005166vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005167{
5168 char_u *buf;
5169
Bram Moolenaar964b3742019-05-24 18:54:09 +02005170 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005171 if (buf == NULL)
5172 return;
5173
5174 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5175 STRCPY(buf, tempdir);
5176 add_pathsep(buf);
5177 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005178# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005179 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005180# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005181 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005182}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005183#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005184
5185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 * vim_tempname(): Return a unique name that can be used for a temp file.
5187 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005188 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5189 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 *
5191 * The returned pointer is to allocated memory.
5192 * The returned pointer is NULL if no valid name was found.
5193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005195vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005196 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005197 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198{
5199#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005201#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005202 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203#else
5204 char_u itmp[TEMPNAMELEN];
5205#endif
5206
5207#ifdef TEMPDIRNAMES
5208 static char *(tempdirs[]) = {TEMPDIRNAMES};
5209 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005211 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212# endif
5213
5214 /*
5215 * This will create a directory for private use by this instance of Vim.
5216 * This is done once, and the same directory is used for all temp files.
5217 * This method avoids security problems because of symlink attacks et al.
5218 * It's also a bit faster, because we only need to check for an existing
5219 * file when creating the directory and not for each temp file.
5220 */
5221 if (vim_tempdir == NULL)
5222 {
5223 /*
5224 * Try the entries in TEMPDIRNAMES to create the temp directory.
5225 */
K.Takataeeec2542021-06-02 13:28:16 +02005226 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005228# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005229 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005230 long nr;
5231 long off;
5232# endif
5233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005234 // Expand $TMP, leave room for "/v1100000/999999999".
5235 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005237 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005240 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
Bram Moolenaareaf03392009-11-17 11:08:52 +00005242# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005243 {
5244# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 // Make sure the umask doesn't remove the executable bit.
5246 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005247 mode_t umask_save = umask(077);
5248# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005250 STRCAT(itmp, "vXXXXXX");
5251 if (mkdtemp((char *)itmp) != NULL)
5252 vim_settempdir(itmp);
5253# if defined(UNIX) || defined(VMS)
5254 (void)umask(umask_save);
5255# endif
5256 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005257# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 // Get an arbitrary number of up to 6 digits. When it's
5259 // unlikely that it already exists it will be faster,
5260 // otherwise it doesn't matter. The use of mkdir() avoids any
5261 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005263 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 // Try up to 10000 different values until we find a name that
5266 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 for (off = 0; off < 10000L; ++off)
5268 {
5269 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005270# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
Bram Moolenaareaf03392009-11-17 11:08:52 +00005274 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5275# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005276 // If mkdir() does not set errno to EEXIST, check for
5277 // existing file here. There is a race condition then,
5278 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 if (mch_stat((char *)itmp, &st) >= 0)
5280 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005281# endif
5282# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005283 // Make sure the umask doesn't remove the executable bit.
5284 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005288# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 if (r == 0)
5292 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005293 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 break;
5295 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005296# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005297 // If the mkdir() didn't fail because the file/dir exists,
5298 // we probably can't create any dir here, try another
5299 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 break;
5303 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (vim_tempdir != NULL)
5306 break;
5307 }
5308 }
5309 }
5310
5311 if (vim_tempdir != NULL)
5312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005313 // There is no need to check if the file exists, because we own the
5314 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5316 return vim_strsave(itmp);
5317 }
5318
5319 return NULL;
5320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
Bram Moolenaar4f974752019-02-17 17:44:42 +01005323# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005324 WCHAR wszTempFile[_MAX_PATH + 1];
5325 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005326 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 char_u *retval;
5328 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005329 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005330 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005332 wcscpy(itmp, L"");
5333 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005334 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005335 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005336 wszTempFile[1] = L'\\';
5337 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005338 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005339 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005340
5341 // randomize the name to avoid collisions
5342 i = mch_get_pid() + extra_char;
5343 buf4[1] = chartab[i % 36];
5344 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005345 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005347 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005348 // GetTempFileName() will create the file, we don't want that
5349 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005351 // Backslashes in a temp file name cause problems when filtering with
5352 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005353 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005354 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005355 shname = gettail(p_sh);
5356 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5357 || strstr((char *)shname, "pwsh") != NULL ))
5358 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 for (p = retval; *p; ++p)
5360 if (*p == '\\')
5361 *p = '/';
5362 return retval;
5363
Bram Moolenaar4f974752019-02-17 17:44:42 +01005364# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005367 char_u *p;
5368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005370 p = tmpnam((char *)itmp);
5371 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 return NULL;
5373# else
5374 char_u *p;
5375
5376# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 // mktemp() is not working on VMS. It seems to be
5378 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 sprintf((char *)itmp, "VIM%c", extra_char);
5380 p = (char_u *)tempnam("tmp:", (char *)itmp);
5381 if (p != NULL)
5382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 // VMS will use '.LIS' if we don't explicitly specify an extension,
5384 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 STRCPY(itmp, p);
5386 STRCAT(itmp, ".txt");
5387 free(p);
5388 }
5389 else
5390 return NULL;
5391# else
5392 STRCPY(itmp, TEMPNAME);
5393 if ((p = vim_strchr(itmp, '?')) != NULL)
5394 *p = extra_char;
5395 if (mktemp((char *)itmp) == NULL)
5396 return NULL;
5397# endif
5398# endif
5399
5400 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005401# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403}
5404
5405#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5406/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005407 * Convert all backslashes in fname to forward slashes in-place, unless when
5408 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 */
5410 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 char_u *p;
5414
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005415 if (path_with_url(fname))
5416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005419 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005421 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 *p = '/';
5423}
5424#endif
5425
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005426/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005427 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5428 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5429 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * Used for autocommands and 'wildignore'.
5431 * Returns TRUE if there is a match, FALSE otherwise.
5432 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005433 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 char_u *pattern, // pattern to match with
5436 regprog_T **prog, // pre-compiled regprog or NULL
5437 char_u *fname, // full path of file name
5438 char_u *sfname, // short file name or NULL
5439 char_u *tail, // tail of path
5440 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 regmatch_T regmatch;
5443 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005445 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005446 if (prog != NULL)
5447 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005449 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 /*
5452 * Try for a match with the pattern with:
5453 * 1. the full file name, when the pattern has a '/'.
5454 * 2. the short file name, when the pattern has a '/'.
5455 * 3. the tail of the file name, when the pattern has no '/'.
5456 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005457 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 && ((allow_dirs
5459 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5460 || (sfname != NULL
5461 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005462 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 result = TRUE;
5464
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005465 if (prog != NULL)
5466 *prog = regmatch.regprog;
5467 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005468 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 return result;
5470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472/*
5473 * Return TRUE if a file matches with a pattern in "list".
5474 * "list" is a comma-separated list of patterns, like 'wildignore'.
5475 * "sfname" is the short file name or NULL, "ffname" the long file name.
5476 */
5477 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005478match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 char_u buf[100];
5481 char_u *tail;
5482 char_u *regpat;
5483 char allow_dirs;
5484 int match;
5485 char_u *p;
5486
5487 tail = gettail(sfname);
5488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005489 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 p = list;
5491 while (*p)
5492 {
5493 copy_option_part(&p, buf, 100, ",");
5494 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5495 if (regpat == NULL)
5496 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005497 match = match_file_pat(regpat, NULL, ffname, sfname,
5498 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 vim_free(regpat);
5500 if (match)
5501 return TRUE;
5502 }
5503 return FALSE;
5504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506/*
5507 * Convert the given pattern "pat" which has shell style wildcards in it, into
5508 * a regular expression, and return the result in allocated memory. If there
5509 * is a directory path separator to be matched, then TRUE is put in
5510 * allow_dirs, otherwise FALSE is put there -- webb.
5511 * Handle backslashes before special characters, like "\*" and "\ ".
5512 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * Returns NULL when out of memory.
5514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516file_pat_to_reg_pat(
5517 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 char_u *pat_end, // first char after pattern or NULL
5519 char *allow_dirs, // Result passed back out in here
5520 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 char_u *endp;
5524 char_u *reg_pat;
5525 char_u *p;
5526 int i;
5527 int nested = 0;
5528 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 if (allow_dirs != NULL)
5531 *allow_dirs = FALSE;
5532 if (pat_end == NULL)
5533 pat_end = pat + STRLEN(pat);
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 for (p = pat; p < pat_end; p++)
5536 {
5537 switch (*p)
5538 {
5539 case '*':
5540 case '.':
5541 case ',':
5542 case '{':
5543 case '}':
5544 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005545 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 break;
5547#ifdef BACKSLASH_IN_FILENAME
5548 case '\\':
5549 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005550 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 break;
5552#endif
5553 default:
5554 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005555 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
5557 ++p;
5558 ++size;
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 break;
5561 }
5562 }
5563 reg_pat = alloc(size + 1);
5564 if (reg_pat == NULL)
5565 return NULL;
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (pat[0] == '*')
5570 while (pat[0] == '*' && pat < pat_end - 1)
5571 pat++;
5572 else
5573 reg_pat[i++] = '^';
5574 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005575 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 while (endp - pat > 0 && *endp == '*')
5578 endp--;
5579 add_dollar = FALSE;
5580 }
5581 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5582 {
5583 switch (*p)
5584 {
5585 case '*':
5586 reg_pat[i++] = '.';
5587 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005589 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 break;
5591 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 case '~':
5593 reg_pat[i++] = '\\';
5594 reg_pat[i++] = *p;
5595 break;
5596 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 reg_pat[i++] = '.';
5598 break;
5599 case '\\':
5600 if (p[1] == NUL)
5601 break;
5602#ifdef BACKSLASH_IN_FILENAME
5603 if (!no_bslash)
5604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 // translate:
5606 // "\x" to "\\x" e.g., "dir\file"
5607 // "\*" to "\\.*" e.g., "dir\*.c"
5608 // "\?" to "\\." e.g., "dir\??.c"
5609 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5611 && p[1] != '+')
5612 {
5613 reg_pat[i++] = '[';
5614 reg_pat[i++] = '\\';
5615 reg_pat[i++] = '/';
5616 reg_pat[i++] = ']';
5617 if (allow_dirs != NULL)
5618 *allow_dirs = TRUE;
5619 break;
5620 }
5621 }
5622#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005623 // Undo escaping from ExpandEscape():
5624 // foo\?bar -> foo?bar
5625 // foo\%bar -> foo%bar
5626 // foo\,bar -> foo,bar
5627 // foo\ bar -> foo bar
5628 // Don't unescape \, * and others that are also special in a
5629 // regexp.
5630 // An escaped { must be unescaped since we use magic not
5631 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 if (*++p == '?'
5633#ifdef BACKSLASH_IN_FILENAME
5634 && no_bslash
5635#endif
5636 )
5637 reg_pat[i++] = '?';
5638 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005639 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005640 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005641 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005642 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5643 {
5644 reg_pat[i++] = '\\';
5645 reg_pat[i++] = '{';
5646 p += 2;
5647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 else
5649 {
5650 if (allow_dirs != NULL && vim_ispathsep(*p)
5651#ifdef BACKSLASH_IN_FILENAME
5652 && (!no_bslash || *p != '\\')
5653#endif
5654 )
5655 *allow_dirs = TRUE;
5656 reg_pat[i++] = '\\';
5657 reg_pat[i++] = *p;
5658 }
5659 break;
5660#ifdef BACKSLASH_IN_FILENAME
5661 case '/':
5662 reg_pat[i++] = '[';
5663 reg_pat[i++] = '\\';
5664 reg_pat[i++] = '/';
5665 reg_pat[i++] = ']';
5666 if (allow_dirs != NULL)
5667 *allow_dirs = TRUE;
5668 break;
5669#endif
5670 case '{':
5671 reg_pat[i++] = '\\';
5672 reg_pat[i++] = '(';
5673 nested++;
5674 break;
5675 case '}':
5676 reg_pat[i++] = '\\';
5677 reg_pat[i++] = ')';
5678 --nested;
5679 break;
5680 case ',':
5681 if (nested)
5682 {
5683 reg_pat[i++] = '\\';
5684 reg_pat[i++] = '|';
5685 }
5686 else
5687 reg_pat[i++] = ',';
5688 break;
5689 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005690 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005692 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 *allow_dirs = TRUE;
5694 reg_pat[i++] = *p;
5695 break;
5696 }
5697 }
5698 if (add_dollar)
5699 reg_pat[i++] = '$';
5700 reg_pat[i] = NUL;
5701 if (nested != 0)
5702 {
5703 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005704 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005706 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005707 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 return reg_pat;
5710}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005711
5712#if defined(EINTR) || defined(PROTO)
5713/*
5714 * Version of read() that retries when interrupted by EINTR (possibly
5715 * by a SIGWINCH).
5716 */
5717 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005719{
5720 long ret;
5721
5722 for (;;)
5723 {
5724 ret = vim_read(fd, buf, bufsize);
5725 if (ret >= 0 || errno != EINTR)
5726 break;
5727 }
5728 return ret;
5729}
5730
5731/*
5732 * Version of write() that retries when interrupted by EINTR (possibly
5733 * by a SIGWINCH).
5734 */
5735 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005737{
5738 long ret = 0;
5739 long wlen;
5740
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005741 // Repeat the write() so long it didn't fail, other than being interrupted
5742 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005743 while (ret < (long)bufsize)
5744 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005745 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005746 if (wlen < 0)
5747 {
5748 if (errno != EINTR)
5749 break;
5750 }
5751 else
5752 ret += wlen;
5753 }
5754 return ret;
5755}
5756#endif