blob: 8b65baf62a5a101ea6f85d3dec00414e531cfb89 [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 Moolenaar438d0c52023-06-17 15:00:27 +0100221 int eof = FALSE;
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 */
1232 if (read_buf_lnum > from)
1233 size = 0;
1234 else
1235 {
1236 int n, ni;
1237 long tlen;
1238
1239 tlen = 0;
1240 for (;;)
1241 {
1242 p = ml_get(read_buf_lnum) + read_buf_col;
1243 n = (int)STRLEN(p);
1244 if ((int)tlen + n + 1 > size)
1245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001246 // Filled up to "size", append partial line.
1247 // Change NL to NUL to reverse the effect done
1248 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001249 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 for (ni = 0; ni < n; ++ni)
1251 {
1252 if (p[ni] == NL)
1253 ptr[tlen++] = NUL;
1254 else
1255 ptr[tlen++] = p[ni];
1256 }
1257 read_buf_col += n;
1258 break;
1259 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001260
1261 // Append whole line and new-line. Change NL
1262 // to NUL to reverse the effect done below.
1263 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001265 if (p[ni] == NL)
1266 ptr[tlen++] = NUL;
1267 else
1268 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001270 ptr[tlen++] = NL;
1271 read_buf_col = 0;
1272 if (++read_buf_lnum > from)
1273 {
1274 // When the last line didn't have an
1275 // end-of-line don't add it now either.
1276 if (!curbuf->b_p_eol)
1277 --tlen;
1278 size = tlen;
1279 eof = TRUE;
1280 break;
1281 }
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 }
1286 else
1287 {
1288 /*
1289 * Read bytes from the file.
1290 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001291# ifdef FEAT_SODIUM
1292 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001293 //
1294 // Sodium encryption requires a fixed block size to
1295 // successfully decrypt. However, unfortunately the file
1296 // header size changes between xchacha20 and xchacha20v2 by
1297 // 'add_len' bytes.
1298 // So we will now read the maximum header size + encryption
1299 // metadata, but after determining to read an xchacha20
1300 // encrypted file, we have to rewind the file descriptor by
1301 // 'add_len' bytes in the second round.
1302 //
1303 // Be careful with changing it, it needs to stay the same
1304 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001305 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001306 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001307 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001308 size = WRITEBUFSIZE + crypt_get_max_header_len()
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001309 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1310 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001311 may_need_lseek = TRUE;
1312 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001313
Christian Brabandtaae58342023-04-23 17:50:22 +01001314 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1315 && crypt_method_is_sodium(
1316 curbuf->b_cryptstate->method_nr)))
1317 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001318 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001319 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1320 // description above)
1321 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1322 && !eof && may_need_lseek)
1323 {
1324 lseek(fd, crypt_get_header_len(
1325 curbuf->b_cryptstate->method_nr)
1326 - crypt_get_max_header_len(), SEEK_CUR);
1327 may_need_lseek = FALSE;
1328 }
1329 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001330# endif
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001331 long read_size = size;
1332 size = read_eintr(fd, ptr, read_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001333 filesize_count += size;
1334 // hit end of file
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001335 eof = (size < read_size || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001338#ifdef FEAT_CRYPT
1339 /*
1340 * At start of file: Check for magic number of encryption.
1341 */
1342 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001343 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001344 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1345 &filesize, newfile, sfname,
1346 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001347# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001348 if (curbuf->b_cryptstate != NULL
1349 && !crypt_works_inplace(curbuf->b_cryptstate))
1350 // reading undo file requires crypt_decode_inplace()
1351 read_undo_file = FALSE;
1352# endif
1353 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001354 /*
1355 * Decrypt the read bytes. This is done before checking for
1356 * EOF because the crypt layer may be buffering.
1357 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001358 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1359 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001360 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001361# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001362 if (crypt_works_inplace(curbuf->b_cryptstate))
1363 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001364# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001365 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1366 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001367# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001368 }
1369 else
1370 {
1371 char_u *newptr = NULL;
1372 int decrypted_size;
1373
1374 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001375 curbuf->b_cryptstate, ptr, size,
1376 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001378 if (decrypted_size < 0)
1379 {
1380 // error message already given
1381 error = TRUE;
1382 vim_free(newptr);
1383 break;
1384 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001385 // If the crypt layer is buffering, not producing
1386 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001387 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001388 continue;
1389
1390 if (linerest == 0)
1391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 // Simple case: reuse returned buffer (may be
1393 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001394 new_buffer = newptr;
1395 }
1396 else
1397 {
1398 long_u new_size;
1399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001400 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001401 new_size = (long_u)(decrypted_size + linerest + 1);
1402 new_buffer = lalloc(new_size, FALSE);
1403 if (new_buffer == NULL)
1404 {
1405 do_outofmem_msg(new_size);
1406 error = TRUE;
1407 break;
1408 }
1409
1410 mch_memmove(new_buffer, buffer, linerest);
1411 if (newptr != NULL)
1412 mch_memmove(new_buffer + linerest, newptr,
1413 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001414 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001415 }
1416
1417 if (new_buffer != NULL)
1418 {
1419 vim_free(buffer);
1420 buffer = new_buffer;
1421 new_buffer = NULL;
1422 line_start = buffer;
1423 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001424 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001425 }
1426 size = decrypted_size;
1427 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001428# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001429 }
1430#endif
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (size <= 0)
1433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001437 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001438 /*
1439 * Reached end-of-file but some trailing bytes could
1440 * not be converted. Truncated file?
1441 */
1442
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001443 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001444 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001445#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001446 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001447#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001448 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001449 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001450 if (can_retry)
1451 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001452 if (conv_error == 0)
1453 conv_error = curbuf->b_ml.ml_line_count
1454 - linecnt + 1;
1455 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001456 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001457 else if (illegal_byte == 0)
1458 illegal_byte = curbuf->b_ml.ml_line_count
1459 - linecnt + 1;
1460 if (bad_char_behavior == BAD_DROP)
1461 {
1462 *(ptr - conv_restlen) = NUL;
1463 conv_restlen = 0;
1464 }
1465 else
1466 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // Replace the trailing bytes with the replacement
1468 // character if we were converting; if we weren't,
1469 // leave the UTF8 checking code to do it, as it
1470 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001471 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001472#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001473 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001474#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001475 ))
1476 {
1477 while (conv_restlen > 0)
1478 {
1479 *(--ptr) = bad_char_behavior;
1480 --conv_restlen;
1481 }
1482 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001483 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001484#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001485 if (iconv_fd != (iconv_t)-1)
1486 {
1487 iconv_close(iconv_fd);
1488 iconv_fd = (iconv_t)-1;
1489 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001490#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001491 }
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 skip_read = FALSE;
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /*
1498 * At start of file (or after crypt magic number): Check for BOM.
1499 * Also check for a BOM for other Unicode encodings, but not after
1500 * converting with 'charconvert' or when a BOM has already been
1501 * found.
1502 */
1503 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001504#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001505 || (cryptkey != NULL
1506 && filesize == crypt_get_header_len(
1507 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 )
1510 && (fio_flags == FIO_UCSBOM
1511 || (!curbuf->b_p_bomb
1512 && tmpname == NULL
1513 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1514 {
1515 char_u *ccname;
1516 int blen;
1517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (size < 2 || curbuf->b_p_bin)
1520 ccname = NULL;
1521 else
1522 ccname = check_for_bom(ptr, size, &blen,
1523 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1524 if (ccname != NULL)
1525 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 filesize += blen;
1528 size -= blen;
1529 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001530 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001533 curbuf->b_start_bomb = TRUE;
1534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536
1537 if (fio_flags == FIO_UCSBOM)
1538 {
1539 if (ccname == NULL)
1540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001541 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 advance_fenc = TRUE;
1543 }
1544 else
1545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (fenc_alloced)
1548 vim_free(fenc);
1549 fenc = ccname;
1550 fenc_alloced = FALSE;
1551 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 skip_read = TRUE;
1554 goto retry;
1555 }
1556 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001558 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001559 ptr -= conv_restlen;
1560 size += conv_restlen;
1561 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 /*
1563 * Break here for a read error or end-of-file.
1564 */
1565 if (size <= 0)
1566 break;
1567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar13505972019-01-24 15:04:48 +01001569#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (iconv_fd != (iconv_t)-1)
1571 {
1572 /*
1573 * Attempt conversion of the read bytes to 'encoding' using
1574 * iconv().
1575 */
1576 const char *fromp;
1577 char *top;
1578 size_t from_size;
1579 size_t to_size;
1580
1581 fromp = (char *)ptr;
1582 from_size = size;
1583 ptr += size;
1584 top = (char *)ptr;
1585 to_size = real_size - size;
1586
1587 /*
1588 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001589 * another conversion. Except for when there is no
1590 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001592 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1593 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1595 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001596 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001597 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001598 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001599 if (conv_error == 0)
1600 conv_error = readfile_linenr(linecnt,
1601 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001604 ++fromp;
1605 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001606 if (bad_char_behavior == BAD_KEEP)
1607 {
1608 *top++ = *(fromp - 1);
1609 --to_size;
1610 }
1611 else if (bad_char_behavior != BAD_DROP)
1612 {
1613 *top++ = bad_char_behavior;
1614 --to_size;
1615 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 if (from_size > 0)
1619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001620 // Some remaining characters, keep them for the next
1621 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1623 conv_restlen = (int)from_size;
1624 }
1625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001626 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 line_start = ptr - linerest;
1628 mch_memmove(line_start, buffer, (size_t)linerest);
1629 size = (long)((char_u *)top - ptr);
1630 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar4f974752019-02-17 17:44:42 +01001633#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (fio_flags & FIO_CODEPAGE)
1635 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 WCHAR ucs2buf[3];
1638 int ucs2len;
1639 int codepage = FIO_GET_CP(fio_flags);
1640 int bytelen;
1641 int found_bad;
1642 char replstr[2];
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /*
1645 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 * a codepage, using standard MS-Windows functions. This
1647 * requires two steps:
1648 * 1. convert from 'fileencoding' to ucs-2
1649 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001651 * Because there may be illegal bytes AND an incomplete byte
1652 * sequence at the end, we may have to do the conversion one
1653 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001657 if (bad_char_behavior > 0)
1658 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001660 replstr[0] = '?';
1661 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001664 * Move the bytes to the end of the buffer, so that we have
1665 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001667 src = ptr + real_size - size;
1668 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001670 /*
1671 * Do the conversion.
1672 */
1673 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001674 while (size > 0)
1675 {
1676 found_bad = FALSE;
1677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001678# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001679 if (codepage == CP_UTF8)
1680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // Handle CP_UTF8 input ourselves to be able to handle
1682 // trailing bytes properly.
1683 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001684 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001685 if (bytelen > size)
1686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // Only got some bytes of a character. Normally
1688 // it's put in "conv_rest", but if it's too long
1689 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001690 if (bytelen <= CONV_RESTLEN)
1691 break;
1692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001693 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001694 bytelen = size;
1695 found_bad = TRUE;
1696 }
1697 else
1698 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001699 int u8c = utf_ptr2char(src);
1700
Bram Moolenaar86e01082005-12-29 22:45:34 +00001701 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001702 found_bad = TRUE;
1703 ucs2buf[0] = u8c;
1704 ucs2len = 1;
1705 }
1706 }
1707 else
1708# endif
1709 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001710 // We don't know how long the byte sequence is, try
1711 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001712 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1713 ++bytelen)
1714 {
1715 ucs2len = MultiByteToWideChar(codepage,
1716 MB_ERR_INVALID_CHARS,
1717 (LPCSTR)src, bytelen,
1718 ucs2buf, 3);
1719 if (ucs2len > 0)
1720 break;
1721 }
1722 if (ucs2len == 0)
1723 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001724 // If we have only one byte then it's probably an
1725 // incomplete byte sequence. Otherwise discard
1726 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001727 if (size == 1)
1728 break;
1729 found_bad = TRUE;
1730 bytelen = 1;
1731 }
1732 }
1733
1734 if (!found_bad)
1735 {
1736 int i;
1737
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001738 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001739 if (enc_utf8)
1740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001742 for (i = 0; i < ucs2len; ++i)
1743 dst += utf_char2bytes(ucs2buf[i], dst);
1744 }
1745 else
1746 {
1747 BOOL bad = FALSE;
1748 int dstlen;
1749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001750 // From UCS-2 to "enc_codepage". If the
1751 // conversion uses the default character "?",
1752 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001753 dstlen = WideCharToMultiByte(enc_codepage, 0,
1754 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001755 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001756 replstr, &bad);
1757 if (bad)
1758 found_bad = TRUE;
1759 else
1760 dst += dstlen;
1761 }
1762 }
1763
1764 if (found_bad)
1765 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001767 if (can_retry)
1768 goto rewind_retry;
1769 if (conv_error == 0)
1770 conv_error = readfile_linenr(linecnt, ptr, dst);
1771 if (bad_char_behavior != BAD_DROP)
1772 {
1773 if (bad_char_behavior == BAD_KEEP)
1774 {
1775 mch_memmove(dst, src, bytelen);
1776 dst += bytelen;
1777 }
1778 else
1779 *dst++ = bad_char_behavior;
1780 }
1781 }
1782
1783 src += bytelen;
1784 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001786
1787 if (size > 0)
1788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001789 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001790 mch_memmove(conv_rest, src, size);
1791 conv_restlen = size;
1792 }
1793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001795 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001798#endif
1799#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (fio_flags & FIO_MACROMAN)
1801 {
1802 /*
1803 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001804 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001806 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (fio_flags != 0)
1812 {
1813 int u8c;
1814 char_u *dest;
1815 char_u *tail = NULL;
1816
1817 /*
1818 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1819 * "enc_utf8" not set: Convert Unicode to Latin1.
1820 * Go from end to start through the buffer, because the number
1821 * of bytes may increase.
1822 * "dest" points to after where the UTF-8 bytes go, "p" points
1823 * to after the next character to convert.
1824 */
1825 dest = ptr + real_size;
1826 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1827 {
1828 p = ptr + size;
1829 if (fio_flags == FIO_UTF8)
1830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001831 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 tail = ptr + size - 1;
1833 while (tail > ptr && (*tail & 0xc0) == 0x80)
1834 --tail;
1835 if (tail + utf_byte2len(*tail) <= ptr + size)
1836 tail = NULL;
1837 else
1838 p = tail;
1839 }
1840 }
1841 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001843 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 p = ptr + (size & ~1);
1845 if (size & 1)
1846 tail = p;
1847 if ((fio_flags & FIO_UTF16) && p > ptr)
1848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001849 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (fio_flags & FIO_ENDIAN_L)
1851 {
1852 u8c = (*--p << 8);
1853 u8c += *--p;
1854 }
1855 else
1856 {
1857 u8c = *--p;
1858 u8c += (*--p << 8);
1859 }
1860 if (u8c >= 0xd800 && u8c <= 0xdbff)
1861 tail = p;
1862 else
1863 p += 2;
1864 }
1865 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001866 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 p = ptr + (size & ~3);
1870 if (size & 3)
1871 tail = p;
1872 }
1873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001874 // If there is a trailing incomplete sequence move it to
1875 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (tail != NULL)
1877 {
1878 conv_restlen = (int)((ptr + size) - tail);
1879 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1880 size -= conv_restlen;
1881 }
1882
1883
1884 while (p > ptr)
1885 {
1886 if (fio_flags & FIO_LATIN1)
1887 u8c = *--p;
1888 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1889 {
1890 if (fio_flags & FIO_ENDIAN_L)
1891 {
1892 u8c = (*--p << 8);
1893 u8c += *--p;
1894 }
1895 else
1896 {
1897 u8c = *--p;
1898 u8c += (*--p << 8);
1899 }
1900 if ((fio_flags & FIO_UTF16)
1901 && u8c >= 0xdc00 && u8c <= 0xdfff)
1902 {
1903 int u16c;
1904
1905 if (p == ptr)
1906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001907 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (can_retry)
1909 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001910 if (conv_error == 0)
1911 conv_error = readfile_linenr(linecnt,
1912 ptr, p);
1913 if (bad_char_behavior == BAD_DROP)
1914 continue;
1915 if (bad_char_behavior != BAD_KEEP)
1916 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001919 // found second word of double-word, get the first
1920 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if (fio_flags & FIO_ENDIAN_L)
1922 {
1923 u16c = (*--p << 8);
1924 u16c += *--p;
1925 }
1926 else
1927 {
1928 u16c = *--p;
1929 u16c += (*--p << 8);
1930 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001931 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1932 + (u8c & 0x3ff);
1933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001934 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (u16c < 0xd800 || u16c > 0xdbff)
1936 {
1937 if (can_retry)
1938 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001939 if (conv_error == 0)
1940 conv_error = readfile_linenr(linecnt,
1941 ptr, p);
1942 if (bad_char_behavior == BAD_DROP)
1943 continue;
1944 if (bad_char_behavior != BAD_KEEP)
1945 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 }
1949 else if (fio_flags & FIO_UCS4)
1950 {
1951 if (fio_flags & FIO_ENDIAN_L)
1952 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001953 u8c = (unsigned)*--p << 24;
1954 u8c += (unsigned)*--p << 16;
1955 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 u8c += *--p;
1957 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001961 u8c += (unsigned)*--p << 8;
1962 u8c += (unsigned)*--p << 16;
1963 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 if (*--p < 0x80)
1969 u8c = *p;
1970 else
1971 {
1972 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001973 p -= len;
1974 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (len == 0)
1976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // Not a valid UTF-8 character, retry with
1978 // another fenc when possible, otherwise just
1979 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (can_retry)
1981 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001982 if (conv_error == 0)
1983 conv_error = readfile_linenr(linecnt,
1984 ptr, p);
1985 if (bad_char_behavior == BAD_DROP)
1986 continue;
1987 if (bad_char_behavior != BAD_KEEP)
1988 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 dest -= utf_char2len(u8c);
1995 (void)utf_char2bytes(u8c, dest);
1996 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 --dest;
2000 if (u8c >= 0x100)
2001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // character doesn't fit in latin1, retry with
2003 // another fenc when possible, otherwise just
2004 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002005 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002007 if (conv_error == 0)
2008 conv_error = readfile_linenr(linecnt, ptr, p);
2009 if (bad_char_behavior == BAD_DROP)
2010 ++dest;
2011 else if (bad_char_behavior == BAD_KEEP)
2012 *dest = u8c;
2013 else if (eap != NULL && eap->bad_char != 0)
2014 *dest = bad_char_behavior;
2015 else
2016 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 else
2019 *dest = u8c;
2020 }
2021 }
2022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002023 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 line_start = dest - linerest;
2025 mch_memmove(line_start, buffer, (size_t)linerest);
2026 size = (long)((ptr + real_size) - dest);
2027 ptr = dest;
2028 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002029 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002031 int incomplete_tail = FALSE;
2032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002034 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002036 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002037 int l;
2038
2039 if (todo <= 0)
2040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (*p >= 0x80)
2042 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // A length of 1 means it's an illegal byte. Accept
2044 // an incomplete character at the end though, the next
2045 // read() will get the next bytes, we'll check it
2046 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002047 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002048 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002050 // Avoid retrying with a different encoding when
2051 // a truncated file is more likely, or attempting
2052 // to read the rest of an incomplete sequence when
2053 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002054 if (p > ptr || filesize > 0)
2055 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 // Incomplete byte sequence, move it to conv_rest[]
2057 // and try to read the rest of it, unless we've
2058 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002059 if (p > ptr)
2060 {
2061 conv_restlen = todo;
2062 mch_memmove(conv_rest, p, conv_restlen);
2063 size -= conv_restlen;
2064 break;
2065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002067 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // Illegal byte. If we can try another encoding
2070 // do that, unless at EOF where a truncated
2071 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002072 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002073 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002074#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002075 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002076 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2077 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002078#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002079 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002080 if (conv_error == 0 && illegal_byte == 0)
2081 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002083 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002084 if (bad_char_behavior == BAD_DROP)
2085 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002086 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002087 --p;
2088 --size;
2089 }
2090 else if (bad_char_behavior != BAD_KEEP)
2091 *p = bad_char_behavior;
2092 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002093 else
2094 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002097 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002102#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002103 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002104 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002105 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002107#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002109 advance_fenc = TRUE;
2110 file_rewind = TRUE;
2111 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 filesize += size;
2117
2118 /*
2119 * when reading the first part of a file: guess EOL type
2120 */
2121 if (fileformat == EOL_UNKNOWN)
2122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002123 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (try_dos || try_unix)
2125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002127 if (try_mac)
2128 try_mac = 1;
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 for (p = ptr; p < ptr + size; ++p)
2131 {
2132 if (*p == NL)
2133 {
2134 if (!try_unix
2135 || (try_dos && p > ptr && p[-1] == CAR))
2136 fileformat = EOL_DOS;
2137 else
2138 fileformat = EOL_UNIX;
2139 break;
2140 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002141 else if (*p == CAR && try_mac)
2142 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002145 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (fileformat == EOL_UNIX && try_mac)
2147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002148 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 try_mac = 1;
2150 try_unix = 1;
2151 for (; p >= ptr && *p != CAR; p--)
2152 ;
2153 if (p >= ptr)
2154 {
2155 for (p = ptr; p < ptr + size; ++p)
2156 {
2157 if (*p == NL)
2158 try_unix++;
2159 else if (*p == CAR)
2160 try_mac++;
2161 }
2162 if (try_mac > try_unix)
2163 fileformat = EOL_MAC;
2164 }
2165 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002166 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002167 // Looking for CR but found no end-of-line markers at
2168 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002169 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002172 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (fileformat == EOL_UNKNOWN && try_mac)
2174 fileformat = EOL_MAC;
2175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002176 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (fileformat == EOL_UNKNOWN)
2178 fileformat = default_fileformat();
2179
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002180 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 set_fileformat(fileformat, OPT_LOCAL);
2183 }
2184 }
2185
2186 /*
2187 * This loop is executed once for every character read.
2188 * Keep it fast!
2189 */
2190 if (fileformat == EOL_MAC)
2191 {
2192 --ptr;
2193 while (++ptr, --size >= 0)
2194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if ((c = *ptr) != NUL && c != CAR && c != NL)
2197 continue;
2198 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002199 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
2203 {
2204 if (skip_count == 0)
2205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 len = (colnr_T) (ptr - line_start + 1);
2208 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2209 {
2210 error = TRUE;
2211 break;
2212 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002213#ifdef FEAT_PERSISTENT_UNDO
2214 if (read_undo_file)
2215 sha256_update(&sha_ctx, line_start, len);
2216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ++lnum;
2218 if (--read_count == 0)
2219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 error = TRUE; // break loop
2221 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 break;
2223 }
2224 }
2225 else
2226 --skip_count;
2227 line_start = ptr + 1;
2228 }
2229 }
2230 }
2231 else
2232 {
2233 --ptr;
2234 while (++ptr, --size >= 0)
2235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 continue;
2238 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 else
2241 {
2242 if (skip_count == 0)
2243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 len = (colnr_T)(ptr - line_start + 1);
2246 if (fileformat == EOL_DOS)
2247 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002248 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 ptr[-1] = NUL;
2252 --len;
2253 }
2254 /*
2255 * Reading in Dos format, but no CR-LF found!
2256 * When 'fileformats' includes "unix", delete all
2257 * the lines read so far and start all over again.
2258 * Otherwise give an error message later.
2259 */
2260 else if (ff_error != EOL_DOS)
2261 {
2262 if ( try_unix
2263 && !read_stdin
2264 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002265 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2266 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 set_fileformat(EOL_UNIX, OPT_LOCAL);
2271 file_rewind = TRUE;
2272 keep_fileformat = TRUE;
2273 goto retry;
2274 }
2275 ff_error = EOL_DOS;
2276 }
2277 }
2278 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2279 {
2280 error = TRUE;
2281 break;
2282 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002283#ifdef FEAT_PERSISTENT_UNDO
2284 if (read_undo_file)
2285 sha256_update(&sha_ctx, line_start, len);
2286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 ++lnum;
2288 if (--read_count == 0)
2289 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 error = TRUE; // break loop
2291 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 break;
2293 }
2294 }
2295 else
2296 --skip_count;
2297 line_start = ptr + 1;
2298 }
2299 }
2300 }
2301 linerest = (long)(ptr - line_start);
2302 ui_breakcheck();
2303 }
2304
2305failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (error && read_count == 0)
2308 error = FALSE;
2309
K.Takata3af98212022-11-01 20:36:19 +00002310 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2311 // In old days the file length was in sector count and the CTRL-Z the
2312 // marker where the file really ended. Assuming we write it to a file
2313 // system that keeps file length properly the CTRL-Z should be dropped.
2314 // Set the 'endoffile' option so the user can decide what to write later.
2315 // In Unix format the CTRL-Z is just another character.
2316 if (linerest != 0
2317 && !curbuf->b_p_bin
2318 && fileformat == EOL_DOS
2319 && ptr[-1] == Ctrl_Z)
2320 {
2321 ptr--;
2322 linerest--;
2323 if (set_options)
2324 curbuf->b_p_eof = TRUE;
2325 }
2326
2327 // If we get EOF in the middle of a line, note the fact by resetting
2328 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (!error
2330 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002331 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002334 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 curbuf->b_p_eol = FALSE;
2336 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002337 len = (colnr_T)(ptr - line_start + 1);
2338 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 error = TRUE;
2340 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002341 {
2342#ifdef FEAT_PERSISTENT_UNDO
2343 if (read_undo_file)
2344 sha256_update(&sha_ctx, line_start, len);
2345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002350 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002351 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002354 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002355 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002356 crypt_free_state(curbuf->b_cryptstate);
2357 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002358 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002359 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2360 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 // Don't set cryptkey to NULL, it's used below as a flag that
2362 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#endif
2364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 // If editing a new file: set 'fenc' for the current buffer.
2366 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002367 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002369 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (fenc_alloced)
2371 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002372#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375#endif
2376
2377 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002378 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002379#ifdef HAVE_FD_CLOEXEC
2380 else
2381 {
2382 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002383
Bram Moolenaarf05da212009-11-17 16:13:15 +00002384 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002385 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002386 }
2387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 vim_free(buffer);
2389
2390#ifdef HAVE_DUP
2391 if (read_stdin)
2392 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002393 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002395 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (tmpname != NULL)
2400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 vim_free(tmpname);
2403 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002404 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /*
2407 * In recovery mode everything but autocommands is skipped.
2408 */
2409 if (!recoverymode)
2410 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002411 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2413 {
2414#ifdef FEAT_NETBEANS_INTG
2415 netbeansFireChanges = 0;
2416#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002417 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef FEAT_NETBEANS_INTG
2419 netbeansFireChanges = 1;
2420#endif
2421 --linecnt;
2422 }
2423 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2424 if (filesize == 0)
2425 linecnt = 0;
2426 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002427 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002428 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002429#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 // After reading the text into the buffer the diff info needs to
2431 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002432 diff_invalidate(curbuf);
2433#endif
2434#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 // All folds in the window are invalid now. Mark them for update
2436 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002437 foldUpdateAll(curwin);
2438#endif
2439 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002440 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 appended_lines_mark(from, linecnt);
2442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#ifndef ALWAYS_USE_GUI
2444 /*
2445 * If we were reading from the same terminal as where messages go,
2446 * the screen will have been messed up.
2447 * Switch on raw mode now and clear the screen.
2448 */
2449 if (read_stdin)
2450 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002451 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 starttermcap();
2453 screenclear();
2454 }
2455#endif
2456
2457 if (got_int)
2458 {
2459 if (!(flags & READ_DUMMY))
2460 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002461 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 msg_scroll = msg_save;
2466#ifdef FEAT_VIMINFO
2467 check_marks_read();
2468#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002469 retval = OK; // an interrupt isn't really an error
2470 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 if (!filtering && !(flags & READ_DUMMY))
2474 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002475 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 c = FALSE;
2477
2478#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 STRCAT(IObuff, _("[fifo]"));
2482 c = TRUE;
2483 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002484 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 STRCAT(IObuff, _("[socket]"));
2487 c = TRUE;
2488 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002489# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002490 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002491 {
2492 STRCAT(IObuff, _("[character special]"));
2493 c = TRUE;
2494 }
2495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#endif
2497 if (curbuf->b_p_ro)
2498 {
2499 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2500 c = TRUE;
2501 }
2502 if (read_no_eol_lnum)
2503 {
2504 msg_add_eol();
2505 c = TRUE;
2506 }
2507 if (ff_error == EOL_DOS)
2508 {
2509 STRCAT(IObuff, _("[CR missing]"));
2510 c = TRUE;
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (split)
2513 {
2514 STRCAT(IObuff, _("[long lines split]"));
2515 c = TRUE;
2516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (notconverted)
2518 {
2519 STRCAT(IObuff, _("[NOT converted]"));
2520 c = TRUE;
2521 }
2522 else if (converted)
2523 {
2524 STRCAT(IObuff, _("[converted]"));
2525 c = TRUE;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#ifdef FEAT_CRYPT
2528 if (cryptkey != NULL)
2529 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002530 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 c = TRUE;
2532 }
2533#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002534 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002536 sprintf((char *)IObuff + STRLEN(IObuff),
2537 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 c = TRUE;
2539 }
2540 else if (illegal_byte > 0)
2541 {
2542 sprintf((char *)IObuff + STRLEN(IObuff),
2543 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2544 c = TRUE;
2545 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002546 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 STRCAT(IObuff, _("[READ ERRORS]"));
2549 c = TRUE;
2550 }
2551 if (msg_add_fileformat(fileformat))
2552 c = TRUE;
2553#ifdef FEAT_CRYPT
2554 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002555 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002556 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 else
2558#endif
2559 msg_add_lines(c, (long)linecnt, filesize);
2560
Bram Moolenaard23a8232018-02-10 18:45:26 +01002561 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 msg_scrolled_ign = TRUE;
2563#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002564 // Don't show the message when reading stdin, it would end up in a
2565 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (read_stdin || read_buffer)
2567 p = msg_may_trunc(FALSE, IObuff);
2568 else
2569#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002570 {
2571 if (msg_col > 0)
2572 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002573 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002576 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002577 // Need to repeat the message after redrawing when:
2578 // - When reading from stdin (the screen will be cleared next).
2579 // - When restart_edit is set (otherwise there will be a delay
2580 // before redrawing).
2581 // - When the screen was scrolled but there is no wait-return
2582 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002583 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 msg_scrolled_ign = FALSE;
2585 }
2586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002587 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002589 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002590 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 curbuf->b_p_ro = TRUE;
2592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002593 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595 /*
2596 * In Ex mode: cursor at last new line.
2597 * Otherwise: cursor at first new line.
2598 */
2599 if (exmode_active)
2600 curwin->w_cursor.lnum = from + linecnt;
2601 else
2602 curwin->w_cursor.lnum = from + 1;
2603 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002604 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
Bram Moolenaare1004402020-10-24 20:49:43 +02002606 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002607 {
2608 // Set '[ and '] marks to the newly read lines.
2609 curbuf->b_op_start.lnum = from + 1;
2610 curbuf->b_op_start.col = 0;
2611 curbuf->b_op_end.lnum = from + linecnt;
2612 curbuf->b_op_end.col = 0;
2613 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002614
Bram Moolenaar4f974752019-02-17 17:44:42 +01002615#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002616 /*
2617 * Work around a weird problem: When a file has two links (only
2618 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002619 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002620 * It's correct again after reading the file, thus reset the timestamp
2621 * here.
2622 */
2623 if (newfile && !read_stdin && !read_buffer
2624 && mch_stat((char *)fname, &st) >= 0)
2625 {
2626 buf_store_time(curbuf, &st, fname);
2627 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002628 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002629 }
2630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 msg_scroll = msg_save;
2633
2634#ifdef FEAT_VIMINFO
2635 /*
2636 * Get the marks before executing autocommands, so they can be used there.
2637 */
2638 check_marks_read();
2639#endif
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002642 * We remember if the last line of the read didn't have
2643 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2644 * or writing the read again with 'binary' on. The latter is required
2645 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002647 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002649 // When reloading a buffer put the cursor at the first line that is
2650 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002651 if (flags & READ_KEEP_UNDO)
2652 u_find_first_changed();
2653
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002654#ifdef FEAT_PERSISTENT_UNDO
2655 /*
2656 * When opening a new file locate undo info and read it.
2657 */
2658 if (read_undo_file)
2659 {
2660 char_u hash[UNDO_HASH_SIZE];
2661
2662 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002663 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002664 }
2665#endif
2666
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002667 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 int m = msg_scroll;
2670 int n = msg_scrolled;
2671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002672 // Save the fileformat now, otherwise the buffer will be considered
2673 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002674 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 save_file_ff(curbuf);
2676
2677 /*
2678 * The output from the autocommands should not overwrite anything and
2679 * should not be overwritten: Set msg_scroll, restore its value if no
2680 * output was done.
2681 */
2682 msg_scroll = TRUE;
2683 if (filtering)
2684 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2685 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002686 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2689 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002690 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2691 /*
2692 * EVENT_FILETYPE was not triggered but the buffer already has a
2693 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2694 */
2695 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2696 TRUE, curbuf);
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2700 FALSE, NULL, eap);
2701 if (msg_scrolled == n)
2702 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002703# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002704 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002705 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002709 if (!(recoverymode && error))
2710 retval = OK;
2711
2712theend:
2713 if (curbuf->b_ml.ml_mfp != NULL
2714 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2715 // OK to sync the swap file now
2716 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2717
2718 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002721#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002722/*
2723 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2724 * which is the name of files used for process substitution output by
2725 * some shells on some operating systems, e.g., bash on SunOS.
2726 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2727 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002728 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002729is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002730{
2731 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2732 && VIM_ISDIGIT(fname[8])
2733 && *skipdigits(fname + 9) == NUL
2734 && (fname[9] != NUL
2735 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2736}
2737#endif
2738
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002739/*
2740 * From the current line count and characters read after that, estimate the
2741 * line number where we are now.
2742 * Used for error messages that include a line number.
2743 */
2744 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002745readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002746 linenr_T linecnt, // line count before reading more bytes
2747 char_u *p, // start of more bytes read
2748 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002749{
2750 char_u *s;
2751 linenr_T lnum;
2752
2753 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2754 for (s = p; s < endp; ++s)
2755 if (*s == '\n')
2756 ++lnum;
2757 return lnum;
2758}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
Rob Pilling8196e942022-02-11 15:12:10 +00002761 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002762 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * Returns OK or FAIL.
2764 */
2765 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002766prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
Bram Moolenaar13505972019-01-24 15:04:48 +01002768 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (eap->cmd == NULL)
2770 return FAIL;
2771
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002772 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2773 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002774 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002775 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002776
2777 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002778 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002779 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 return OK;
2781}
2782
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002783/*
2784 * Set default or forced 'fileformat' and 'binary'.
2785 */
2786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002787set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002788{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002789 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002790 if (set_options)
2791 {
2792 if (eap != NULL && eap->force_ff != 0)
2793 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2794 else if (*p_ffs != NUL)
2795 set_fileformat(default_fileformat(), OPT_LOCAL);
2796 }
2797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002798 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002799 if (eap != NULL && eap->force_bin != 0)
2800 {
2801 int oldval = curbuf->b_p_bin;
2802
2803 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2804 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2805 }
2806}
2807
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002808/*
2809 * Set forced 'fileencoding'.
2810 */
2811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002812set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002813{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002814 if (eap->force_enc == 0)
2815 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002816
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002817 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2818
2819 if (fenc != NULL)
2820 set_string_option_direct((char_u *)"fenc", -1,
2821 fenc, OPT_FREE|OPT_LOCAL, 0);
2822 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002823}
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825/*
2826 * Find next fileencoding to use from 'fileencodings'.
2827 * "pp" points to fenc_next. It's advanced to the next item.
2828 * When there are no more items, an empty string is returned and *pp is set to
2829 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002830 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2831 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
2833 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002834next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 char_u *p;
2837 char_u *r;
2838
Bram Moolenaarf077db22019-08-13 00:18:24 +02002839 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (**pp == NUL)
2841 {
2842 *pp = NULL;
2843 return (char_u *)"";
2844 }
2845 p = vim_strchr(*pp, ',');
2846 if (p == NULL)
2847 {
2848 r = enc_canonize(*pp);
2849 *pp += STRLEN(*pp);
2850 }
2851 else
2852 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002853 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 *pp = p + 1;
2855 if (r != NULL)
2856 {
2857 p = enc_canonize(r);
2858 vim_free(r);
2859 r = p;
2860 }
2861 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002862 if (r != NULL)
2863 *alloced = TRUE;
2864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002866 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 r = (char_u *)"";
2868 *pp = NULL;
2869 }
2870 return r;
2871}
2872
Bram Moolenaar13505972019-01-24 15:04:48 +01002873#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874/*
2875 * Convert a file with the 'charconvert' expression.
2876 * This closes the file which is to be read, converts it and opens the
2877 * resulting file for reading.
2878 * Returns name of the resulting converted file (the caller should delete it
2879 * after reading it).
2880 * Returns NULL if the conversion failed ("*fdp" is not set) .
2881 */
2882 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002883readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002884 char_u *fname, // name of input file
2885 char_u *fenc, // converted from
2886 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002889 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002891 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002893 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
2895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002896 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 *fdp = -1;
2898 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2899 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002900 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2902 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002903 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905
2906 if (errmsg != NULL)
2907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002908 // Don't use emsg(), it breaks mappings, the retry with
2909 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002910 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (tmpname != NULL)
2912 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002914 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 }
2917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002918 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (*fdp < 0)
2920 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2921
2922 return tmpname;
2923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#endif
2925
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002926#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002928 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2930 * *filesizep are updated.
2931 * Return the (new) encryption key, NULL for no encryption.
2932 */
2933 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002934check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002935 char_u *cryptkey, // previous encryption key or NULL
2936 char_u *ptr, // pointer to read bytes
2937 long *sizep, // length of read bytes
2938 off_T *filesizep, // nr of bytes used from file
2939 int newfile, // editing a new buffer
2940 char_u *fname, // file name to display
2941 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002943 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002944 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002945
2946 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002948 // Mark the buffer as read-only until the decryption has taken place.
2949 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002950 curbuf->b_p_ro = TRUE;
2951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002952 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002953 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002954 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002959 crypt_check_swapfile_curbuf();
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else
2962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // When newfile is TRUE, store the typed key in the 'key'
2964 // option and don't free it. bf needs hash of the key saved.
2965 // Don't ask for the key again when first time Enter was hit.
2966 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002968 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002969 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002970 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002971 *did_ask = TRUE;
2972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002973 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (cryptkey != NULL && *cryptkey == NUL)
2975 {
2976 if (cryptkey != curbuf->b_p_key)
2977 vim_free(cryptkey);
2978 cryptkey = NULL;
2979 }
2980 }
2981 }
2982
2983 if (cryptkey != NULL)
2984 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002985 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002986
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002987 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002988 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002989 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002990 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002991
2992 curbuf->b_cryptstate = crypt_create_from_header(
2993 method, cryptkey, ptr);
2994 crypt_set_cm_option(curbuf, method);
2995
2996 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002997 *filesizep += header_len;
2998 *sizep -= header_len;
2999 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003001 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003002 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003005 // When starting to edit a new file which does not have encryption, clear
3006 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003007 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003008 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 return cryptkey;
3011}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003012#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003015 * Return TRUE if a file appears to be read-only from the file permissions.
3016 */
3017 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003018check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003019 char_u *fname, // full path to file
3020 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003021{
3022#ifndef USE_MCH_ACCESS
3023 int fd = 0;
3024#endif
3025
3026 return (
3027#ifdef USE_MCH_ACCESS
3028# ifdef UNIX
3029 (perm & 0222) == 0 ||
3030# endif
3031 mch_access((char *)fname, W_OK)
3032#else
3033 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3034 ? TRUE : (close(fd), FALSE)
3035#endif
3036 );
3037}
3038
Bram Moolenaara7870192019-02-14 12:56:36 +01003039#if defined(HAVE_FSYNC) || defined(PROTO)
3040/*
3041 * Call fsync() with Mac-specific exception.
3042 * Return fsync() result: zero for success.
3043 */
3044 int
3045vim_fsync(int fd)
3046{
3047 int r;
3048
3049# ifdef MACOS_X
3050 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003051 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003052# endif
3053 r = fsync(fd);
3054 return r;
3055}
3056#endif
3057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003059 * Set the name of the current buffer. Use when the buffer doesn't have a
3060 * name and a ":r" or ":w" command with a file name is used.
3061 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003063set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003064{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003065 buf_T *buf = curbuf;
3066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003067 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003068 if (curbuf->b_p_bl)
3069 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3070 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003071#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003072 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003073 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003074#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003075 if (curbuf != buf)
3076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003077 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003078 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003079 return FAIL;
3080 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003081
3082 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3083 curbuf->b_flags |= BF_NOTEDITED;
3084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003085 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003086 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3087 if (curbuf->b_p_bl)
3088 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003089#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003090 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003091 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003092#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003093
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003094 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003095 if (*curbuf->b_p_ft == NUL)
3096 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003097 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003098 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003099 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003100 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003101
3102 return OK;
3103}
3104
3105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Put file name into IObuff with quotes.
3107 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 if (fname == NULL)
3112 fname = (char_u *)"-stdin-";
3113 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3114 IObuff[0] = '"';
3115 STRCAT(IObuff, "\" ");
3116}
3117
3118/*
3119 * Append message for text mode to IObuff.
3120 * Return TRUE if something appended.
3121 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003122 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003123msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125#ifndef USE_CRNL
3126 if (eol_type == EOL_DOS)
3127 {
3128 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3129 return TRUE;
3130 }
3131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (eol_type == EOL_MAC)
3133 {
3134 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3135 return TRUE;
3136 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003137#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (eol_type == EOL_UNIX)
3139 {
3140 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3141 return TRUE;
3142 }
3143#endif
3144 return FALSE;
3145}
3146
3147/*
3148 * Append line and character count to IObuff.
3149 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151msg_add_lines(
3152 int insert_space,
3153 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003154 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
3156 char_u *p;
3157
3158 p = IObuff + STRLEN(IObuff);
3159
3160 if (insert_space)
3161 *p++ = ' ';
3162 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003163 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003164 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 else
3166 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003167 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003169 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003170 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003171 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
3173}
3174
3175/*
3176 * Append message for missing line separator to IObuff.
3177 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003178 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003179msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3182}
3183
Bram Moolenaar473952e2019-09-28 16:30:04 +02003184 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003185time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
ichizokdef69df2021-10-15 17:23:12 +01003187 return
3188#ifdef ST_MTIM_NSEC
3189 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#endif
ichizokdef69df2021-10-15 17:23:12 +01003191#if defined(__linux__) || defined(MSWIN)
3192 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3193 // the seconds. Since the roundoff is done when flushing the inode, the
3194 // time may change unexpectedly by one second!!!
3195 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3196#else
3197 (long)st->st_mtime != mtime
3198#endif
3199 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200}
3201
3202/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003203 * Return TRUE if file encoding "fenc" requires conversion from or to
3204 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003206 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003207need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003209 int same_encoding;
3210 int enc_flags;
3211 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003213 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003214 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003215 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003216 fenc_flags = 0;
3217 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003218 else
3219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3221 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003222 enc_flags = get_fio_flags(p_enc);
3223 fenc_flags = get_fio_flags(fenc);
3224 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3225 }
3226 if (same_encoding)
3227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 // Specified encoding matches with 'encoding'. This requires
3229 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003230 return enc_unicode != 0;
3231 }
3232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 // Encodings differ. However, conversion is not needed when 'enc' is any
3234 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003235 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236}
3237
3238/*
3239 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3240 * internal conversion.
3241 * if "ptr" is an empty string, use 'encoding'.
3242 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003243 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003244get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 int prop;
3247
3248 if (*ptr == NUL)
3249 ptr = p_enc;
3250
3251 prop = enc_canon_props(ptr);
3252 if (prop & ENC_UNICODE)
3253 {
3254 if (prop & ENC_2BYTE)
3255 {
3256 if (prop & ENC_ENDIAN_L)
3257 return FIO_UCS2 | FIO_ENDIAN_L;
3258 return FIO_UCS2;
3259 }
3260 if (prop & ENC_4BYTE)
3261 {
3262 if (prop & ENC_ENDIAN_L)
3263 return FIO_UCS4 | FIO_ENDIAN_L;
3264 return FIO_UCS4;
3265 }
3266 if (prop & ENC_2WORD)
3267 {
3268 if (prop & ENC_ENDIAN_L)
3269 return FIO_UTF16 | FIO_ENDIAN_L;
3270 return FIO_UTF16;
3271 }
3272 return FIO_UTF8;
3273 }
3274 if (prop & ENC_LATIN1)
3275 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003276 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 return 0;
3278}
3279
Bram Moolenaar473952e2019-09-28 16:30:04 +02003280#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281/*
3282 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3283 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3284 * Used for conversion between 'encoding' and 'fileencoding'.
3285 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003286 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003287get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 int cp;
3290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003291 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (!enc_utf8 && enc_codepage <= 0)
3293 return 0;
3294
3295 cp = encname2codepage(ptr);
3296 if (cp == 0)
3297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003298# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (STRCMP(ptr, "utf-8") == 0)
3300 cp = CP_UTF8;
3301 else
3302# endif
3303 return 0;
3304 }
3305 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3306}
3307#endif
3308
Bram Moolenaar473952e2019-09-28 16:30:04 +02003309#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310/*
3311 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3312 * needed for the internal conversion to/from utf-8 or latin1.
3313 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3318 && (enc_canon_props(ptr) & ENC_MACROMAN))
3319 return FIO_MACROMAN;
3320 return 0;
3321}
3322#endif
3323
3324/*
3325 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3326 * "size" must be at least 2.
3327 * Return the name of the encoding and set "*lenp" to the length.
3328 * Returns NULL when no BOM found.
3329 */
3330 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003331check_for_bom(
3332 char_u *p,
3333 long size,
3334 int *lenp,
3335 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 char *name = NULL;
3338 int len = 2;
3339
3340 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003341 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003343 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 len = 3;
3345 }
3346 else if (p[0] == 0xff && p[1] == 0xfe)
3347 {
3348 if (size >= 4 && p[2] == 0 && p[3] == 0
3349 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003351 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 len = 4;
3353 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003354 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003355 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003356 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003357 // utf-16le is preferred, it also works for ucs-2le text
3358 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 else if (p[0] == 0xfe && p[1] == 0xff
3361 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003363 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003364 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003365 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003366 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003367 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3370 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 len = 4;
3374 }
3375
3376 *lenp = len;
3377 return (char_u *)name;
3378}
3379
3380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * Try to find a shortname by comparing the fullname with the current
3382 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003383 * Returns "full_path" or pointer into "full_path" if shortened.
3384 */
3385 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003387{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003388 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003389 char_u *p = full_path;
3390
Bram Moolenaard9462e32011-04-11 21:35:11 +02003391 dirname = alloc(MAXPATHL);
3392 if (dirname == NULL)
3393 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003394 if (mch_dirname(dirname, MAXPATHL) == OK)
3395 {
3396 p = shorten_fname(full_path, dirname);
3397 if (p == NULL || *p == NUL)
3398 p = full_path;
3399 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003400 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003401 return p;
3402}
3403
3404/*
3405 * Try to find a shortname by comparing the fullname with the current
3406 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 * Returns NULL if not shorter name possible, pointer into "full_path"
3408 * otherwise.
3409 */
3410 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003411shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 int len;
3414 char_u *p;
3415
3416 if (full_path == NULL)
3417 return NULL;
3418 len = (int)STRLEN(dir_name);
3419 if (fnamencmp(dir_name, full_path, len) == 0)
3420 {
3421 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003422#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003424 * MS-Windows: when a file is in the root directory, dir_name will end
3425 * in a slash, since C: by itself does not define a specific dir. In
3426 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
3428 if (!((len > 2) && (*(p - 2) == ':')))
3429#endif
3430 {
3431 if (vim_ispathsep(*p))
3432 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003433#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else
3435 p = NULL;
3436#endif
3437 }
3438 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003439#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /*
3441 * When using a file in the current drive, remove the drive name:
3442 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3443 * a floppy from "A:\dir" to "B:\dir".
3444 */
3445 else if (len > 3
3446 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3447 && full_path[1] == ':'
3448 && vim_ispathsep(full_path[2]))
3449 p = full_path + 2;
3450#endif
3451 else
3452 p = NULL;
3453 return p;
3454}
3455
3456/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003457 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 * When "force" is TRUE: Use full path from now on for files currently being
3459 * edited, both for file name and swap file name. Try to shorten the file
3460 * names a bit, if safe to do so.
3461 * When "force" is FALSE: Only try to shorten absolute file names.
3462 * For buffers that have buftype "nofile" or "scratch": never change the file
3463 * name.
3464 */
3465 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003466shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3467{
3468 char_u *p;
3469
3470 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003471 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003472 && !path_with_url(buf->b_fname)
3473 && (force
3474 || buf->b_sfname == NULL
3475 || mch_isFullName(buf->b_sfname)))
3476 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003477 if (buf->b_sfname != buf->b_ffname)
3478 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003479 p = shorten_fname(buf->b_ffname, dirname);
3480 if (p != NULL)
3481 {
3482 buf->b_sfname = vim_strsave(p);
3483 buf->b_fname = buf->b_sfname;
3484 }
3485 if (p == NULL || buf->b_fname == NULL)
3486 buf->b_fname = buf->b_ffname;
3487 }
3488}
3489
3490/*
3491 * Shorten filenames for all buffers.
3492 */
3493 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003494shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u dirname[MAXPATHL];
3497 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003500 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003502 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003504 // Always make the swap file name a full path, a "nofile" buffer may
3505 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003506 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003509 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003510#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003511 popup_update_preview_title();
3512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513}
3514
3515#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3516 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003517 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 || defined(PROTO)
3519/*
3520 * Shorten all filenames in "fnames[count]" by current directory.
3521 */
3522 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003523shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 int i;
3526 char_u dirname[MAXPATHL];
3527 char_u *p;
3528
3529 if (fnames == NULL || count < 1)
3530 return;
3531 mch_dirname(dirname, sizeof(dirname));
3532 for (i = 0; i < count; ++i)
3533 {
3534 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003536 // shorten_fname() returns pointer in given "fnames[i]". If free
3537 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3538 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 p = vim_strsave(p);
3540 vim_free(fnames[i]);
3541 fnames[i] = p;
3542 }
3543 }
3544}
3545#endif
3546
3547/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003548 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 * fo_o_h.ext for MSDOS or when shortname option set.
3550 *
3551 * Assumed that fname is a valid name found in the filesystem we assure that
3552 * the return value is a different name and ends in 'ext'.
3553 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3554 * characters otherwise.
3555 * Space for the returned name is allocated, must be freed later.
3556 * Returns NULL when out of memory.
3557 */
3558 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003559modname(
3560 char_u *fname,
3561 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003562 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003564 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 fname, ext, prepend_dot);
3566}
3567
3568 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003569buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571 char_u *fname,
3572 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 char_u *retval;
3576 char_u *s;
3577 char_u *e;
3578 char_u *ptr;
3579 int fnamelen, extlen;
3580
3581 extlen = (int)STRLEN(ext);
3582
3583 /*
3584 * If there is no file name we must get the name of the current directory
3585 * (we need the full path in case :cd is used).
3586 */
3587 if (fname == NULL || *fname == NUL)
3588 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003589 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (retval == NULL)
3591 return NULL;
3592 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3593 (fnamelen = (int)STRLEN(retval)) == 0)
3594 {
3595 vim_free(retval);
3596 return NULL;
3597 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003598 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 retval[fnamelen++] = PATHSEP;
3601 retval[fnamelen] = NUL;
3602 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003603 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605 else
3606 {
3607 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003608 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (retval == NULL)
3610 return NULL;
3611 STRCPY(retval, fname);
3612#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003613 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614#endif
3615 }
3616
3617 /*
3618 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3619 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3620 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3621 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3622 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003623 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003625 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003626 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003629 {
3630 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003635 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3637 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 s = ptr + STRLEN(ptr);
3640
3641 /*
3642 * For 8.3 file names we may have to reduce the length.
3643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
3646 /*
3647 * If there is no file name, or the file name ends in '/', and the
3648 * extension starts with '.', put a '_' before the dot, because just
3649 * ".ext" is invalid.
3650 */
3651 if (fname == NULL || *fname == NUL
3652 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 *s++ = '_';
3656 }
3657 /*
3658 * If the extension starts with '.', truncate the base name at 8
3659 * characters
3660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003663 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 s = ptr + 8;
3666 *s = '\0';
3667 }
3668 }
3669 /*
3670 * If the extension doesn't start with '.', and the file name
3671 * doesn't have an extension yet, append a '.'
3672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else if ((e = vim_strchr(ptr, '.')) == NULL)
3674 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /*
3676 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003677 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 */
3679 else if ((int)STRLEN(e) + extlen > 4)
3680 s = e + 4 - extlen;
3681 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003682#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 /*
3684 * If there is no file name, and the extension starts with '.', put a
3685 * '_' before the dot, because just ".ext" may be invalid if it's on a
3686 * FAT partition, and on HPFS it doesn't matter.
3687 */
3688 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3689 *s++ = '_';
3690#endif
3691
3692 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003693 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 * ext can start with '.' and cannot exceed 3 more characters.
3695 */
3696 STRCPY(s, ext);
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 /*
3699 * Prepend the dot.
3700 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003701 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003703 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /*
3708 * Check that, after appending the extension, the file name is really
3709 * different.
3710 */
3711 if (fname != NULL && STRCMP(fname, retval) == 0)
3712 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003713 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 while (--s >= ptr)
3715 {
3716 if (*s != '_')
3717 {
3718 *s = '_';
3719 break;
3720 }
3721 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 *ptr = 'v';
3724 }
3725 return retval;
3726}
3727
3728/*
3729 * Like fgets(), but if the file line is too long, it is truncated and the
3730 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003731 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003734vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
3736 char *eof;
3737#define FGETS_SIZE 200
3738 char tbuf[FGETS_SIZE];
3739
3740 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3743 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003744 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003746 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 do
3748 {
3749 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003750 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3752 }
3753 return (eof == NULL);
3754}
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
3757 * rename() only works if both files are on the same file system, this
3758 * function will (attempts to?) copy the file across if rename fails -- webb
3759 * Return -1 for failure, 0 for success.
3760 */
3761 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003762vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 int fd_in;
3765 int fd_out;
3766 int n;
3767 char *errmsg = NULL;
3768 char *buffer;
3769#ifdef AMIGA
3770 BPTR flock;
3771#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003772 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003773 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003774#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003775 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003776#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003777 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003780 * When the names are identical, there is nothing to do. When they refer
3781 * to the same file (ignoring case and slash/backslash differences) but
3782 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
3784 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003785 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003786 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003787 use_tmp_file = TRUE;
3788 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003789 return 0;
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /*
3793 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3794 */
3795 if (mch_stat((char *)from, &st) < 0)
3796 return -1;
3797
Bram Moolenaar3576da72008-12-30 15:15:57 +00003798#ifdef UNIX
3799 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003800 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003802 // It's possible for the source and destination to be the same file.
3803 // This happens when "from" and "to" differ in case and are on a FAT32
3804 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003805 if (mch_stat((char *)to, &st_to) >= 0
3806 && st.st_dev == st_to.st_dev
3807 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003808 use_tmp_file = TRUE;
3809 }
3810#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003811#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003812 {
3813 BY_HANDLE_FILE_INFORMATION info1, info2;
3814
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003815 // It's possible for the source and destination to be the same file.
3816 // In that case go through a temp file name. This makes rename("foo",
3817 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003818 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3819 && win32_fileinfo(to, &info2) == FILEINFO_OK
3820 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3821 && info1.nFileIndexHigh == info2.nFileIndexHigh
3822 && info1.nFileIndexLow == info2.nFileIndexLow)
3823 use_tmp_file = TRUE;
3824 }
3825#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003826
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003827 if (use_tmp_file)
3828 {
3829 char tempname[MAXPATHL + 1];
3830
3831 /*
3832 * Find a name that doesn't exist and is in the same directory.
3833 * Rename "from" to "tempname" and then rename "tempname" to "to".
3834 */
3835 if (STRLEN(from) >= MAXPATHL - 5)
3836 return -1;
3837 STRCPY(tempname, from);
3838 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003839 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003840 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3841 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003842 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003843 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003844 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003845 if (mch_rename(tempname, (char *)to) == 0)
3846 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003847 // Strange, the second step failed. Try moving the
3848 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003849 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003850 return -1;
3851 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // If it fails for one temp name it will most likely fail
3853 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003854 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003855 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003856 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003857 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003858 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 /*
3861 * Delete the "to" file, this is required on some systems to make the
3862 * mch_rename() work, on other systems it makes sure that we don't have
3863 * two files when the mch_rename() fails.
3864 */
3865
3866#ifdef AMIGA
3867 /*
3868 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3869 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003870 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 * deleting the "from" file (horror!) we lock it during the remove.
3872 *
3873 * When used for making a backup before writing the file: This should not
3874 * happen with ":w", because startscript() should detect this problem and
3875 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3876 * name. This problem does exist with ":w filename", but then the
3877 * original file will be somewhere else so the backup isn't really
3878 * important. If autoscripting is off the rename may fail.
3879 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003880 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881#endif
3882 mch_remove(to);
3883#ifdef AMIGA
3884 if (flock)
3885 UnLock(flock);
3886#endif
3887
3888 /*
3889 * First try a normal rename, return if it works.
3890 */
3891 if (mch_rename((char *)from, (char *)to) == 0)
3892 return 0;
3893
3894 /*
3895 * Rename() failed, try copying the file.
3896 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003897 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003898#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003899 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003900 acl = mch_get_acl(from);
3901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3903 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003904 {
3905#ifdef HAVE_ACL
3906 mch_free_acl(acl);
3907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003909 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003911 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003912 fd_out = mch_open((char *)to,
3913 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (fd_out == -1)
3915 {
3916 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003917#ifdef HAVE_ACL
3918 mch_free_acl(acl);
3919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return -1;
3921 }
3922
Bram Moolenaar473952e2019-09-28 16:30:04 +02003923 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (buffer == NULL)
3925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003927 close(fd_in);
3928#ifdef HAVE_ACL
3929 mch_free_acl(acl);
3930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 return -1;
3932 }
3933
Bram Moolenaar473952e2019-09-28 16:30:04 +02003934 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003935 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003937 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939 }
3940
3941 vim_free(buffer);
3942 close(fd_in);
3943 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003944 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (n < 0)
3946 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003947 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 to = from;
3949 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003950#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003951 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003952#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003953#ifdef HAVE_ACL
3954 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003955 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003956#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003957#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003958 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (errmsg != NULL)
3961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003962 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 return -1;
3964 }
3965 mch_remove(from);
3966 return 0;
3967}
3968
3969static int already_warned = FALSE;
3970
3971/*
3972 * Check if any not hidden buffer has been changed.
3973 * Postpone the check if there are characters in the stuff buffer, a global
3974 * command is being executed, a mapping is being executed or an autocommand is
3975 * busy.
3976 * Returns TRUE if some message was written (screen should be redrawn and
3977 * cursor positioned).
3978 */
3979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003980check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003981 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 buf_T *buf;
3984 int didit = 0;
3985 int n;
3986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 // Don't check timestamps while system() or another low-level function may
3988 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (no_check_timestamps > 0)
3990 return FALSE;
3991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003992 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3993 // event and we would keep on checking if the file is steadily growing.
3994 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (focus && did_check_timestamps)
3996 {
3997 need_check_timestamps = TRUE;
3998 return FALSE;
3999 }
4000
4001 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004002 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004003 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 else
4005 {
4006 ++no_wait_return;
4007 did_check_timestamps = TRUE;
4008 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004009 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004011 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (buf->b_nwindows > 0)
4013 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004014 bufref_T bufref;
4015
4016 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 n = buf_check_timestamp(buf, focus);
4018 if (didit < n)
4019 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004020 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004022 // Autocommands have removed the buffer, start at the
4023 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 buf = firstbuf;
4025 continue;
4026 }
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 --no_wait_return;
4030 need_check_timestamps = FALSE;
4031 if (need_wait_return && didit == 2)
4032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004033 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004034 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 out_flush();
4036 }
4037 }
4038 return didit;
4039}
4040
4041/*
4042 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4043 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4044 * empty.
4045 */
4046 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004047move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
4049 buf_T *tbuf = curbuf;
4050 int retval = OK;
4051 linenr_T lnum;
4052 char_u *p;
4053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004054 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 curbuf = tobuf;
4056 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4057 {
4058 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4059 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4060 {
4061 vim_free(p);
4062 retval = FAIL;
4063 break;
4064 }
4065 vim_free(p);
4066 }
4067
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004068 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (retval != FAIL)
4070 {
4071 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004072 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004073 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004075 // Oops! We could try putting back the saved lines, but that
4076 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 retval = FAIL;
4078 break;
4079 }
4080 }
4081
4082 curbuf = tbuf;
4083 return retval;
4084}
4085
4086/*
4087 * Check if buffer "buf" has been changed.
4088 * Also check if the file for a new buffer unexpectedly appeared.
4089 * return 1 if a changed buffer was found.
4090 * return 2 if a message has been displayed.
4091 * return 0 otherwise.
4092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004094buf_check_timestamp(
4095 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004096 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004098 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 int stat_res;
4100 int retval = 0;
4101 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004104 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004106 enum {
4107 RELOAD_NONE,
4108 RELOAD_NORMAL,
4109 RELOAD_DETECT
4110 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004111 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4113 int can_reload = FALSE;
4114#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004115 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 int orig_mode = buf->b_orig_mode;
4117#ifdef FEAT_GUI
4118 int save_mouse_correct = need_mouse_correct;
4119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004121 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004122#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004123 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004124#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004125 bufref_T bufref;
4126
4127 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004129 // If there is no file name, the buffer is not loaded, 'buftype' is
4130 // set, we are in the middle of a save or being called recursively: ignore
4131 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (buf->b_ffname == NULL
4133 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004134 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004137#ifdef FEAT_NETBEANS_INTG
4138 || isNetbeansBuffer(buf)
4139#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004140#ifdef FEAT_TERMINAL
4141 || buf->b_term != NULL
4142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 )
4144 return 0;
4145
4146 if ( !(buf->b_flags & BF_NOTEDITED)
4147 && buf->b_mtime != 0
4148 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004149 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004150 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#ifdef HAVE_ST_MODE
4152 || (int)st.st_mode != buf->b_orig_mode
4153#else
4154 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4155#endif
4156 ))
4157 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004158 long prev_b_mtime = buf->b_mtime;
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 retval = 1;
4161
Bram Moolenaar386bc822018-07-07 18:34:12 +02004162 // set b_mtime to stop further warnings (e.g., when executing
4163 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (stat_res < 0)
4165 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004166 // Check the file again later to see if it re-appears.
4167 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 buf->b_orig_size = 0;
4169 buf->b_orig_mode = 0;
4170 }
4171 else
4172 buf_store_time(buf, &st, buf->b_ffname);
4173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004174 // Don't do anything for a directory. Might contain the file
4175 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 if (mch_isdir(buf->b_fname))
4177 ;
4178
4179 /*
4180 * If 'autoread' is set, the buffer has no changes and the file still
4181 * exists, reload the buffer. Use the buffer-local option value if it
4182 * was set, the global option value otherwise.
4183 */
4184 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4185 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004186 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
4188 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004189 if (stat_res < 0)
4190 reason = "deleted";
4191 else if (bufIsChanged(buf))
4192 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004193 /*
4194 * Check if the file contents really changed to avoid giving a
4195 * warning when only the timestamp was set (e.g., checked out of
4196 * CVS). Always warn when the buffer was changed.
4197 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004198 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4199 reason = "changed";
4200 else if (orig_mode != buf->b_orig_mode)
4201 reason = "mode";
4202 else
4203 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 /*
4206 * Only give the warning if there are no FileChangedShell
4207 * autocommands.
4208 * Avoid being called recursively by setting "busy".
4209 */
4210 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004211#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004212 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4213 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004214#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004215 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4217 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004218 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 busy = FALSE;
4220 if (n)
4221 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004222 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004223 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004224#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004225 s = get_vim_var_str(VV_FCS_CHOICE);
4226 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004227 reload = RELOAD_NORMAL;
4228 else if (STRCMP(s, "edit") == 0)
4229 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230 else if (STRCMP(s, "ask") == 0)
4231 n = FALSE;
4232 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004233#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004236 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004239 {
4240 // Only give the message once.
4241 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004242 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 else
4245 {
4246 helpmesg = TRUE;
4247#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4248 can_reload = TRUE;
4249#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004250 if (reason[2] == 'n')
4251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004253 mesg2 = _("See \":help W12\" for more info.");
4254 }
4255 else if (reason[1] == 'h')
4256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004258 mesg2 = _("See \":help W11\" for more info.");
4259 }
4260 else if (*reason == 'm')
4261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263 mesg2 = _("See \":help W16\" for more info.");
4264 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004265 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 // Only timestamp changed, store it to avoid a warning
4268 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004269 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004270 buf->b_mtime_read_ns = buf->b_mtime_ns;
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274 }
4275
4276 }
4277 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4278 && vim_fexists(buf->b_ffname))
4279 {
4280 retval = 1;
4281 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4282 buf->b_flags |= BF_NEW_W;
4283#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4284 can_reload = TRUE;
4285#endif
4286 }
4287
4288 if (mesg != NULL)
4289 {
4290 path = home_replace_save(buf, buf->b_fname);
4291 if (path != NULL)
4292 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004293 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004295 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004297#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 // Set warningmsg here, before the unimportant and output-specific
4299 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004300 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4303 if (can_reload)
4304 {
4305 if (*mesg2 != NUL)
4306 {
4307 STRCAT(tbuf, "\n");
4308 STRCAT(tbuf, mesg2);
4309 }
Rob Pilling8196e942022-02-11 15:12:10 +00004310 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4311 (char_u *)tbuf,
4312 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4313 1, NULL, TRUE))
4314 {
4315 case 2:
4316 reload = RELOAD_NORMAL;
4317 break;
4318 case 3:
4319 reload = RELOAD_DETECT;
4320 break;
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 else
4324#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004325 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4326 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 if (*mesg2 != NUL)
4329 {
4330 STRCAT(tbuf, "; ");
4331 STRCAT(tbuf, mesg2);
4332 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 retval = 2;
4335 }
4336 else
4337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
4340 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004341 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004343 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 msg_clr_eos();
4345 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004346 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004349#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004351#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004353 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004355 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 redraw_cmdline = FALSE;
4357 }
4358 }
4359 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361
4362 vim_free(path);
4363 vim_free(tbuf);
4364 }
4365 }
4366
Rob Pilling8196e942022-02-11 15:12:10 +00004367 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004368 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004370 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004371#ifdef FEAT_PERSISTENT_UNDO
4372 if (buf->b_p_udf && buf->b_ffname != NULL)
4373 {
4374 char_u hash[UNDO_HASH_SIZE];
4375 buf_T *save_curbuf = curbuf;
4376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004377 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004378 curbuf = buf;
4379 u_compute_hash(hash);
4380 u_write_undo(NULL, FALSE, buf, hash);
4381 curbuf = save_curbuf;
4382 }
4383#endif
4384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004387 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004388 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4389 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // restore this in case an autocommand has set it; it would break
4392 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 need_mouse_correct = save_mouse_correct;
4394#endif
4395
4396 return retval;
4397}
4398
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004399/*
4400 * Reload a buffer that is already loaded.
4401 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004402 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4403 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004404 */
4405 void
Rob Pilling8196e942022-02-11 15:12:10 +00004406buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004407{
4408 exarg_T ea;
4409 pos_T old_cursor;
4410 linenr_T old_topline;
4411 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004414 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004415 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004416 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004417 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004418
Bram Moolenaare76062c2022-11-28 18:51:43 +00004419 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004420 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004421 if (curbuf != buf)
4422 {
4423 // Failed to find a window for "buf", it is dangerous to continue,
4424 // better bail out.
4425 return;
4426 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004427
Rob Pilling8196e942022-02-11 15:12:10 +00004428 // Unless reload_options is set, we only want to read the text from the
4429 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4430 // Force the fileformat and encoding to be the same.
4431 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004432 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004433 else
4434 prepped = prep_exarg(&ea, buf);
4435
4436 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004437 {
4438 old_cursor = curwin->w_cursor;
4439 old_topline = curwin->w_topline;
4440
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004441 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 // Save all the text, so that the reload can be undone.
4444 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004445 u_sync(FALSE);
4446 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4447 flags |= READ_KEEP_UNDO;
4448 }
4449
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004450 /*
4451 * To behave like when a new file is edited (matters for
4452 * BufReadPost autocommands) we first need to delete the current
4453 * buffer contents. But if reading the file fails we should keep
4454 * the old contents. Can't use memory only, the file might be
4455 * too big. Use a hidden buffer to move the buffer contents to.
4456 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004457 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004458 savebuf = NULL;
4459 else
4460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004462 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004463 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004464 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004465 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004467 curbuf = savebuf;
4468 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004469 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004470 curbuf = buf;
4471 curwin->w_buffer = buf;
4472 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004473 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474 || move_lines(buf, savebuf) == FAIL)
4475 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004476 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004477 saved = FAIL;
4478 }
4479 }
4480
4481 if (saved == OK)
4482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004483 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4484 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004485 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4486 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004487 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004488 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004489#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004490 if (!aborting())
4491#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004492 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004493 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 // Put the text back from the save buffer. First
4496 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004497 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004498 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004499 break;
4500 (void)move_lines(savebuf, buf);
4501 }
4502 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004505 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004506 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004507 if ((flags & READ_KEEP_UNDO) == 0)
4508 {
4509 u_blockfree(buf);
4510 u_clearall(buf);
4511 }
4512 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004515 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004516 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004517 }
4518 }
4519 vim_free(ea.cmd);
4520
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004521 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004522 wipe_buffer(savebuf, FALSE);
4523
4524#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004526 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004527#endif
4528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004529 // Restore the topline and cursor position and check it (lines may
4530 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004531 if (old_topline > curbuf->b_ml.ml_line_count)
4532 curwin->w_topline = curbuf->b_ml.ml_line_count;
4533 else
4534 curwin->w_topline = old_topline;
4535 curwin->w_cursor = old_cursor;
4536 check_cursor();
4537 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004538 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004539#ifdef FEAT_FOLDING
4540 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004541 win_T *wp;
4542 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004544 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004545 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004546 if (wp->w_buffer == curwin->w_buffer
4547 && !foldmethodIsManual(wp))
4548 foldUpdateAll(wp);
4549 }
4550#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 // If the mode didn't change and 'readonly' was set, keep the old
4552 // value; the user probably used the ":view" command. But don't
4553 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004554 if (orig_mode == curbuf->b_orig_mode)
4555 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004558 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004559 }
4560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004561 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004562 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004563 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004564}
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004567buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
4569 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004570#ifdef ST_MTIM_NSEC
4571 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4572#else
4573 buf->b_mtime_ns = 0;
4574#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004575 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef HAVE_ST_MODE
4577 buf->b_orig_mode = (int)st->st_mode;
4578#else
4579 buf->b_orig_mode = mch_getperm(fname);
4580#endif
4581}
4582
4583/*
4584 * Adjust the line with missing eol, used for the next write.
4585 * Used for do_filter(), when the input lines for the filter are deleted.
4586 */
4587 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004588write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004590 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004591 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592}
4593
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004594// Subfuncions for readdirex()
4595#ifdef FEAT_EVAL
4596# ifdef MSWIN
4597 static char_u *
4598getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4599{
4600 stat_T st;
4601 unsigned short st_mode;
4602 DWORD flag = wfd->dwFileAttributes;
4603 WCHAR *wp;
4604
4605 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4606 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4607 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4608 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4609
4610 wp = wcsrchr(wfd->cFileName, L'.');
4611 if (wp != NULL)
4612 {
4613 if (_wcsicmp(wp, L".exe") == 0 ||
4614 _wcsicmp(wp, L".com") == 0 ||
4615 _wcsicmp(wp, L".cmd") == 0 ||
4616 _wcsicmp(wp, L".bat") == 0)
4617 st_mode |= _S_IEXEC;
4618 }
4619
4620 // Copy user bits to group/other.
4621 st_mode |= (st_mode & 0700) >> 3;
4622 st_mode |= (st_mode & 0700) >> 6;
4623
4624 st.st_mode = st_mode;
4625 return getfpermst(&st, perm);
4626}
4627
4628 static char_u *
4629getftypewfd(WIN32_FIND_DATAW *wfd)
4630{
4631 DWORD flag = wfd->dwFileAttributes;
4632 DWORD tag = wfd->dwReserved0;
4633
4634 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4635 {
4636 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4637 return (char_u*)"junction";
4638 else if (tag == IO_REPARSE_TAG_SYMLINK)
4639 {
4640 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4641 return (char_u*)"linkd";
4642 else
4643 return (char_u*)"link";
4644 }
4645 return (char_u*)"reparse"; // unknown reparse point type
4646 }
4647 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4648 return (char_u*)"dir";
4649 else
4650 return (char_u*)"file";
4651}
4652
4653 static dict_T *
4654create_readdirex_item(WIN32_FIND_DATAW *wfd)
4655{
4656 dict_T *item;
4657 char_u *p;
4658 varnumber_T size, time;
4659 char_u permbuf[] = "---------";
4660
4661 item = dict_alloc();
4662 if (item == NULL)
4663 return NULL;
4664 item->dv_refcount++;
4665
4666 p = utf16_to_enc(wfd->cFileName, NULL);
4667 if (p == NULL)
4668 goto theend;
4669 if (dict_add_string(item, "name", p) == FAIL)
4670 {
4671 vim_free(p);
4672 goto theend;
4673 }
4674 vim_free(p);
4675
4676 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4677 if (dict_add_number(item, "size", size) == FAIL)
4678 goto theend;
4679
4680 // Convert FILETIME to unix time.
4681 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4682 wfd->ftLastWriteTime.dwLowDateTime)
4683 - 116444736000000000) / 10000000;
4684 if (dict_add_number(item, "time", time) == FAIL)
4685 goto theend;
4686
4687 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4688 goto theend;
4689 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4690 goto theend;
4691
4692 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4693 goto theend;
4694 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4695 goto theend;
4696
4697 return item;
4698
4699theend:
4700 dict_unref(item);
4701 return NULL;
4702}
4703# else
4704 static dict_T *
4705create_readdirex_item(char_u *path, char_u *name)
4706{
4707 dict_T *item;
4708 char *p;
4709 size_t len;
4710 stat_T st;
4711 int ret, link = FALSE;
4712 varnumber_T size;
4713 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004714 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004715 struct passwd *pw;
4716 struct group *gr;
4717
4718 item = dict_alloc();
4719 if (item == NULL)
4720 return NULL;
4721 item->dv_refcount++;
4722
4723 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4724 p = alloc(len);
4725 if (p == NULL)
4726 goto theend;
4727 vim_snprintf(p, len, "%s/%s", path, name);
4728 ret = mch_lstat(p, &st);
4729 if (ret >= 0 && S_ISLNK(st.st_mode))
4730 {
4731 link = TRUE;
4732 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004733 if (ret < 0)
4734 q = (char_u*)"link";
4735
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004736 }
4737 vim_free(p);
4738
4739 if (dict_add_string(item, "name", name) == FAIL)
4740 goto theend;
4741
4742 if (ret >= 0)
4743 {
4744 size = (varnumber_T)st.st_size;
4745 if (S_ISDIR(st.st_mode))
4746 size = 0;
4747 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004748 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004749 size = -2;
4750 if (dict_add_number(item, "size", size) == FAIL)
4751 goto theend;
4752 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4753 goto theend;
4754
4755 if (link)
4756 {
4757 if (S_ISDIR(st.st_mode))
4758 q = (char_u*)"linkd";
4759 else
4760 q = (char_u*)"link";
4761 }
4762 else
4763 q = getftypest(&st);
4764 if (dict_add_string(item, "type", q) == FAIL)
4765 goto theend;
4766 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4767 goto theend;
4768
4769 pw = getpwuid(st.st_uid);
4770 if (pw == NULL)
4771 q = (char_u*)"";
4772 else
4773 q = (char_u*)pw->pw_name;
4774 if (dict_add_string(item, "user", q) == FAIL)
4775 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004776# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004777 gr = getgrgid(st.st_gid);
4778 if (gr == NULL)
4779 q = (char_u*)"";
4780 else
4781 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004782# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004783 if (dict_add_string(item, "group", q) == FAIL)
4784 goto theend;
4785 }
4786 else
4787 {
4788 if (dict_add_number(item, "size", -1) == FAIL)
4789 goto theend;
4790 if (dict_add_number(item, "time", -1) == FAIL)
4791 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004792 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004793 goto theend;
4794 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4795 goto theend;
4796 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4797 goto theend;
4798 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4799 goto theend;
4800 }
4801 return item;
4802
4803theend:
4804 dict_unref(item);
4805 return NULL;
4806}
4807# endif
4808
4809 static int
4810compare_readdirex_item(const void *p1, const void *p2)
4811{
4812 char_u *name1, *name2;
4813
Bram Moolenaard61efa52022-07-23 09:52:04 +01004814 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4815 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004816 if (readdirex_sort == READDIR_SORT_BYTE)
4817 return STRCMP(name1, name2);
4818 else if (readdirex_sort == READDIR_SORT_IC)
4819 return STRICMP(name1, name2);
4820 else
4821 return STRCOLL(name1, name2);
4822}
4823
4824 static int
4825compare_readdir_item(const void *s1, const void *s2)
4826{
4827 if (readdirex_sort == READDIR_SORT_BYTE)
4828 return STRCMP(*(char **)s1, *(char **)s2);
4829 else if (readdirex_sort == READDIR_SORT_IC)
4830 return STRICMP(*(char **)s1, *(char **)s2);
4831 else
4832 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004833}
4834#endif
4835
Bram Moolenaarda440d22016-01-16 21:27:23 +01004836#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4837/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004838 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004839 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004840 * If "withattr" is TRUE, retrieve the names and their attributes.
4841 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004842 * Return OK for success, FAIL for failure.
4843 */
4844 int
4845readdir_core(
4846 garray_T *gap,
4847 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004848 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004849 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004850 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004851 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004852{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004853 int failed = FALSE;
4854 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004855# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004856 char_u *buf;
4857 int ok;
4858 HANDLE hFind = INVALID_HANDLE_VALUE;
4859 WIN32_FIND_DATAW wfd;
4860 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004861# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004862 DIR *dirp;
4863 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004864# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004865
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004866 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004867
4868# ifdef FEAT_EVAL
4869# define FREE_ITEM(item) do { \
4870 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004871 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004872 else \
4873 vim_free(item); \
4874 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004875
4876 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004877# else
4878# define FREE_ITEM(item) vim_free(item)
4879# endif
4880
4881# ifdef MSWIN
4882 buf = alloc(MAXPATHL);
4883 if (buf == NULL)
4884 return FAIL;
4885 STRNCPY(buf, path, MAXPATHL-5);
4886 p = buf + STRLEN(buf);
4887 MB_PTR_BACK(buf, p);
4888 if (*p == '\\' || *p == '/')
4889 *p = NUL;
4890 STRCAT(p, "\\*");
4891
4892 wn = enc_to_utf16(buf, NULL);
4893 if (wn != NULL)
4894 hFind = FindFirstFileW(wn, &wfd);
4895 ok = (hFind != INVALID_HANDLE_VALUE);
4896 if (!ok)
4897 {
4898 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004899 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004900 }
4901 else
4902 {
4903 while (ok)
4904 {
4905 int ignore;
4906 void *item;
4907 WCHAR *wp;
4908
4909 wp = wfd.cFileName;
4910 ignore = wp[0] == L'.' &&
4911 (wp[1] == NUL ||
4912 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004913 if (ignore)
4914 {
4915 ok = FindNextFileW(hFind, &wfd);
4916 continue;
4917 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004918# ifdef FEAT_EVAL
4919 if (withattr)
4920 item = (void*)create_readdirex_item(&wfd);
4921 else
4922# endif
4923 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4924 if (item == NULL)
4925 {
4926 failed = TRUE;
4927 break;
4928 }
4929
4930 if (!ignore && checkitem != NULL)
4931 {
4932 int r = checkitem(context, item);
4933
4934 if (r < 0)
4935 {
4936 FREE_ITEM(item);
4937 break;
4938 }
4939 if (r == 0)
4940 ignore = TRUE;
4941 }
4942
4943 if (!ignore)
4944 {
4945 if (ga_grow(gap, 1) == OK)
4946 ((void**)gap->ga_data)[gap->ga_len++] = item;
4947 else
4948 {
4949 failed = TRUE;
4950 FREE_ITEM(item);
4951 break;
4952 }
4953 }
4954 else
4955 FREE_ITEM(item);
4956
4957 ok = FindNextFileW(hFind, &wfd);
4958 }
4959 FindClose(hFind);
4960 }
4961
4962 vim_free(buf);
4963 vim_free(wn);
4964# else // MSWIN
4965 dirp = opendir((char *)path);
4966 if (dirp == NULL)
4967 {
4968 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004969 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004970 }
4971 else
4972 {
4973 for (;;)
4974 {
4975 int ignore;
4976 void *item;
4977
4978 dp = readdir(dirp);
4979 if (dp == NULL)
4980 break;
4981 p = (char_u *)dp->d_name;
4982
4983 ignore = p[0] == '.' &&
4984 (p[1] == NUL ||
4985 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004986 if (ignore)
4987 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004988# ifdef FEAT_EVAL
4989 if (withattr)
4990 item = (void*)create_readdirex_item(path, p);
4991 else
4992# endif
4993 item = (void*)vim_strsave(p);
4994 if (item == NULL)
4995 {
4996 failed = TRUE;
4997 break;
4998 }
4999
Bram Moolenaarfe154992022-03-22 20:42:12 +00005000 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005001 {
5002 int r = checkitem(context, item);
5003
5004 if (r < 0)
5005 {
5006 FREE_ITEM(item);
5007 break;
5008 }
5009 if (r == 0)
5010 ignore = TRUE;
5011 }
5012
5013 if (!ignore)
5014 {
5015 if (ga_grow(gap, 1) == OK)
5016 ((void**)gap->ga_data)[gap->ga_len++] = item;
5017 else
5018 {
5019 failed = TRUE;
5020 FREE_ITEM(item);
5021 break;
5022 }
5023 }
5024 else
5025 FREE_ITEM(item);
5026 }
5027
5028 closedir(dirp);
5029 }
5030# endif // MSWIN
5031
5032# undef FREE_ITEM
5033
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005034 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005035 {
5036# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005037 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005038 if (withattr)
5039 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5040 compare_readdirex_item);
5041 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005042 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5043 compare_readdir_item);
5044# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005045 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005046# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005047 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005048
5049 return failed ? FAIL : OK;
5050}
5051
5052/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005053 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005054 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005055 */
5056 int
5057delete_recursive(char_u *name)
5058{
5059 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005060 int i;
5061 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005062 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005063
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005064 // A symbolic link to a directory itself is deleted, not the directory it
5065 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005066 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005067# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005068 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005069# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005070 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005071# endif
5072 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005073 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005074 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005075 if (exp == NULL)
5076 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005077 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005078 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005079 for (i = 0; i < ga.ga_len; ++i)
5080 {
5081 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5082 ((char_u **)ga.ga_data)[i]);
5083 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005084 // Remember the failure but continue deleting any further
5085 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005086 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005087 }
5088 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005089 if (mch_rmdir(exp) != 0)
5090 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005091 }
5092 else
5093 result = -1;
5094 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005095 }
5096 else
5097 result = mch_remove(name) == 0 ? 0 : -1;
5098
5099 return result;
5100}
5101#endif
5102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005106# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5107/*
5108 * Open temporary directory and take file lock to prevent
5109 * to be auto-cleaned.
5110 */
5111 static void
5112vim_opentempdir(void)
5113{
5114 DIR *dp = NULL;
5115
5116 if (vim_tempdir_dp != NULL)
5117 return;
5118
5119 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005120 if (dp == NULL)
5121 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005122
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005123 vim_tempdir_dp = dp;
5124 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005125}
5126
5127/*
5128 * Close temporary directory - it automatically release file lock.
5129 */
5130 static void
5131vim_closetempdir(void)
5132{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005133 if (vim_tempdir_dp == NULL)
5134 return;
5135
5136 closedir(vim_tempdir_dp);
5137 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005138}
5139# endif
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141/*
5142 * Delete the temp directory and all files it contains.
5143 */
5144 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005145vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005147 if (vim_tempdir == NULL)
5148 return;
5149
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005150# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005151 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005152# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005153 // remove the trailing path separator
5154 gettail(vim_tempdir)[-1] = NUL;
5155 delete_recursive(vim_tempdir);
5156 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005160 * Directory "tempdir" was created. Expand this name to a full path and put
5161 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5162 * "tempdir" must be no longer than MAXPATHL.
5163 */
5164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005166{
5167 char_u *buf;
5168
Bram Moolenaar964b3742019-05-24 18:54:09 +02005169 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005170 if (buf == NULL)
5171 return;
5172
5173 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5174 STRCPY(buf, tempdir);
5175 add_pathsep(buf);
5176 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005177# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005178 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005179# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005180 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005181}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005182#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005183
5184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 * vim_tempname(): Return a unique name that can be used for a temp file.
5186 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005187 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5188 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 *
5190 * The returned pointer is to allocated memory.
5191 * The returned pointer is NULL if no valid name was found.
5192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005194vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005195 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005200#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005201 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#else
5203 char_u itmp[TEMPNAMELEN];
5204#endif
5205
5206#ifdef TEMPDIRNAMES
5207 static char *(tempdirs[]) = {TEMPDIRNAMES};
5208 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005210 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211# endif
5212
5213 /*
5214 * This will create a directory for private use by this instance of Vim.
5215 * This is done once, and the same directory is used for all temp files.
5216 * This method avoids security problems because of symlink attacks et al.
5217 * It's also a bit faster, because we only need to check for an existing
5218 * file when creating the directory and not for each temp file.
5219 */
5220 if (vim_tempdir == NULL)
5221 {
5222 /*
5223 * Try the entries in TEMPDIRNAMES to create the temp directory.
5224 */
K.Takataeeec2542021-06-02 13:28:16 +02005225 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005227# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005228 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005229 long nr;
5230 long off;
5231# endif
5232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005233 // Expand $TMP, leave room for "/v1100000/999999999".
5234 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005236 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005239 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
Bram Moolenaareaf03392009-11-17 11:08:52 +00005241# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005242 {
5243# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 // Make sure the umask doesn't remove the executable bit.
5245 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005246 mode_t umask_save = umask(077);
5247# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005249 STRCAT(itmp, "vXXXXXX");
5250 if (mkdtemp((char *)itmp) != NULL)
5251 vim_settempdir(itmp);
5252# if defined(UNIX) || defined(VMS)
5253 (void)umask(umask_save);
5254# endif
5255 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005256# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // Get an arbitrary number of up to 6 digits. When it's
5258 // unlikely that it already exists it will be faster,
5259 // otherwise it doesn't matter. The use of mkdir() avoids any
5260 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005262 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005264 // Try up to 10000 different values until we find a name that
5265 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 for (off = 0; off < 10000L; ++off)
5267 {
5268 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005269# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaareaf03392009-11-17 11:08:52 +00005273 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5274# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 // If mkdir() does not set errno to EEXIST, check for
5276 // existing file here. There is a race condition then,
5277 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (mch_stat((char *)itmp, &st) >= 0)
5279 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005280# endif
5281# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005282 // Make sure the umask doesn't remove the executable bit.
5283 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005287# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 if (r == 0)
5291 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005292 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 break;
5294 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005295# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005296 // If the mkdir() didn't fail because the file/dir exists,
5297 // we probably can't create any dir here, try another
5298 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 break;
5302 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (vim_tempdir != NULL)
5305 break;
5306 }
5307 }
5308 }
5309
5310 if (vim_tempdir != NULL)
5311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // There is no need to check if the file exists, because we own the
5313 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5315 return vim_strsave(itmp);
5316 }
5317
5318 return NULL;
5319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005320#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar4f974752019-02-17 17:44:42 +01005322# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005323 WCHAR wszTempFile[_MAX_PATH + 1];
5324 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005325 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 char_u *retval;
5327 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005328 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005329 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005331 wcscpy(itmp, L"");
5332 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005333 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005334 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005335 wszTempFile[1] = L'\\';
5336 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005337 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005338 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005339
5340 // randomize the name to avoid collisions
5341 i = mch_get_pid() + extra_char;
5342 buf4[1] = chartab[i % 36];
5343 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005344 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005346 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005347 // GetTempFileName() will create the file, we don't want that
5348 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005350 // Backslashes in a temp file name cause problems when filtering with
5351 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005352 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005353 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005354 shname = gettail(p_sh);
5355 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5356 || strstr((char *)shname, "pwsh") != NULL ))
5357 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 for (p = retval; *p; ++p)
5359 if (*p == '\\')
5360 *p = '/';
5361 return retval;
5362
Bram Moolenaar4f974752019-02-17 17:44:42 +01005363# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005366 char_u *p;
5367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005368 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005369 p = tmpnam((char *)itmp);
5370 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return NULL;
5372# else
5373 char_u *p;
5374
5375# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // mktemp() is not working on VMS. It seems to be
5377 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 sprintf((char *)itmp, "VIM%c", extra_char);
5379 p = (char_u *)tempnam("tmp:", (char *)itmp);
5380 if (p != NULL)
5381 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 // VMS will use '.LIS' if we don't explicitly specify an extension,
5383 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 STRCPY(itmp, p);
5385 STRCAT(itmp, ".txt");
5386 free(p);
5387 }
5388 else
5389 return NULL;
5390# else
5391 STRCPY(itmp, TEMPNAME);
5392 if ((p = vim_strchr(itmp, '?')) != NULL)
5393 *p = extra_char;
5394 if (mktemp((char *)itmp) == NULL)
5395 return NULL;
5396# endif
5397# endif
5398
5399 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005400# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005401#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402}
5403
5404#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5405/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005406 * Convert all backslashes in fname to forward slashes in-place, unless when
5407 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 */
5409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *p;
5413
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005414 if (path_with_url(fname))
5415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005417 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005418 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005420 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 *p = '/';
5422}
5423#endif
5424
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005425/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005426 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5427 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5428 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * Used for autocommands and 'wildignore'.
5430 * Returns TRUE if there is a match, FALSE otherwise.
5431 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005432 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005433match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 char_u *pattern, // pattern to match with
5435 regprog_T **prog, // pre-compiled regprog or NULL
5436 char_u *fname, // full path of file name
5437 char_u *sfname, // short file name or NULL
5438 char_u *tail, // tail of path
5439 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 regmatch_T regmatch;
5442 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005445 if (prog != NULL)
5446 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005448 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 /*
5451 * Try for a match with the pattern with:
5452 * 1. the full file name, when the pattern has a '/'.
5453 * 2. the short file name, when the pattern has a '/'.
5454 * 3. the tail of the file name, when the pattern has no '/'.
5455 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005456 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 && ((allow_dirs
5458 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5459 || (sfname != NULL
5460 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005461 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 result = TRUE;
5463
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005464 if (prog != NULL)
5465 *prog = regmatch.regprog;
5466 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005467 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return result;
5469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471/*
5472 * Return TRUE if a file matches with a pattern in "list".
5473 * "list" is a comma-separated list of patterns, like 'wildignore'.
5474 * "sfname" is the short file name or NULL, "ffname" the long file name.
5475 */
5476 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005477match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005479 char_u buf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 char_u *tail;
5481 char_u *regpat;
5482 char allow_dirs;
5483 int match;
5484 char_u *p;
5485
5486 tail = gettail(sfname);
5487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 p = list;
5490 while (*p)
5491 {
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005492 copy_option_part(&p, buf, MAXPATHL, ",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5494 if (regpat == NULL)
5495 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005496 match = match_file_pat(regpat, NULL, ffname, sfname,
5497 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 vim_free(regpat);
5499 if (match)
5500 return TRUE;
5501 }
5502 return FALSE;
5503}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505/*
5506 * Convert the given pattern "pat" which has shell style wildcards in it, into
5507 * a regular expression, and return the result in allocated memory. If there
5508 * is a directory path separator to be matched, then TRUE is put in
5509 * allow_dirs, otherwise FALSE is put there -- webb.
5510 * Handle backslashes before special characters, like "\*" and "\ ".
5511 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * Returns NULL when out of memory.
5513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515file_pat_to_reg_pat(
5516 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 char_u *pat_end, // first char after pattern or NULL
5518 char *allow_dirs, // Result passed back out in here
5519 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u *endp;
5523 char_u *reg_pat;
5524 char_u *p;
5525 int i;
5526 int nested = 0;
5527 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 if (allow_dirs != NULL)
5530 *allow_dirs = FALSE;
5531 if (pat_end == NULL)
5532 pat_end = pat + STRLEN(pat);
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 for (p = pat; p < pat_end; p++)
5535 {
5536 switch (*p)
5537 {
5538 case '*':
5539 case '.':
5540 case ',':
5541 case '{':
5542 case '}':
5543 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 break;
5546#ifdef BACKSLASH_IN_FILENAME
5547 case '\\':
5548 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005549 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551#endif
5552 default:
5553 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 ++p;
5557 ++size;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 break;
5560 }
5561 }
5562 reg_pat = alloc(size + 1);
5563 if (reg_pat == NULL)
5564 return NULL;
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 if (pat[0] == '*')
5569 while (pat[0] == '*' && pat < pat_end - 1)
5570 pat++;
5571 else
5572 reg_pat[i++] = '^';
5573 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005574 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 while (endp - pat > 0 && *endp == '*')
5577 endp--;
5578 add_dollar = FALSE;
5579 }
5580 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5581 {
5582 switch (*p)
5583 {
5584 case '*':
5585 reg_pat[i++] = '.';
5586 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005588 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 break;
5590 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 case '~':
5592 reg_pat[i++] = '\\';
5593 reg_pat[i++] = *p;
5594 break;
5595 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 reg_pat[i++] = '.';
5597 break;
5598 case '\\':
5599 if (p[1] == NUL)
5600 break;
5601#ifdef BACKSLASH_IN_FILENAME
5602 if (!no_bslash)
5603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005604 // translate:
5605 // "\x" to "\\x" e.g., "dir\file"
5606 // "\*" to "\\.*" e.g., "dir\*.c"
5607 // "\?" to "\\." e.g., "dir\??.c"
5608 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5610 && p[1] != '+')
5611 {
5612 reg_pat[i++] = '[';
5613 reg_pat[i++] = '\\';
5614 reg_pat[i++] = '/';
5615 reg_pat[i++] = ']';
5616 if (allow_dirs != NULL)
5617 *allow_dirs = TRUE;
5618 break;
5619 }
5620 }
5621#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005622 // Undo escaping from ExpandEscape():
5623 // foo\?bar -> foo?bar
5624 // foo\%bar -> foo%bar
5625 // foo\,bar -> foo,bar
5626 // foo\ bar -> foo bar
5627 // Don't unescape \, * and others that are also special in a
5628 // regexp.
5629 // An escaped { must be unescaped since we use magic not
5630 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (*++p == '?'
5632#ifdef BACKSLASH_IN_FILENAME
5633 && no_bslash
5634#endif
5635 )
5636 reg_pat[i++] = '?';
5637 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005638 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005639 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005640 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005641 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5642 {
5643 reg_pat[i++] = '\\';
5644 reg_pat[i++] = '{';
5645 p += 2;
5646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 else
5648 {
5649 if (allow_dirs != NULL && vim_ispathsep(*p)
5650#ifdef BACKSLASH_IN_FILENAME
5651 && (!no_bslash || *p != '\\')
5652#endif
5653 )
5654 *allow_dirs = TRUE;
5655 reg_pat[i++] = '\\';
5656 reg_pat[i++] = *p;
5657 }
5658 break;
5659#ifdef BACKSLASH_IN_FILENAME
5660 case '/':
5661 reg_pat[i++] = '[';
5662 reg_pat[i++] = '\\';
5663 reg_pat[i++] = '/';
5664 reg_pat[i++] = ']';
5665 if (allow_dirs != NULL)
5666 *allow_dirs = TRUE;
5667 break;
5668#endif
5669 case '{':
5670 reg_pat[i++] = '\\';
5671 reg_pat[i++] = '(';
5672 nested++;
5673 break;
5674 case '}':
5675 reg_pat[i++] = '\\';
5676 reg_pat[i++] = ')';
5677 --nested;
5678 break;
5679 case ',':
5680 if (nested)
5681 {
5682 reg_pat[i++] = '\\';
5683 reg_pat[i++] = '|';
5684 }
5685 else
5686 reg_pat[i++] = ',';
5687 break;
5688 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005689 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005691 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 *allow_dirs = TRUE;
5693 reg_pat[i++] = *p;
5694 break;
5695 }
5696 }
5697 if (add_dollar)
5698 reg_pat[i++] = '$';
5699 reg_pat[i] = NUL;
5700 if (nested != 0)
5701 {
5702 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005703 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005705 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005706 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 return reg_pat;
5709}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005710
5711#if defined(EINTR) || defined(PROTO)
5712/*
5713 * Version of read() that retries when interrupted by EINTR (possibly
5714 * by a SIGWINCH).
5715 */
5716 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005718{
5719 long ret;
5720
5721 for (;;)
5722 {
5723 ret = vim_read(fd, buf, bufsize);
5724 if (ret >= 0 || errno != EINTR)
5725 break;
5726 }
5727 return ret;
5728}
5729
5730/*
5731 * Version of write() that retries when interrupted by EINTR (possibly
5732 * by a SIGWINCH).
5733 */
5734 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005736{
5737 long ret = 0;
5738 long wlen;
5739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 // Repeat the write() so long it didn't fail, other than being interrupted
5741 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005742 while (ret < (long)bufsize)
5743 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005744 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005745 if (wlen < 0)
5746 {
5747 if (errno != EINTR)
5748 break;
5749 }
5750 else
5751 ret += wlen;
5752 }
5753 return ret;
5754}
5755#endif