blob: ac46a28239261956cf3e15e542819f293c381577 [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 Moolenaarbc385a12023-06-17 15:35:03 +0100221#ifdef FEAT_CRYPT
Bram Moolenaar438d0c52023-06-17 15:00:27 +0100222 int eof = FALSE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +0100223#endif
Christian Brabandtaae58342023-04-23 17:50:22 +0100224#ifdef FEAT_SODIUM
225 int may_need_lseek = FALSE;
226#endif
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200227
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100228 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200229
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100230 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232 /*
233 * If there is no file name yet, use the one for the read file.
234 * BF_NOTEDITED is set to reflect this.
235 * Don't do this for a read from a filter.
236 * Only do this when 'cpoptions' contains the 'f' flag.
237 */
238 if (curbuf->b_ffname == NULL
239 && !filtering
240 && fname != NULL
241 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
242 && !(flags & READ_DUMMY))
243 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000244 if (set_rw_fname(fname, sfname) == FAIL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100245 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
247
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100248 // Remember the initial values of curbuf, curbuf->b_ffname and
249 // curbuf->b_fname to detect whether they are altered as a result of
250 // executing nasty autocommands. Also check if "fname" and "sfname"
251 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200252 old_curbuf = curbuf;
253 old_b_ffname = curbuf->b_ffname;
254 old_b_fname = curbuf->b_fname;
255 using_b_ffname = (fname == curbuf->b_ffname)
256 || (sfname == curbuf->b_ffname);
257 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200258
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100259 // After reading a file the cursor line changes but we don't want to
260 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000261 ex_no_reprint = TRUE;
262
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100263 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000264 need_fileinfo = FALSE;
265
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 /*
267 * For Unix: Use the short file name whenever possible.
268 * Avoids problems with networks and when directory names are changed.
269 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
270 * another directory, which we don't detect.
271 */
272 if (sfname == NULL)
273 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200274#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 fname = sfname;
276#endif
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 /*
279 * The BufReadCmd and FileReadCmd events intercept the reading process by
280 * executing the associated commands instead.
281 */
282 if (!filtering && !read_stdin && !read_buffer)
283 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100284 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100286 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
288 curbuf->b_op_start.col = 0;
289
290 if (newfile)
291 {
292 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
293 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200294 {
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100295 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200297 if (aborting())
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100298 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200300 // The BufReadCmd code usually uses ":read" to get the text and
301 // perhaps ":file" to change the buffer name. But we should
302 // consider this to work like ":edit", thus reset the
303 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
304 // same file.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100305 if (retval == OK)
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200306 curbuf->b_flags &= ~BF_NOTEDITED;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100307 goto theend;
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 }
310 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
311 FALSE, NULL, eap))
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef FEAT_EVAL
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100314 retval = aborting() ? FAIL : OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#else
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100316 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100318 goto theend;
319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100321 curbuf->b_op_start = orig_start;
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100322
323 if (flags & READ_NOFILE)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100324 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100325 // Return NOTDONE instead of FAIL so that BufEnter can be triggered
326 // and other operations don't fail.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100327 retval = NOTDONE;
328 goto theend;
329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100333 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100335 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000337 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200339 size_t namelen = STRLEN(fname);
340
341 // If the name is too long we might crash further on, quit here.
342 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000343 {
344 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
345 msg_end();
346 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100347 goto theend;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000348 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200349
350 // If the name ends in a path separator, we can't open it. Check here,
351 // because reading the file may actually work, but then creating the
352 // swap file may destroy it! Reported on MS-DOS and Win 95.
353 if (after_pathsep(fname, fname + namelen))
354 {
355 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
356 msg_end();
357 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100358 retval = NOTDONE;
359 goto theend;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 }
362
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200363 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100365#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200366 /*
367 * On Unix it is possible to read a directory, so we have to
368 * check for it before the mch_open().
369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100371 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
372 && !S_ISFIFO(perm) // ... or fifo
373 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000374# ifdef OPEN_CHR_FILES
375 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100376 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 )
379 {
380 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100381 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200382 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100383 retval = NOTDONE;
384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 else
386 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
387 msg_end();
388 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100389 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200391#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100392#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000393 /*
394 * MS-Windows allows opening a device, but we will probably get stuck
395 * trying to read it.
396 */
397 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
398 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000399 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000400 msg_end();
401 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100402 goto theend;
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000403 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000404#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200405 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000406
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100407 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200408 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410 /*
411 * When opening a new file we take the readonly flag from the file.
412 * Default is r/w, can be set to r/o below.
413 * Don't reset it when in readonly mode
414 * Only set/reset b_p_ro when BF_CHECK_RO is set.
415 */
416 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000417 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 curbuf->b_p_ro = FALSE;
419
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200420 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100422 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (mch_stat((char *)fname, &st) >= 0)
424 {
425 buf_store_time(curbuf, &st, fname);
426 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100427 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200428 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#ifdef UNIX
430 /*
431 * Use the protection bits of the original file for the swap file.
432 * This makes it possible for others to read the name of the
433 * edited file from the swapfile, but only if they can read the
434 * edited file.
435 * Remove the "write" and "execute" bits for group and others
436 * (they must not write the swapfile).
437 * Add the "read" and "write" bits for the user, otherwise we may
438 * not be able to write to the file ourselves.
439 * Setting the bits is done below, after creating the swap file.
440 */
441 swap_mode = (st.st_mode & 0644) | 0600;
442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#ifdef VMS
444 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000445 curbuf->b_fab_rat = st.st_fab_rat;
446 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
448 }
449 else
450 {
451 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100452 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100454 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 curbuf->b_orig_size = 0;
456 curbuf->b_orig_mode = 0;
457 }
458
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100459 // Reset the "new file" flag. It will be set again below when the
460 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
462 }
463
464/*
465 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100466 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 */
468 file_readonly = FALSE;
469 if (read_stdin)
470 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100471#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 setmode(0, O_BINARY);
474#endif
475 }
476 else if (!read_buffer)
477 {
478#ifdef USE_MCH_ACCESS
479 if (
480# ifdef UNIX
481 !(perm & 0222) ||
482# endif
483 mch_access((char *)fname, W_OK))
484 file_readonly = TRUE;
485 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
486#else
487 if (!newfile
488 || readonlymode
489 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
490 {
491 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
494 }
495#endif
496 }
497
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
500#ifndef UNIX
501 int isdir_f;
502#endif
503 msg_scroll = msg_save;
504#ifndef UNIX
505 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100506 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 */
508 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100509 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (isdir_f)
511 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200512 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100513 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
515 else
516#endif
517 if (newfile)
518 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200519 if (perm < 0
520#ifdef ENOENT
521 && errno == ENOENT
522#endif
523 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 /*
526 * Set the 'new-file' flag, so that when the file has
527 * been created by someone else, a ":w" will complain.
528 */
529 curbuf->b_flags |= BF_NEW;
530
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // Create a swap file now, so that other Vims are warned
532 // that we are editing this file. Don't do this for a
533 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000538 if (curbuf != old_curbuf
539 || (using_b_ffname
540 && (old_b_ffname != curbuf->b_ffname))
541 || (using_b_fname
542 && (old_b_fname != curbuf->b_fname)))
543 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000544 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100545 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000546 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000547 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000548 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200549 filemess(curbuf, sfname,
550 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000551 else
552 filemess(curbuf, sfname,
553 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100555 // Even though this is a new file, it might have been
556 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 check_marks_read();
558#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100559 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200560 if (eap != NULL)
561 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
563 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100564 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 save_file_ff(curbuf);
566
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100567#if defined(FEAT_EVAL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100568 if (!aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100570 retval = OK; // a new file is not an error
571 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573 else
574 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000575 filemess(curbuf, sfname, (char_u *)(
576# ifdef EFBIG
577 (errno == EFBIG) ? _("[File too big]") :
578# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200579# ifdef EOVERFLOW
580 (errno == EOVERFLOW) ? _("[File too big]") :
581# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000582 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100583 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585 }
586
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100587 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589
590 /*
591 * Only set the 'ro' flag for readonly files the first time they are
592 * loaded. Help files always get readonly mode
593 */
594 if ((check_readonly && file_readonly) || curbuf->b_help)
595 curbuf->b_p_ro = TRUE;
596
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000597 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100599 // Don't change 'eol' if reading from buffer as it will already be
600 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000601 if (!read_buffer)
602 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100603 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100604 curbuf->b_start_eof = FALSE;
605 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000606 curbuf->b_start_eol = TRUE;
607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000609 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100612 // Create a swap file now, so that other Vims are warned that we are
613 // editing this file.
614 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000618 if (!read_stdin && (curbuf != old_curbuf
619 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
620 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
621 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000622 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000623 if (!read_buffer)
624 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100625 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000629 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
630 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100631 {
632 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
633
634 /*
635 * If the group-read bit is set but not the world-read bit, then
636 * the group must be equal to the group of the original file. If
637 * we can't make that happen then reset the group-read bit. This
638 * avoids making the swap file readable to more users when the
639 * primary group of the user is too permissive.
640 */
641 if ((swap_mode & 044) == 040)
642 {
643 stat_T swap_st;
644
645 if (mch_stat((char *)swap_fname, &swap_st) >= 0
646 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200647# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100648 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200649 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200650# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200651 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100652 swap_mode &= 0600;
653 }
654
655 (void)mch_setperm(swap_fname, (long)swap_mode);
656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657#endif
658 }
659
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200660 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (swap_exists_action == SEA_QUIT)
662 {
663 if (!read_buffer && !read_stdin)
664 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100665 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670 /*
671 * Set '[ mark to the line above where the lines go (line 1 if zero).
672 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100673 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
675 curbuf->b_op_start.col = 0;
676
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100677 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
678 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
679 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (!read_buffer)
682 {
683 int m = msg_scroll;
684 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 /*
687 * The file must be closed again, the autocommands may want to change
688 * the file before reading it.
689 */
690 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100691 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 /*
694 * The output from the autocommands should not overwrite anything and
695 * should not be overwritten: Set msg_scroll, restore its value if no
696 * output was done.
697 */
698 msg_scroll = TRUE;
699 if (filtering)
700 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
701 FALSE, curbuf, eap);
702 else if (read_stdin)
703 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
704 FALSE, curbuf, eap);
705 else if (newfile)
706 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
707 FALSE, curbuf, eap);
708 else
709 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
710 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100712 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
713 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
714 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100715 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (msg_scrolled == n)
718 msg_scroll = m;
719
720#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100721 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 --no_wait_return;
724 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100725 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100726 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728#endif
729 /*
730 * Don't allow the autocommands to change the current buffer.
731 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000732 *
733 * Don't allow the autocommands to change the buffer name either
734 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 */
736 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000737 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
738 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
740 {
741 --no_wait_return;
742 msg_scroll = msg_save;
743 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000744 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000746 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100747 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100748 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100752 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
754
755 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
756 {
757 /*
758 * Show the user that we are busy reading the input. Sometimes this
759 * may take a while. When reading from stdin another program may
760 * still be running, don't move the cursor to the last line, unless
761 * always using the GUI.
762 */
763 if (read_stdin)
764 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100765 if (!is_not_a_term())
766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200768# ifdef VIMDLL
769 if (!gui.in_use)
770# endif
771 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100775 if (gui.in_use && !gui.dying && !gui.starting)
776 {
Amon Sha10197932022-02-21 15:07:12 +0000777 // make a copy, gui_write() may try to change it
778 p = vim_strsave((char_u *)_("Reading from stdin..."));
779 if (p != NULL)
780 {
781 gui_write(p, (int)STRLEN(p));
782 vim_free(p);
783 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788 else if (!read_buffer)
789 filemess(curbuf, sfname, (char_u *)"", 0);
790 }
791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 /*
795 * Set linecnt now, before the "retry" caused by a wrong guess for
796 * fileformat, and after the autocommands, which may change them.
797 */
798 linecnt = curbuf->b_ml.ml_line_count;
799
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000801 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000802 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000803 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000804 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000805 curbuf->b_bad_char = eap->bad_char;
806 }
807 else
808 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000811 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 */
813 if (eap != NULL && eap->force_enc != 0)
814 {
815 fenc = enc_canonize(eap->cmd + eap->force_enc);
816 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000817 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
819 else if (curbuf->b_p_bin)
820 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 fenc_alloced = FALSE;
823 }
824 else if (curbuf->b_help)
825 {
826 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000827 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100829 // Help files are either utf-8 or latin1. Try utf-8 first, if this
830 // fails it must be latin1.
831 // Always do this when 'encoding' is "utf-8". Otherwise only do
832 // this when needed to avoid [converted] remarks all the time.
833 // It is needed when the first line contains non-ASCII characters.
834 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 fenc = (char_u *)"latin1";
836 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000837 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000839 fc = fname[STRLEN(fname) - 1];
840 if (TOLOWER_ASC(fc) == 'x')
841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // Read the first line (and a bit more). Immediately rewind to
843 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100844 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200845 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000846 for (p = firstline; p < firstline + len; ++p)
847 if (*p >= 0x80)
848 {
849 c = TRUE;
850 break;
851 }
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854
855 if (c)
856 {
857 fenc_next = fenc;
858 fenc = (char_u *)"utf-8";
859
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100860 // When the file is utf-8 but a character doesn't fit in
861 // 'encoding' don't retry. In help text editing utf-8 bytes
862 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000863 if (!enc_utf8)
864 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 fenc_alloced = FALSE;
867 }
868 else if (*p_fencs == NUL)
869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100870 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 fenc_alloced = FALSE;
872 }
873 else
874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200876 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 /*
880 * Jump back here to retry reading the file in different ways.
881 * Reasons to retry:
882 * - encoding conversion failed: try another one from "fenc_next"
883 * - BOM detected and fenc was set, need to setup conversion
884 * - "fileformat" check failed: try another
885 *
886 * Variables set for special retry actions:
887 * "file_rewind" Rewind the file to start reading it again.
888 * "advance_fenc" Advance "fenc" using "fenc_next".
889 * "skip_read" Re-use already read bytes (BOM detected).
890 * "did_iconv" iconv() conversion failed, try 'charconvert'.
891 * "keep_fileformat" Don't reset "fileformat".
892 *
893 * Other status indicators:
894 * "tmpname" When != NULL did conversion with 'charconvert'.
895 * Output file has to be deleted afterwards.
896 * "iconv_fd" When != -1 did conversion with iconv().
897 */
898retry:
899
900 if (file_rewind)
901 {
902 if (read_buffer)
903 {
904 read_buf_lnum = 1;
905 read_buf_col = 0;
906 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200907 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100909 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 error = TRUE;
911 goto failed;
912 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100913 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200915 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000917 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000920 curbuf->b_start_bomb = FALSE;
921 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000922 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924
925 /*
926 * When retrying with another "fenc" and the first time "fileformat"
927 * will be reset.
928 */
929 if (keep_fileformat)
930 keep_fileformat = FALSE;
931 else
932 {
933 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000936 try_unix = try_dos = try_mac = FALSE;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100939 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100941 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945
Bram Moolenaar13505972019-01-24 15:04:48 +0100946#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (iconv_fd != (iconv_t)-1)
948 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100949 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 iconv_close(iconv_fd);
951 iconv_fd = (iconv_t)-1;
952 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 if (advance_fenc)
956 {
957 /*
958 * Try the next entry in 'fileencodings'.
959 */
960 advance_fenc = FALSE;
961
962 if (eap != NULL && eap->force_enc != 0)
963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100964 // Conversion given with "++cc=" wasn't possible, read
965 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000967 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (fenc_alloced)
969 vim_free(fenc);
970 fenc = (char_u *)"";
971 fenc_alloced = FALSE;
972 }
973 else
974 {
975 if (fenc_alloced)
976 vim_free(fenc);
977 if (fenc_next != NULL)
978 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200979 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 else
982 {
983 fenc = (char_u *)"";
984 fenc_alloced = FALSE;
985 }
986 }
987 if (tmpname != NULL)
988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100989 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100990 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992 }
993
994 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000995 * Conversion may be required when the encoding of the file is different
996 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 */
998 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000999 converted = need_conversion(fenc);
1000 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {
1002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 // "ucs-bom" means we need to check the first bytes of the file
1004 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1006 fio_flags = FIO_UCSBOM;
1007
1008 /*
1009 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1010 * done. This is handled below after read(). Prepare the
1011 * fio_flags to avoid having to parse the string each time.
1012 * Also check for Unicode to Latin1 conversion, because iconv()
1013 * appears not to handle this correctly. This works just like
1014 * conversion to UTF-8 except how the resulting character is put in
1015 * the buffer.
1016 */
1017 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1018 fio_flags = get_fio_flags(fenc);
1019
Bram Moolenaar4f974752019-02-17 17:44:42 +01001020#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 /*
1022 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1023 * is handled with MultiByteToWideChar().
1024 */
1025 if (fio_flags == 0)
1026 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaar13505972019-01-24 15:04:48 +01001029#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001030 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (fio_flags == 0)
1032 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar13505972019-01-24 15:04:48 +01001035#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 /*
1037 * Try using iconv() if we can't convert internally.
1038 */
1039 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001040# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 )
1044 iconv_fd = (iconv_t)my_iconv_open(
1045 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 /*
1050 * Use the 'charconvert' expression when conversion is required
1051 * and we can't do it internally or with iconv().
1052 */
1053 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001054 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001055# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 )
1059 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001060# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001062# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Skip conversion when it's already done (retry for wrong
1064 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (tmpname == NULL)
1066 {
1067 tmpname = readfile_charconvert(fname, fenc, &fd);
1068 if (tmpname == NULL)
1069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 advance_fenc = TRUE;
1072 if (fd < 0)
1073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001075 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 error = TRUE;
1077 goto failed;
1078 }
1079 goto retry;
1080 }
1081 }
1082 }
1083 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001087#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 )
1091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // Conversion wanted but we can't.
1093 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 advance_fenc = TRUE;
1095 goto retry;
1096 }
1097 }
1098 }
1099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001100 // Set "can_retry" when it's possible to rewind the file and try with
1101 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1102 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001103 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105 if (!skip_read)
1106 {
1107 linerest = 0;
1108 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001109 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 skip_count = lines_to_skip;
1111 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001113#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001114 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1115 && curbuf->b_ffname != NULL
1116 && curbuf->b_p_udf
1117 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001118 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001119 && !read_stdin
1120 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001121 if (read_undo_file)
1122 sha256_start(&sha_ctx);
1123#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001124#ifdef FEAT_CRYPT
1125 if (curbuf->b_cryptstate != NULL)
1126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // Need to free the state, but keep the key, don't want to ask for
1128 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001129 crypt_free_state(curbuf->b_cryptstate);
1130 curbuf->b_cryptstate = NULL;
1131 }
1132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134
1135 while (!error && !got_int)
1136 {
1137 /*
1138 * We allocate as much space for the file as we can get, plus
1139 * space for the old line plus room for one terminating NUL.
1140 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001141 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001143 if (!skip_read)
1144 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001145#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001147#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001148 // Use buffer >= 64K. Add linerest to double the size if the
1149 // line gets very long, to avoid a lot of copying. But don't
1150 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001151 size = 0x10000L + linerest;
1152 if (size > 0x100000L)
1153 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001154#endif
1155 }
1156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001157 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001158 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 size = 1;
1163 }
1164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 if (!skip_read)
1167 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001168 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001170 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 FALSE)) != NULL)
1172 break;
1173 }
1174 if (new_buffer == NULL)
1175 {
1176 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1177 error = TRUE;
1178 break;
1179 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1182 vim_free(buffer);
1183 buffer = new_buffer;
1184 ptr = buffer + linerest;
1185 line_start = buffer;
1186
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001187 // May need room to translate into.
1188 // For iconv() we don't really know the required space, use a
1189 // factor ICONV_MULT.
1190 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1191 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1192 // become up to 4 bytes, size must be multiple of 2
1193 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1194 // multiple of 2
1195 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1196 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001197 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001198#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (iconv_fd != (iconv_t)-1)
1200 size = size / ICONV_MULT;
1201 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (fio_flags & FIO_LATIN1)
1204 size = size / 2;
1205 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1206 size = (size * 2 / 3) & ~1;
1207 else if (fio_flags & FIO_UCS4)
1208 size = (size * 2 / 3) & ~3;
1209 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001210 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001211#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001213 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001214#endif
1215#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001217 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (conv_restlen > 0)
1221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 mch_memmove(ptr, conv_rest, conv_restlen);
1224 ptr += conv_restlen;
1225 size -= conv_restlen;
1226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 if (read_buffer)
1229 {
1230 /*
1231 * Read bytes from curbuf. Used for converting text read
1232 * from stdin.
1233 */
1234 if (read_buf_lnum > from)
1235 size = 0;
1236 else
1237 {
1238 int n, ni;
1239 long tlen;
1240
1241 tlen = 0;
1242 for (;;)
1243 {
1244 p = ml_get(read_buf_lnum) + read_buf_col;
1245 n = (int)STRLEN(p);
1246 if ((int)tlen + n + 1 > size)
1247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001248 // Filled up to "size", append partial line.
1249 // Change NL to NUL to reverse the effect done
1250 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001251 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 for (ni = 0; ni < n; ++ni)
1253 {
1254 if (p[ni] == NL)
1255 ptr[tlen++] = NUL;
1256 else
1257 ptr[tlen++] = p[ni];
1258 }
1259 read_buf_col += n;
1260 break;
1261 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001262
1263 // Append whole line and new-line. Change NL
1264 // to NUL to reverse the effect done below.
1265 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001267 if (p[ni] == NL)
1268 ptr[tlen++] = NUL;
1269 else
1270 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001272 ptr[tlen++] = NL;
1273 read_buf_col = 0;
1274 if (++read_buf_lnum > from)
1275 {
1276 // When the last line didn't have an
1277 // end-of-line don't add it now either.
1278 if (!curbuf->b_p_eol)
1279 --tlen;
1280 size = tlen;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001281#ifdef FEAT_CRYPT
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001282 eof = TRUE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001283#endif
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001284 break;
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288 }
1289 else
1290 {
1291 /*
1292 * Read bytes from the file.
1293 */
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001294#ifdef FEAT_SODIUM
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001295 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001296 //
1297 // Sodium encryption requires a fixed block size to
1298 // successfully decrypt. However, unfortunately the file
1299 // header size changes between xchacha20 and xchacha20v2 by
1300 // 'add_len' bytes.
1301 // So we will now read the maximum header size + encryption
1302 // metadata, but after determining to read an xchacha20
1303 // encrypted file, we have to rewind the file descriptor by
1304 // 'add_len' bytes in the second round.
1305 //
1306 // Be careful with changing it, it needs to stay the same
1307 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001308 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001309 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001310 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001311 size = WRITEBUFSIZE + crypt_get_max_header_len()
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001312 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1313 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001314 may_need_lseek = TRUE;
1315 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001316
Christian Brabandtaae58342023-04-23 17:50:22 +01001317 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1318 && crypt_method_is_sodium(
1319 curbuf->b_cryptstate->method_nr)))
1320 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001321 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001322 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1323 // description above)
1324 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1325 && !eof && may_need_lseek)
1326 {
1327 lseek(fd, crypt_get_header_len(
1328 curbuf->b_cryptstate->method_nr)
1329 - crypt_get_max_header_len(), SEEK_CUR);
1330 may_need_lseek = FALSE;
1331 }
1332 }
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001333#endif
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001334 long read_size = size;
1335 size = read_eintr(fd, ptr, read_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001336 filesize_count += size;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001337#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001338 // hit end of file
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001339 eof = (size < read_size || filesize_count == filesize_disk);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001343#ifdef FEAT_CRYPT
1344 /*
1345 * At start of file: Check for magic number of encryption.
1346 */
1347 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001348 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001349 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1350 &filesize, newfile, sfname,
1351 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001352# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001353 if (curbuf->b_cryptstate != NULL
1354 && !crypt_works_inplace(curbuf->b_cryptstate))
1355 // reading undo file requires crypt_decode_inplace()
1356 read_undo_file = FALSE;
1357# endif
1358 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001359 /*
1360 * Decrypt the read bytes. This is done before checking for
1361 * EOF because the crypt layer may be buffering.
1362 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001363 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1364 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001365 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001366# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001367 if (crypt_works_inplace(curbuf->b_cryptstate))
1368 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001369# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001370 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1371 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001372# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001373 }
1374 else
1375 {
1376 char_u *newptr = NULL;
1377 int decrypted_size;
1378
1379 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001380 curbuf->b_cryptstate, ptr, size,
1381 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001382
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001383 if (decrypted_size < 0)
1384 {
1385 // error message already given
1386 error = TRUE;
1387 vim_free(newptr);
1388 break;
1389 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001390 // If the crypt layer is buffering, not producing
1391 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001392 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001393 continue;
1394
1395 if (linerest == 0)
1396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 // Simple case: reuse returned buffer (may be
1398 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001399 new_buffer = newptr;
1400 }
1401 else
1402 {
1403 long_u new_size;
1404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001405 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001406 new_size = (long_u)(decrypted_size + linerest + 1);
1407 new_buffer = lalloc(new_size, FALSE);
1408 if (new_buffer == NULL)
1409 {
1410 do_outofmem_msg(new_size);
1411 error = TRUE;
1412 break;
1413 }
1414
1415 mch_memmove(new_buffer, buffer, linerest);
1416 if (newptr != NULL)
1417 mch_memmove(new_buffer + linerest, newptr,
1418 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001419 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001420 }
1421
1422 if (new_buffer != NULL)
1423 {
1424 vim_free(buffer);
1425 buffer = new_buffer;
1426 new_buffer = NULL;
1427 line_start = buffer;
1428 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001429 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001430 }
1431 size = decrypted_size;
1432 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001433# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001434 }
1435#endif
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (size <= 0)
1438 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001442 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001443 /*
1444 * Reached end-of-file but some trailing bytes could
1445 * not be converted. Truncated file?
1446 */
1447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001448 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001449 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001450#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001451 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001452#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001453 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001454 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001455 if (can_retry)
1456 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001457 if (conv_error == 0)
1458 conv_error = curbuf->b_ml.ml_line_count
1459 - linecnt + 1;
1460 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001461 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001462 else if (illegal_byte == 0)
1463 illegal_byte = curbuf->b_ml.ml_line_count
1464 - linecnt + 1;
1465 if (bad_char_behavior == BAD_DROP)
1466 {
1467 *(ptr - conv_restlen) = NUL;
1468 conv_restlen = 0;
1469 }
1470 else
1471 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 // Replace the trailing bytes with the replacement
1473 // character if we were converting; if we weren't,
1474 // leave the UTF8 checking code to do it, as it
1475 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001476 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001477#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001478 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001479#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001480 ))
1481 {
1482 while (conv_restlen > 0)
1483 {
1484 *(--ptr) = bad_char_behavior;
1485 --conv_restlen;
1486 }
1487 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001488 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001489#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001490 if (iconv_fd != (iconv_t)-1)
1491 {
1492 iconv_close(iconv_fd);
1493 iconv_fd = (iconv_t)-1;
1494 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001495#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001496 }
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 skip_read = FALSE;
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 /*
1503 * At start of file (or after crypt magic number): Check for BOM.
1504 * Also check for a BOM for other Unicode encodings, but not after
1505 * converting with 'charconvert' or when a BOM has already been
1506 * found.
1507 */
1508 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001509#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001510 || (cryptkey != NULL
1511 && filesize == crypt_get_header_len(
1512 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 )
1515 && (fio_flags == FIO_UCSBOM
1516 || (!curbuf->b_p_bomb
1517 && tmpname == NULL
1518 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1519 {
1520 char_u *ccname;
1521 int blen;
1522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (size < 2 || curbuf->b_p_bin)
1525 ccname = NULL;
1526 else
1527 ccname = check_for_bom(ptr, size, &blen,
1528 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1529 if (ccname != NULL)
1530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001531 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 filesize += blen;
1533 size -= blen;
1534 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001535 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001538 curbuf->b_start_bomb = TRUE;
1539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
1541
1542 if (fio_flags == FIO_UCSBOM)
1543 {
1544 if (ccname == NULL)
1545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 advance_fenc = TRUE;
1548 }
1549 else
1550 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001551 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (fenc_alloced)
1553 vim_free(fenc);
1554 fenc = ccname;
1555 fenc_alloced = FALSE;
1556 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 skip_read = TRUE;
1559 goto retry;
1560 }
1561 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001563 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001564 ptr -= conv_restlen;
1565 size += conv_restlen;
1566 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 /*
1568 * Break here for a read error or end-of-file.
1569 */
1570 if (size <= 0)
1571 break;
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar13505972019-01-24 15:04:48 +01001574#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (iconv_fd != (iconv_t)-1)
1576 {
1577 /*
1578 * Attempt conversion of the read bytes to 'encoding' using
1579 * iconv().
1580 */
1581 const char *fromp;
1582 char *top;
1583 size_t from_size;
1584 size_t to_size;
1585
1586 fromp = (char *)ptr;
1587 from_size = size;
1588 ptr += size;
1589 top = (char *)ptr;
1590 to_size = real_size - size;
1591
1592 /*
1593 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001594 * another conversion. Except for when there is no
1595 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001597 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1598 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1600 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001601 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001602 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001603 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001604 if (conv_error == 0)
1605 conv_error = readfile_linenr(linecnt,
1606 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001608 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001609 ++fromp;
1610 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001611 if (bad_char_behavior == BAD_KEEP)
1612 {
1613 *top++ = *(fromp - 1);
1614 --to_size;
1615 }
1616 else if (bad_char_behavior != BAD_DROP)
1617 {
1618 *top++ = bad_char_behavior;
1619 --to_size;
1620 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 if (from_size > 0)
1624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001625 // Some remaining characters, keep them for the next
1626 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1628 conv_restlen = (int)from_size;
1629 }
1630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 line_start = ptr - linerest;
1633 mch_memmove(line_start, buffer, (size_t)linerest);
1634 size = (long)((char_u *)top - ptr);
1635 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar4f974752019-02-17 17:44:42 +01001638#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (fio_flags & FIO_CODEPAGE)
1640 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001641 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001642 WCHAR ucs2buf[3];
1643 int ucs2len;
1644 int codepage = FIO_GET_CP(fio_flags);
1645 int bytelen;
1646 int found_bad;
1647 char replstr[2];
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 /*
1650 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001651 * a codepage, using standard MS-Windows functions. This
1652 * requires two steps:
1653 * 1. convert from 'fileencoding' to ucs-2
1654 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001656 * Because there may be illegal bytes AND an incomplete byte
1657 * sequence at the end, we may have to do the conversion one
1658 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001662 if (bad_char_behavior > 0)
1663 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665 replstr[0] = '?';
1666 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 * Move the bytes to the end of the buffer, so that we have
1670 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001672 src = ptr + real_size - size;
1673 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001675 /*
1676 * Do the conversion.
1677 */
1678 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001679 while (size > 0)
1680 {
1681 found_bad = FALSE;
1682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001683# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684 if (codepage == CP_UTF8)
1685 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001686 // Handle CP_UTF8 input ourselves to be able to handle
1687 // trailing bytes properly.
1688 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001689 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001690 if (bytelen > size)
1691 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001692 // Only got some bytes of a character. Normally
1693 // it's put in "conv_rest", but if it's too long
1694 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001695 if (bytelen <= CONV_RESTLEN)
1696 break;
1697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001698 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001699 bytelen = size;
1700 found_bad = TRUE;
1701 }
1702 else
1703 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001704 int u8c = utf_ptr2char(src);
1705
Bram Moolenaar86e01082005-12-29 22:45:34 +00001706 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001707 found_bad = TRUE;
1708 ucs2buf[0] = u8c;
1709 ucs2len = 1;
1710 }
1711 }
1712 else
1713# endif
1714 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001715 // We don't know how long the byte sequence is, try
1716 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001717 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1718 ++bytelen)
1719 {
1720 ucs2len = MultiByteToWideChar(codepage,
1721 MB_ERR_INVALID_CHARS,
1722 (LPCSTR)src, bytelen,
1723 ucs2buf, 3);
1724 if (ucs2len > 0)
1725 break;
1726 }
1727 if (ucs2len == 0)
1728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // If we have only one byte then it's probably an
1730 // incomplete byte sequence. Otherwise discard
1731 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001732 if (size == 1)
1733 break;
1734 found_bad = TRUE;
1735 bytelen = 1;
1736 }
1737 }
1738
1739 if (!found_bad)
1740 {
1741 int i;
1742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001743 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001744 if (enc_utf8)
1745 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001747 for (i = 0; i < ucs2len; ++i)
1748 dst += utf_char2bytes(ucs2buf[i], dst);
1749 }
1750 else
1751 {
1752 BOOL bad = FALSE;
1753 int dstlen;
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // From UCS-2 to "enc_codepage". If the
1756 // conversion uses the default character "?",
1757 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001758 dstlen = WideCharToMultiByte(enc_codepage, 0,
1759 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001760 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001761 replstr, &bad);
1762 if (bad)
1763 found_bad = TRUE;
1764 else
1765 dst += dstlen;
1766 }
1767 }
1768
1769 if (found_bad)
1770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001772 if (can_retry)
1773 goto rewind_retry;
1774 if (conv_error == 0)
1775 conv_error = readfile_linenr(linecnt, ptr, dst);
1776 if (bad_char_behavior != BAD_DROP)
1777 {
1778 if (bad_char_behavior == BAD_KEEP)
1779 {
1780 mch_memmove(dst, src, bytelen);
1781 dst += bytelen;
1782 }
1783 else
1784 *dst++ = bad_char_behavior;
1785 }
1786 }
1787
1788 src += bytelen;
1789 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001791
1792 if (size > 0)
1793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001795 mch_memmove(conv_rest, src, size);
1796 conv_restlen = size;
1797 }
1798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001799 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001800 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001803#endif
1804#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (fio_flags & FIO_MACROMAN)
1806 {
1807 /*
1808 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001809 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001811 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (fio_flags != 0)
1817 {
1818 int u8c;
1819 char_u *dest;
1820 char_u *tail = NULL;
1821
1822 /*
1823 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1824 * "enc_utf8" not set: Convert Unicode to Latin1.
1825 * Go from end to start through the buffer, because the number
1826 * of bytes may increase.
1827 * "dest" points to after where the UTF-8 bytes go, "p" points
1828 * to after the next character to convert.
1829 */
1830 dest = ptr + real_size;
1831 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1832 {
1833 p = ptr + size;
1834 if (fio_flags == FIO_UTF8)
1835 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 tail = ptr + size - 1;
1838 while (tail > ptr && (*tail & 0xc0) == 0x80)
1839 --tail;
1840 if (tail + utf_byte2len(*tail) <= ptr + size)
1841 tail = NULL;
1842 else
1843 p = tail;
1844 }
1845 }
1846 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001848 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 p = ptr + (size & ~1);
1850 if (size & 1)
1851 tail = p;
1852 if ((fio_flags & FIO_UTF16) && p > ptr)
1853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001854 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (fio_flags & FIO_ENDIAN_L)
1856 {
1857 u8c = (*--p << 8);
1858 u8c += *--p;
1859 }
1860 else
1861 {
1862 u8c = *--p;
1863 u8c += (*--p << 8);
1864 }
1865 if (u8c >= 0xd800 && u8c <= 0xdbff)
1866 tail = p;
1867 else
1868 p += 2;
1869 }
1870 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001873 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 p = ptr + (size & ~3);
1875 if (size & 3)
1876 tail = p;
1877 }
1878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001879 // If there is a trailing incomplete sequence move it to
1880 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (tail != NULL)
1882 {
1883 conv_restlen = (int)((ptr + size) - tail);
1884 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1885 size -= conv_restlen;
1886 }
1887
1888
1889 while (p > ptr)
1890 {
1891 if (fio_flags & FIO_LATIN1)
1892 u8c = *--p;
1893 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1894 {
1895 if (fio_flags & FIO_ENDIAN_L)
1896 {
1897 u8c = (*--p << 8);
1898 u8c += *--p;
1899 }
1900 else
1901 {
1902 u8c = *--p;
1903 u8c += (*--p << 8);
1904 }
1905 if ((fio_flags & FIO_UTF16)
1906 && u8c >= 0xdc00 && u8c <= 0xdfff)
1907 {
1908 int u16c;
1909
1910 if (p == ptr)
1911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (can_retry)
1914 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001915 if (conv_error == 0)
1916 conv_error = readfile_linenr(linecnt,
1917 ptr, p);
1918 if (bad_char_behavior == BAD_DROP)
1919 continue;
1920 if (bad_char_behavior != BAD_KEEP)
1921 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
1923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001924 // found second word of double-word, get the first
1925 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (fio_flags & FIO_ENDIAN_L)
1927 {
1928 u16c = (*--p << 8);
1929 u16c += *--p;
1930 }
1931 else
1932 {
1933 u16c = *--p;
1934 u16c += (*--p << 8);
1935 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001936 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1937 + (u8c & 0x3ff);
1938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (u16c < 0xd800 || u16c > 0xdbff)
1941 {
1942 if (can_retry)
1943 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001944 if (conv_error == 0)
1945 conv_error = readfile_linenr(linecnt,
1946 ptr, p);
1947 if (bad_char_behavior == BAD_DROP)
1948 continue;
1949 if (bad_char_behavior != BAD_KEEP)
1950 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 }
1954 else if (fio_flags & FIO_UCS4)
1955 {
1956 if (fio_flags & FIO_ENDIAN_L)
1957 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001958 u8c = (unsigned)*--p << 24;
1959 u8c += (unsigned)*--p << 16;
1960 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 u8c += *--p;
1962 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001966 u8c += (unsigned)*--p << 8;
1967 u8c += (unsigned)*--p << 16;
1968 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 if (*--p < 0x80)
1974 u8c = *p;
1975 else
1976 {
1977 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001978 p -= len;
1979 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (len == 0)
1981 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001982 // Not a valid UTF-8 character, retry with
1983 // another fenc when possible, otherwise just
1984 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (can_retry)
1986 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001987 if (conv_error == 0)
1988 conv_error = readfile_linenr(linecnt,
1989 ptr, p);
1990 if (bad_char_behavior == BAD_DROP)
1991 continue;
1992 if (bad_char_behavior != BAD_KEEP)
1993 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 dest -= utf_char2len(u8c);
2000 (void)utf_char2bytes(u8c, dest);
2001 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 --dest;
2005 if (u8c >= 0x100)
2006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // character doesn't fit in latin1, retry with
2008 // another fenc when possible, otherwise just
2009 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002010 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002012 if (conv_error == 0)
2013 conv_error = readfile_linenr(linecnt, ptr, p);
2014 if (bad_char_behavior == BAD_DROP)
2015 ++dest;
2016 else if (bad_char_behavior == BAD_KEEP)
2017 *dest = u8c;
2018 else if (eap != NULL && eap->bad_char != 0)
2019 *dest = bad_char_behavior;
2020 else
2021 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 else
2024 *dest = u8c;
2025 }
2026 }
2027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 line_start = dest - linerest;
2030 mch_memmove(line_start, buffer, (size_t)linerest);
2031 size = (long)((ptr + real_size) - dest);
2032 ptr = dest;
2033 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002034 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002036 int incomplete_tail = FALSE;
2037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002039 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002041 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002042 int l;
2043
2044 if (todo <= 0)
2045 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (*p >= 0x80)
2047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002048 // A length of 1 means it's an illegal byte. Accept
2049 // an incomplete character at the end though, the next
2050 // read() will get the next bytes, we'll check it
2051 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002052 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002053 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 // Avoid retrying with a different encoding when
2056 // a truncated file is more likely, or attempting
2057 // to read the rest of an incomplete sequence when
2058 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002059 if (p > ptr || filesize > 0)
2060 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // Incomplete byte sequence, move it to conv_rest[]
2062 // and try to read the rest of it, unless we've
2063 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002064 if (p > ptr)
2065 {
2066 conv_restlen = todo;
2067 mch_memmove(conv_rest, p, conv_restlen);
2068 size -= conv_restlen;
2069 break;
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002072 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // Illegal byte. If we can try another encoding
2075 // do that, unless at EOF where a truncated
2076 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002077 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002078 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002079#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002081 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2082 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002083#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002085 if (conv_error == 0 && illegal_byte == 0)
2086 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002089 if (bad_char_behavior == BAD_DROP)
2090 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002091 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002092 --p;
2093 --size;
2094 }
2095 else if (bad_char_behavior != BAD_KEEP)
2096 *p = bad_char_behavior;
2097 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002098 else
2099 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002102 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002104 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002107#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002108 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002110 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002112#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002113 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002114 advance_fenc = TRUE;
2115 file_rewind = TRUE;
2116 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002120 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 filesize += size;
2122
2123 /*
2124 * when reading the first part of a file: guess EOL type
2125 */
2126 if (fileformat == EOL_UNKNOWN)
2127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (try_dos || try_unix)
2130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002132 if (try_mac)
2133 try_mac = 1;
2134
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 for (p = ptr; p < ptr + size; ++p)
2136 {
2137 if (*p == NL)
2138 {
2139 if (!try_unix
2140 || (try_dos && p > ptr && p[-1] == CAR))
2141 fileformat = EOL_DOS;
2142 else
2143 fileformat = EOL_UNIX;
2144 break;
2145 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002146 else if (*p == CAR && try_mac)
2147 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (fileformat == EOL_UNIX && try_mac)
2152 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002153 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 try_mac = 1;
2155 try_unix = 1;
2156 for (; p >= ptr && *p != CAR; p--)
2157 ;
2158 if (p >= ptr)
2159 {
2160 for (p = ptr; p < ptr + size; ++p)
2161 {
2162 if (*p == NL)
2163 try_unix++;
2164 else if (*p == CAR)
2165 try_mac++;
2166 }
2167 if (try_mac > try_unix)
2168 fileformat = EOL_MAC;
2169 }
2170 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002171 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002172 // Looking for CR but found no end-of-line markers at
2173 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002174 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002177 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (fileformat == EOL_UNKNOWN && try_mac)
2179 fileformat = EOL_MAC;
2180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002181 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (fileformat == EOL_UNKNOWN)
2183 fileformat = default_fileformat();
2184
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002185 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 set_fileformat(fileformat, OPT_LOCAL);
2188 }
2189 }
2190
2191 /*
2192 * This loop is executed once for every character read.
2193 * Keep it fast!
2194 */
2195 if (fileformat == EOL_MAC)
2196 {
2197 --ptr;
2198 while (++ptr, --size >= 0)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if ((c = *ptr) != NUL && c != CAR && c != NL)
2202 continue;
2203 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 else
2208 {
2209 if (skip_count == 0)
2210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002211 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 len = (colnr_T) (ptr - line_start + 1);
2213 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2214 {
2215 error = TRUE;
2216 break;
2217 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002218#ifdef FEAT_PERSISTENT_UNDO
2219 if (read_undo_file)
2220 sha256_update(&sha_ctx, line_start, len);
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ++lnum;
2223 if (--read_count == 0)
2224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002225 error = TRUE; // break loop
2226 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 break;
2228 }
2229 }
2230 else
2231 --skip_count;
2232 line_start = ptr + 1;
2233 }
2234 }
2235 }
2236 else
2237 {
2238 --ptr;
2239 while (++ptr, --size >= 0)
2240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 continue;
2243 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 else
2246 {
2247 if (skip_count == 0)
2248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 len = (colnr_T)(ptr - line_start + 1);
2251 if (fileformat == EOL_DOS)
2252 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002253 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002255 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 ptr[-1] = NUL;
2257 --len;
2258 }
2259 /*
2260 * Reading in Dos format, but no CR-LF found!
2261 * When 'fileformats' includes "unix", delete all
2262 * the lines read so far and start all over again.
2263 * Otherwise give an error message later.
2264 */
2265 else if (ff_error != EOL_DOS)
2266 {
2267 if ( try_unix
2268 && !read_stdin
2269 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002270 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2271 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002274 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 set_fileformat(EOL_UNIX, OPT_LOCAL);
2276 file_rewind = TRUE;
2277 keep_fileformat = TRUE;
2278 goto retry;
2279 }
2280 ff_error = EOL_DOS;
2281 }
2282 }
2283 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2284 {
2285 error = TRUE;
2286 break;
2287 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002288#ifdef FEAT_PERSISTENT_UNDO
2289 if (read_undo_file)
2290 sha256_update(&sha_ctx, line_start, len);
2291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 ++lnum;
2293 if (--read_count == 0)
2294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 error = TRUE; // break loop
2296 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 break;
2298 }
2299 }
2300 else
2301 --skip_count;
2302 line_start = ptr + 1;
2303 }
2304 }
2305 }
2306 linerest = (long)(ptr - line_start);
2307 ui_breakcheck();
2308 }
2309
2310failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (error && read_count == 0)
2313 error = FALSE;
2314
K.Takata3af98212022-11-01 20:36:19 +00002315 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2316 // In old days the file length was in sector count and the CTRL-Z the
2317 // marker where the file really ended. Assuming we write it to a file
2318 // system that keeps file length properly the CTRL-Z should be dropped.
2319 // Set the 'endoffile' option so the user can decide what to write later.
2320 // In Unix format the CTRL-Z is just another character.
2321 if (linerest != 0
2322 && !curbuf->b_p_bin
2323 && fileformat == EOL_DOS
2324 && ptr[-1] == Ctrl_Z)
2325 {
2326 ptr--;
2327 linerest--;
2328 if (set_options)
2329 curbuf->b_p_eof = TRUE;
2330 }
2331
2332 // If we get EOF in the middle of a line, note the fact by resetting
2333 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (!error
2335 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002336 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 curbuf->b_p_eol = FALSE;
2341 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002342 len = (colnr_T)(ptr - line_start + 1);
2343 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 error = TRUE;
2345 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002346 {
2347#ifdef FEAT_PERSISTENT_UNDO
2348 if (read_undo_file)
2349 sha256_update(&sha_ctx, line_start, len);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002355 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002359 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002360 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002361 crypt_free_state(curbuf->b_cryptstate);
2362 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002363 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002364 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2365 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // Don't set cryptkey to NULL, it's used below as a flag that
2367 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#endif
2369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // If editing a new file: set 'fenc' for the current buffer.
2371 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002372 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002374 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (fenc_alloced)
2376 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002377#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#endif
2381
2382 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002383 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002384#ifdef HAVE_FD_CLOEXEC
2385 else
2386 {
2387 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002388
Bram Moolenaarf05da212009-11-17 16:13:15 +00002389 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002390 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002391 }
2392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 vim_free(buffer);
2394
2395#ifdef HAVE_DUP
2396 if (read_stdin)
2397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002398 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002400 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402#endif
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (tmpname != NULL)
2405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 vim_free(tmpname);
2408 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002409 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 /*
2412 * In recovery mode everything but autocommands is skipped.
2413 */
2414 if (!recoverymode)
2415 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002416 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2418 {
2419#ifdef FEAT_NETBEANS_INTG
2420 netbeansFireChanges = 0;
2421#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002422 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#ifdef FEAT_NETBEANS_INTG
2424 netbeansFireChanges = 1;
2425#endif
2426 --linecnt;
2427 }
2428 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2429 if (filesize == 0)
2430 linecnt = 0;
2431 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002432 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002433 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002434#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 // After reading the text into the buffer the diff info needs to
2436 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002437 diff_invalidate(curbuf);
2438#endif
2439#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002440 // All folds in the window are invalid now. Mark them for update
2441 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002442 foldUpdateAll(curwin);
2443#endif
2444 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002445 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 appended_lines_mark(from, linecnt);
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448#ifndef ALWAYS_USE_GUI
2449 /*
2450 * If we were reading from the same terminal as where messages go,
2451 * the screen will have been messed up.
2452 * Switch on raw mode now and clear the screen.
2453 */
2454 if (read_stdin)
2455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002456 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 starttermcap();
2458 screenclear();
2459 }
2460#endif
2461
2462 if (got_int)
2463 {
2464 if (!(flags & READ_DUMMY))
2465 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002466 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002468 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 msg_scroll = msg_save;
2471#ifdef FEAT_VIMINFO
2472 check_marks_read();
2473#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002474 retval = OK; // an interrupt isn't really an error
2475 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 if (!filtering && !(flags & READ_DUMMY))
2479 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002480 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 c = FALSE;
2482
2483#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002484 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 STRCAT(IObuff, _("[fifo]"));
2487 c = TRUE;
2488 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002489 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 STRCAT(IObuff, _("[socket]"));
2492 c = TRUE;
2493 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002494# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002495 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002496 {
2497 STRCAT(IObuff, _("[character special]"));
2498 c = TRUE;
2499 }
2500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501#endif
2502 if (curbuf->b_p_ro)
2503 {
2504 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2505 c = TRUE;
2506 }
2507 if (read_no_eol_lnum)
2508 {
2509 msg_add_eol();
2510 c = TRUE;
2511 }
2512 if (ff_error == EOL_DOS)
2513 {
2514 STRCAT(IObuff, _("[CR missing]"));
2515 c = TRUE;
2516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (split)
2518 {
2519 STRCAT(IObuff, _("[long lines split]"));
2520 c = TRUE;
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (notconverted)
2523 {
2524 STRCAT(IObuff, _("[NOT converted]"));
2525 c = TRUE;
2526 }
2527 else if (converted)
2528 {
2529 STRCAT(IObuff, _("[converted]"));
2530 c = TRUE;
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#ifdef FEAT_CRYPT
2533 if (cryptkey != NULL)
2534 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002535 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 c = TRUE;
2537 }
2538#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002539 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002541 sprintf((char *)IObuff + STRLEN(IObuff),
2542 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 c = TRUE;
2544 }
2545 else if (illegal_byte > 0)
2546 {
2547 sprintf((char *)IObuff + STRLEN(IObuff),
2548 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2549 c = TRUE;
2550 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002551 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 STRCAT(IObuff, _("[READ ERRORS]"));
2554 c = TRUE;
2555 }
2556 if (msg_add_fileformat(fileformat))
2557 c = TRUE;
2558#ifdef FEAT_CRYPT
2559 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002560 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002561 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 else
2563#endif
2564 msg_add_lines(c, (long)linecnt, filesize);
2565
Bram Moolenaard23a8232018-02-10 18:45:26 +01002566 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 msg_scrolled_ign = TRUE;
2568#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 // Don't show the message when reading stdin, it would end up in a
2570 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (read_stdin || read_buffer)
2572 p = msg_may_trunc(FALSE, IObuff);
2573 else
2574#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002575 {
2576 if (msg_col > 0)
2577 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002578 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002581 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002582 // Need to repeat the message after redrawing when:
2583 // - When reading from stdin (the screen will be cleared next).
2584 // - When restart_edit is set (otherwise there will be a delay
2585 // before redrawing).
2586 // - When the screen was scrolled but there is no wait-return
2587 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002588 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 msg_scrolled_ign = FALSE;
2590 }
2591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002594 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002595 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 curbuf->b_p_ro = TRUE;
2597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002598 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 /*
2601 * In Ex mode: cursor at last new line.
2602 * Otherwise: cursor at first new line.
2603 */
2604 if (exmode_active)
2605 curwin->w_cursor.lnum = from + linecnt;
2606 else
2607 curwin->w_cursor.lnum = from + 1;
2608 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002609 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaare1004402020-10-24 20:49:43 +02002611 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002612 {
2613 // Set '[ and '] marks to the newly read lines.
2614 curbuf->b_op_start.lnum = from + 1;
2615 curbuf->b_op_start.col = 0;
2616 curbuf->b_op_end.lnum = from + linecnt;
2617 curbuf->b_op_end.col = 0;
2618 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002619
Bram Moolenaar4f974752019-02-17 17:44:42 +01002620#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002621 /*
2622 * Work around a weird problem: When a file has two links (only
2623 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002624 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002625 * It's correct again after reading the file, thus reset the timestamp
2626 * here.
2627 */
2628 if (newfile && !read_stdin && !read_buffer
2629 && mch_stat((char *)fname, &st) >= 0)
2630 {
2631 buf_store_time(curbuf, &st, fname);
2632 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002633 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002634 }
2635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 msg_scroll = msg_save;
2638
2639#ifdef FEAT_VIMINFO
2640 /*
2641 * Get the marks before executing autocommands, so they can be used there.
2642 */
2643 check_marks_read();
2644#endif
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002647 * We remember if the last line of the read didn't have
2648 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2649 * or writing the read again with 'binary' on. The latter is required
2650 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002652 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002654 // When reloading a buffer put the cursor at the first line that is
2655 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002656 if (flags & READ_KEEP_UNDO)
2657 u_find_first_changed();
2658
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002659#ifdef FEAT_PERSISTENT_UNDO
2660 /*
2661 * When opening a new file locate undo info and read it.
2662 */
2663 if (read_undo_file)
2664 {
2665 char_u hash[UNDO_HASH_SIZE];
2666
2667 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002668 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002669 }
2670#endif
2671
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002672 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 int m = msg_scroll;
2675 int n = msg_scrolled;
2676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002677 // Save the fileformat now, otherwise the buffer will be considered
2678 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002679 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 save_file_ff(curbuf);
2681
2682 /*
2683 * The output from the autocommands should not overwrite anything and
2684 * should not be overwritten: Set msg_scroll, restore its value if no
2685 * output was done.
2686 */
2687 msg_scroll = TRUE;
2688 if (filtering)
2689 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2690 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002691 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2694 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002695 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2696 /*
2697 * EVENT_FILETYPE was not triggered but the buffer already has a
2698 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2699 */
2700 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2701 TRUE, curbuf);
2702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 else
2704 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2705 FALSE, NULL, eap);
2706 if (msg_scrolled == n)
2707 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002708# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002709 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002710 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002714 if (!(recoverymode && error))
2715 retval = OK;
2716
2717theend:
2718 if (curbuf->b_ml.ml_mfp != NULL
2719 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2720 // OK to sync the swap file now
2721 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2722
2723 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724}
2725
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002726#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002727/*
2728 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2729 * which is the name of files used for process substitution output by
2730 * some shells on some operating systems, e.g., bash on SunOS.
2731 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2732 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002733 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002734is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002735{
2736 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2737 && VIM_ISDIGIT(fname[8])
2738 && *skipdigits(fname + 9) == NUL
2739 && (fname[9] != NUL
2740 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2741}
2742#endif
2743
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002744/*
2745 * From the current line count and characters read after that, estimate the
2746 * line number where we are now.
2747 * Used for error messages that include a line number.
2748 */
2749 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002750readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002751 linenr_T linecnt, // line count before reading more bytes
2752 char_u *p, // start of more bytes read
2753 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002754{
2755 char_u *s;
2756 linenr_T lnum;
2757
2758 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2759 for (s = p; s < endp; ++s)
2760 if (*s == '\n')
2761 ++lnum;
2762 return lnum;
2763}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765/*
Rob Pilling8196e942022-02-11 15:12:10 +00002766 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002767 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 * Returns OK or FAIL.
2769 */
2770 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002771prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
Bram Moolenaar13505972019-01-24 15:04:48 +01002773 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (eap->cmd == NULL)
2775 return FAIL;
2776
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002777 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2778 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002779 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002780 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002781
2782 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002783 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002784 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 return OK;
2786}
2787
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002788/*
2789 * Set default or forced 'fileformat' and 'binary'.
2790 */
2791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002792set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002793{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002795 if (set_options)
2796 {
2797 if (eap != NULL && eap->force_ff != 0)
2798 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2799 else if (*p_ffs != NUL)
2800 set_fileformat(default_fileformat(), OPT_LOCAL);
2801 }
2802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002803 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002804 if (eap != NULL && eap->force_bin != 0)
2805 {
2806 int oldval = curbuf->b_p_bin;
2807
2808 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2809 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2810 }
2811}
2812
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002813/*
2814 * Set forced 'fileencoding'.
2815 */
2816 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002817set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002818{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002819 if (eap->force_enc == 0)
2820 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002821
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002822 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2823
2824 if (fenc != NULL)
2825 set_string_option_direct((char_u *)"fenc", -1,
2826 fenc, OPT_FREE|OPT_LOCAL, 0);
2827 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002828}
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830/*
2831 * Find next fileencoding to use from 'fileencodings'.
2832 * "pp" points to fenc_next. It's advanced to the next item.
2833 * When there are no more items, an empty string is returned and *pp is set to
2834 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002835 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2836 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 */
2838 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002839next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 char_u *p;
2842 char_u *r;
2843
Bram Moolenaarf077db22019-08-13 00:18:24 +02002844 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (**pp == NUL)
2846 {
2847 *pp = NULL;
2848 return (char_u *)"";
2849 }
2850 p = vim_strchr(*pp, ',');
2851 if (p == NULL)
2852 {
2853 r = enc_canonize(*pp);
2854 *pp += STRLEN(*pp);
2855 }
2856 else
2857 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002858 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 *pp = p + 1;
2860 if (r != NULL)
2861 {
2862 p = enc_canonize(r);
2863 vim_free(r);
2864 r = p;
2865 }
2866 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002867 if (r != NULL)
2868 *alloced = TRUE;
2869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002871 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 r = (char_u *)"";
2873 *pp = NULL;
2874 }
2875 return r;
2876}
2877
Bram Moolenaar13505972019-01-24 15:04:48 +01002878#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879/*
2880 * Convert a file with the 'charconvert' expression.
2881 * This closes the file which is to be read, converts it and opens the
2882 * resulting file for reading.
2883 * Returns name of the resulting converted file (the caller should delete it
2884 * after reading it).
2885 * Returns NULL if the conversion failed ("*fdp" is not set) .
2886 */
2887 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002888readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002889 char_u *fname, // name of input file
2890 char_u *fenc, // converted from
2891 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002894 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002896 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002898 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else
2900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002901 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 *fdp = -1;
2903 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2904 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002905 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2907 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002908 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910
2911 if (errmsg != NULL)
2912 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 // Don't use emsg(), it breaks mappings, the retry with
2914 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002915 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (tmpname != NULL)
2917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002918 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002919 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921 }
2922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002923 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (*fdp < 0)
2925 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2926
2927 return tmpname;
2928}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#endif
2930
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002931#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002933 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2935 * *filesizep are updated.
2936 * Return the (new) encryption key, NULL for no encryption.
2937 */
2938 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002939check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940 char_u *cryptkey, // previous encryption key or NULL
2941 char_u *ptr, // pointer to read bytes
2942 long *sizep, // length of read bytes
2943 off_T *filesizep, // nr of bytes used from file
2944 int newfile, // editing a new buffer
2945 char_u *fname, // file name to display
2946 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002948 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002949 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002950
2951 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002953 // Mark the buffer as read-only until the decryption has taken place.
2954 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002955 curbuf->b_p_ro = TRUE;
2956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002958 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002959 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002964 crypt_check_swapfile_curbuf();
2965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
2967 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002968 // When newfile is TRUE, store the typed key in the 'key'
2969 // option and don't free it. bf needs hash of the key saved.
2970 // Don't ask for the key again when first time Enter was hit.
2971 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002973 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002974 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002975 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002976 *did_ask = TRUE;
2977
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002978 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (cryptkey != NULL && *cryptkey == NUL)
2980 {
2981 if (cryptkey != curbuf->b_p_key)
2982 vim_free(cryptkey);
2983 cryptkey = NULL;
2984 }
2985 }
2986 }
2987
2988 if (cryptkey != NULL)
2989 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002990 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002991
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002992 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002993 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002994 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002995 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002996
2997 curbuf->b_cryptstate = crypt_create_from_header(
2998 method, cryptkey, ptr);
2999 crypt_set_cm_option(curbuf, method);
3000
3001 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003002 *filesizep += header_len;
3003 *sizep -= header_len;
3004 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003006 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003007 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
3009 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003010 // When starting to edit a new file which does not have encryption, clear
3011 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003012 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003013 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 return cryptkey;
3016}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003017#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003020 * Return TRUE if a file appears to be read-only from the file permissions.
3021 */
3022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003023check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003024 char_u *fname, // full path to file
3025 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003026{
3027#ifndef USE_MCH_ACCESS
3028 int fd = 0;
3029#endif
3030
3031 return (
3032#ifdef USE_MCH_ACCESS
3033# ifdef UNIX
3034 (perm & 0222) == 0 ||
3035# endif
3036 mch_access((char *)fname, W_OK)
3037#else
3038 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3039 ? TRUE : (close(fd), FALSE)
3040#endif
3041 );
3042}
3043
Bram Moolenaara7870192019-02-14 12:56:36 +01003044#if defined(HAVE_FSYNC) || defined(PROTO)
3045/*
3046 * Call fsync() with Mac-specific exception.
3047 * Return fsync() result: zero for success.
3048 */
3049 int
3050vim_fsync(int fd)
3051{
3052 int r;
3053
3054# ifdef MACOS_X
3055 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003056 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003057# endif
3058 r = fsync(fd);
3059 return r;
3060}
3061#endif
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003064 * Set the name of the current buffer. Use when the buffer doesn't have a
3065 * name and a ":r" or ":w" command with a file name is used.
3066 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003067 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003068set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003069{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003070 buf_T *buf = curbuf;
3071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003072 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003073 if (curbuf->b_p_bl)
3074 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3075 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003076#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003077 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003078 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003079#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003080 if (curbuf != buf)
3081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003082 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003083 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003084 return FAIL;
3085 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003086
3087 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3088 curbuf->b_flags |= BF_NOTEDITED;
3089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003090 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003091 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3092 if (curbuf->b_p_bl)
3093 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003094#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003095 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003096 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003097#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003099 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003100 if (*curbuf->b_p_ft == NUL)
3101 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003102 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003103 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003104 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003105 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003106
3107 return OK;
3108}
3109
3110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * Put file name into IObuff with quotes.
3112 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003113 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 if (fname == NULL)
3117 fname = (char_u *)"-stdin-";
3118 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3119 IObuff[0] = '"';
3120 STRCAT(IObuff, "\" ");
3121}
3122
3123/*
3124 * Append message for text mode to IObuff.
3125 * Return TRUE if something appended.
3126 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130#ifndef USE_CRNL
3131 if (eol_type == EOL_DOS)
3132 {
3133 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3134 return TRUE;
3135 }
3136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (eol_type == EOL_MAC)
3138 {
3139 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3140 return TRUE;
3141 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003142#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (eol_type == EOL_UNIX)
3144 {
3145 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3146 return TRUE;
3147 }
3148#endif
3149 return FALSE;
3150}
3151
3152/*
3153 * Append line and character count to IObuff.
3154 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003155 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003156msg_add_lines(
3157 int insert_space,
3158 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003159 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 char_u *p;
3162
3163 p = IObuff + STRLEN(IObuff);
3164
3165 if (insert_space)
3166 *p++ = ' ';
3167 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003168 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003169 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else
3171 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003172 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003174 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003175 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003176 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178}
3179
3180/*
3181 * Append message for missing line separator to IObuff.
3182 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3187}
3188
Bram Moolenaar473952e2019-09-28 16:30:04 +02003189 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003190time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
ichizokdef69df2021-10-15 17:23:12 +01003192 return
3193#ifdef ST_MTIM_NSEC
3194 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
ichizokdef69df2021-10-15 17:23:12 +01003196#if defined(__linux__) || defined(MSWIN)
3197 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3198 // the seconds. Since the roundoff is done when flushing the inode, the
3199 // time may change unexpectedly by one second!!!
3200 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3201#else
3202 (long)st->st_mtime != mtime
3203#endif
3204 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003208 * Return TRUE if file encoding "fenc" requires conversion from or to
3209 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003211 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003212need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003214 int same_encoding;
3215 int enc_flags;
3216 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003218 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003219 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003220 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003221 fenc_flags = 0;
3222 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003223 else
3224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003225 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3226 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003227 enc_flags = get_fio_flags(p_enc);
3228 fenc_flags = get_fio_flags(fenc);
3229 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3230 }
3231 if (same_encoding)
3232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 // Specified encoding matches with 'encoding'. This requires
3234 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003235 return enc_unicode != 0;
3236 }
3237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003238 // Encodings differ. However, conversion is not needed when 'enc' is any
3239 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003240 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241}
3242
3243/*
3244 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3245 * internal conversion.
3246 * if "ptr" is an empty string, use 'encoding'.
3247 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003248 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003249get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int prop;
3252
3253 if (*ptr == NUL)
3254 ptr = p_enc;
3255
3256 prop = enc_canon_props(ptr);
3257 if (prop & ENC_UNICODE)
3258 {
3259 if (prop & ENC_2BYTE)
3260 {
3261 if (prop & ENC_ENDIAN_L)
3262 return FIO_UCS2 | FIO_ENDIAN_L;
3263 return FIO_UCS2;
3264 }
3265 if (prop & ENC_4BYTE)
3266 {
3267 if (prop & ENC_ENDIAN_L)
3268 return FIO_UCS4 | FIO_ENDIAN_L;
3269 return FIO_UCS4;
3270 }
3271 if (prop & ENC_2WORD)
3272 {
3273 if (prop & ENC_ENDIAN_L)
3274 return FIO_UTF16 | FIO_ENDIAN_L;
3275 return FIO_UTF16;
3276 }
3277 return FIO_UTF8;
3278 }
3279 if (prop & ENC_LATIN1)
3280 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003281 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return 0;
3283}
3284
Bram Moolenaar473952e2019-09-28 16:30:04 +02003285#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286/*
3287 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3288 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3289 * Used for conversion between 'encoding' and 'fileencoding'.
3290 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003291 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003292get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 int cp;
3295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (!enc_utf8 && enc_codepage <= 0)
3298 return 0;
3299
3300 cp = encname2codepage(ptr);
3301 if (cp == 0)
3302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (STRCMP(ptr, "utf-8") == 0)
3305 cp = CP_UTF8;
3306 else
3307# endif
3308 return 0;
3309 }
3310 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3311}
3312#endif
3313
Bram Moolenaar473952e2019-09-28 16:30:04 +02003314#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315/*
3316 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3317 * needed for the internal conversion to/from utf-8 or latin1.
3318 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003319 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003320get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3323 && (enc_canon_props(ptr) & ENC_MACROMAN))
3324 return FIO_MACROMAN;
3325 return 0;
3326}
3327#endif
3328
3329/*
3330 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3331 * "size" must be at least 2.
3332 * Return the name of the encoding and set "*lenp" to the length.
3333 * Returns NULL when no BOM found.
3334 */
3335 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003336check_for_bom(
3337 char_u *p,
3338 long size,
3339 int *lenp,
3340 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 char *name = NULL;
3343 int len = 2;
3344
3345 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003346 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003348 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 len = 3;
3350 }
3351 else if (p[0] == 0xff && p[1] == 0xfe)
3352 {
3353 if (size >= 4 && p[2] == 0 && p[3] == 0
3354 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 len = 4;
3358 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003359 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003360 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003361 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003362 // utf-16le is preferred, it also works for ucs-2le text
3363 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 else if (p[0] == 0xfe && p[1] == 0xff
3366 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003368 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003369 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003370 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003371 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3375 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003377 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 len = 4;
3379 }
3380
3381 *lenp = len;
3382 return (char_u *)name;
3383}
3384
3385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 * Try to find a shortname by comparing the fullname with the current
3387 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003388 * Returns "full_path" or pointer into "full_path" if shortened.
3389 */
3390 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003392{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003393 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003394 char_u *p = full_path;
3395
Bram Moolenaard9462e32011-04-11 21:35:11 +02003396 dirname = alloc(MAXPATHL);
3397 if (dirname == NULL)
3398 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003399 if (mch_dirname(dirname, MAXPATHL) == OK)
3400 {
3401 p = shorten_fname(full_path, dirname);
3402 if (p == NULL || *p == NUL)
3403 p = full_path;
3404 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003405 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003406 return p;
3407}
3408
3409/*
3410 * Try to find a shortname by comparing the fullname with the current
3411 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * Returns NULL if not shorter name possible, pointer into "full_path"
3413 * otherwise.
3414 */
3415 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003416shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 int len;
3419 char_u *p;
3420
3421 if (full_path == NULL)
3422 return NULL;
3423 len = (int)STRLEN(dir_name);
3424 if (fnamencmp(dir_name, full_path, len) == 0)
3425 {
3426 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003427#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003429 * MS-Windows: when a file is in the root directory, dir_name will end
3430 * in a slash, since C: by itself does not define a specific dir. In
3431 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 if (!((len > 2) && (*(p - 2) == ':')))
3434#endif
3435 {
3436 if (vim_ispathsep(*p))
3437 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003438#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 else
3440 p = NULL;
3441#endif
3442 }
3443 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003444#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 /*
3446 * When using a file in the current drive, remove the drive name:
3447 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3448 * a floppy from "A:\dir" to "B:\dir".
3449 */
3450 else if (len > 3
3451 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3452 && full_path[1] == ':'
3453 && vim_ispathsep(full_path[2]))
3454 p = full_path + 2;
3455#endif
3456 else
3457 p = NULL;
3458 return p;
3459}
3460
3461/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003462 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 * When "force" is TRUE: Use full path from now on for files currently being
3464 * edited, both for file name and swap file name. Try to shorten the file
3465 * names a bit, if safe to do so.
3466 * When "force" is FALSE: Only try to shorten absolute file names.
3467 * For buffers that have buftype "nofile" or "scratch": never change the file
3468 * name.
3469 */
3470 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003471shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3472{
3473 char_u *p;
3474
3475 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003476 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003477 && !path_with_url(buf->b_fname)
3478 && (force
3479 || buf->b_sfname == NULL
3480 || mch_isFullName(buf->b_sfname)))
3481 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003482 if (buf->b_sfname != buf->b_ffname)
3483 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003484 p = shorten_fname(buf->b_ffname, dirname);
3485 if (p != NULL)
3486 {
3487 buf->b_sfname = vim_strsave(p);
3488 buf->b_fname = buf->b_sfname;
3489 }
3490 if (p == NULL || buf->b_fname == NULL)
3491 buf->b_fname = buf->b_ffname;
3492 }
3493}
3494
3495/*
3496 * Shorten filenames for all buffers.
3497 */
3498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003499shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 char_u dirname[MAXPATHL];
3502 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
3504 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003505 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003507 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003509 // Always make the swap file name a full path, a "nofile" buffer may
3510 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003511 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003514 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003515#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003516 popup_update_preview_title();
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518}
3519
3520#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3521 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003522 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 || defined(PROTO)
3524/*
3525 * Shorten all filenames in "fnames[count]" by current directory.
3526 */
3527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003528shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 int i;
3531 char_u dirname[MAXPATHL];
3532 char_u *p;
3533
3534 if (fnames == NULL || count < 1)
3535 return;
3536 mch_dirname(dirname, sizeof(dirname));
3537 for (i = 0; i < count; ++i)
3538 {
3539 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003541 // shorten_fname() returns pointer in given "fnames[i]". If free
3542 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3543 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 p = vim_strsave(p);
3545 vim_free(fnames[i]);
3546 fnames[i] = p;
3547 }
3548 }
3549}
3550#endif
3551
3552/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003553 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * fo_o_h.ext for MSDOS or when shortname option set.
3555 *
3556 * Assumed that fname is a valid name found in the filesystem we assure that
3557 * the return value is a different name and ends in 'ext'.
3558 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3559 * characters otherwise.
3560 * Space for the returned name is allocated, must be freed later.
3561 * Returns NULL when out of memory.
3562 */
3563 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003564modname(
3565 char_u *fname,
3566 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003567 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003569 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 fname, ext, prepend_dot);
3571}
3572
3573 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003574buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003575 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003576 char_u *fname,
3577 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003578 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 char_u *retval;
3581 char_u *s;
3582 char_u *e;
3583 char_u *ptr;
3584 int fnamelen, extlen;
3585
3586 extlen = (int)STRLEN(ext);
3587
3588 /*
3589 * If there is no file name we must get the name of the current directory
3590 * (we need the full path in case :cd is used).
3591 */
3592 if (fname == NULL || *fname == NUL)
3593 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003594 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (retval == NULL)
3596 return NULL;
3597 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3598 (fnamelen = (int)STRLEN(retval)) == 0)
3599 {
3600 vim_free(retval);
3601 return NULL;
3602 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003603 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 retval[fnamelen++] = PATHSEP;
3606 retval[fnamelen] = NUL;
3607 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003608 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610 else
3611 {
3612 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003613 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (retval == NULL)
3615 return NULL;
3616 STRCPY(retval, fname);
3617#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003618 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#endif
3620 }
3621
3622 /*
3623 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3624 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3625 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3626 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3627 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003630 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003631 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003634 {
3635 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003640 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3642 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 s = ptr + STRLEN(ptr);
3645
3646 /*
3647 * For 8.3 file names we may have to reduce the length.
3648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 /*
3652 * If there is no file name, or the file name ends in '/', and the
3653 * extension starts with '.', put a '_' before the dot, because just
3654 * ".ext" is invalid.
3655 */
3656 if (fname == NULL || *fname == NUL
3657 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 *s++ = '_';
3661 }
3662 /*
3663 * If the extension starts with '.', truncate the base name at 8
3664 * characters
3665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003668 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 s = ptr + 8;
3671 *s = '\0';
3672 }
3673 }
3674 /*
3675 * If the extension doesn't start with '.', and the file name
3676 * doesn't have an extension yet, append a '.'
3677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else if ((e = vim_strchr(ptr, '.')) == NULL)
3679 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 /*
3681 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003682 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
3684 else if ((int)STRLEN(e) + extlen > 4)
3685 s = e + 4 - extlen;
3686 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003687#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 /*
3689 * If there is no file name, and the extension starts with '.', put a
3690 * '_' before the dot, because just ".ext" may be invalid if it's on a
3691 * FAT partition, and on HPFS it doesn't matter.
3692 */
3693 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3694 *s++ = '_';
3695#endif
3696
3697 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003698 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 * ext can start with '.' and cannot exceed 3 more characters.
3700 */
3701 STRCPY(s, ext);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 /*
3704 * Prepend the dot.
3705 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003706 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003708 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 /*
3713 * Check that, after appending the extension, the file name is really
3714 * different.
3715 */
3716 if (fname != NULL && STRCMP(fname, retval) == 0)
3717 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003718 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 while (--s >= ptr)
3720 {
3721 if (*s != '_')
3722 {
3723 *s = '_';
3724 break;
3725 }
3726 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003727 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 *ptr = 'v';
3729 }
3730 return retval;
3731}
3732
3733/*
3734 * Like fgets(), but if the file line is too long, it is truncated and the
3735 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003736 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003739vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 char *eof;
3742#define FGETS_SIZE 200
3743 char tbuf[FGETS_SIZE];
3744
3745 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3748 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003749 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003751 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 do
3753 {
3754 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003755 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3757 }
3758 return (eof == NULL);
3759}
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761/*
3762 * rename() only works if both files are on the same file system, this
3763 * function will (attempts to?) copy the file across if rename fails -- webb
3764 * Return -1 for failure, 0 for success.
3765 */
3766 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003767vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 int fd_in;
3770 int fd_out;
3771 int n;
3772 char *errmsg = NULL;
3773 char *buffer;
3774#ifdef AMIGA
3775 BPTR flock;
3776#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003777 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003778 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003779#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003780 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003781#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003782 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003785 * When the names are identical, there is nothing to do. When they refer
3786 * to the same file (ignoring case and slash/backslash differences) but
3787 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 */
3789 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003790 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003791 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003792 use_tmp_file = TRUE;
3793 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003794 return 0;
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3799 */
3800 if (mch_stat((char *)from, &st) < 0)
3801 return -1;
3802
Bram Moolenaar3576da72008-12-30 15:15:57 +00003803#ifdef UNIX
3804 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003805 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003807 // It's possible for the source and destination to be the same file.
3808 // This happens when "from" and "to" differ in case and are on a FAT32
3809 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003810 if (mch_stat((char *)to, &st_to) >= 0
3811 && st.st_dev == st_to.st_dev
3812 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003813 use_tmp_file = TRUE;
3814 }
3815#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003816#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003817 {
3818 BY_HANDLE_FILE_INFORMATION info1, info2;
3819
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003820 // It's possible for the source and destination to be the same file.
3821 // In that case go through a temp file name. This makes rename("foo",
3822 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003823 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3824 && win32_fileinfo(to, &info2) == FILEINFO_OK
3825 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3826 && info1.nFileIndexHigh == info2.nFileIndexHigh
3827 && info1.nFileIndexLow == info2.nFileIndexLow)
3828 use_tmp_file = TRUE;
3829 }
3830#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003831
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003832 if (use_tmp_file)
3833 {
3834 char tempname[MAXPATHL + 1];
3835
3836 /*
3837 * Find a name that doesn't exist and is in the same directory.
3838 * Rename "from" to "tempname" and then rename "tempname" to "to".
3839 */
3840 if (STRLEN(from) >= MAXPATHL - 5)
3841 return -1;
3842 STRCPY(tempname, from);
3843 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003844 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003845 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3846 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003847 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003848 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003849 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003850 if (mch_rename(tempname, (char *)to) == 0)
3851 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // Strange, the second step failed. Try moving the
3853 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003854 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003855 return -1;
3856 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 // If it fails for one temp name it will most likely fail
3858 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003859 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003860 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003861 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003862 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003863 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 /*
3866 * Delete the "to" file, this is required on some systems to make the
3867 * mch_rename() work, on other systems it makes sure that we don't have
3868 * two files when the mch_rename() fails.
3869 */
3870
3871#ifdef AMIGA
3872 /*
3873 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3874 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003875 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 * deleting the "from" file (horror!) we lock it during the remove.
3877 *
3878 * When used for making a backup before writing the file: This should not
3879 * happen with ":w", because startscript() should detect this problem and
3880 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3881 * name. This problem does exist with ":w filename", but then the
3882 * original file will be somewhere else so the backup isn't really
3883 * important. If autoscripting is off the rename may fail.
3884 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003885 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#endif
3887 mch_remove(to);
3888#ifdef AMIGA
3889 if (flock)
3890 UnLock(flock);
3891#endif
3892
3893 /*
3894 * First try a normal rename, return if it works.
3895 */
3896 if (mch_rename((char *)from, (char *)to) == 0)
3897 return 0;
3898
3899 /*
3900 * Rename() failed, try copying the file.
3901 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003902 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003903#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003905 acl = mch_get_acl(from);
3906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3908 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003909 {
3910#ifdef HAVE_ACL
3911 mch_free_acl(acl);
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003914 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003916 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003917 fd_out = mch_open((char *)to,
3918 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (fd_out == -1)
3920 {
3921 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003922#ifdef HAVE_ACL
3923 mch_free_acl(acl);
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 return -1;
3926 }
3927
Bram Moolenaar473952e2019-09-28 16:30:04 +02003928 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (buffer == NULL)
3930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003932 close(fd_in);
3933#ifdef HAVE_ACL
3934 mch_free_acl(acl);
3935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return -1;
3937 }
3938
Bram Moolenaar473952e2019-09-28 16:30:04 +02003939 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003940 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003942 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 break;
3944 }
3945
3946 vim_free(buffer);
3947 close(fd_in);
3948 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003949 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (n < 0)
3951 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003952 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 to = from;
3954 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003955#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003956 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003957#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003958#ifdef HAVE_ACL
3959 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003960 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003961#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003962#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003963 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (errmsg != NULL)
3966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003967 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 return -1;
3969 }
3970 mch_remove(from);
3971 return 0;
3972}
3973
3974static int already_warned = FALSE;
3975
3976/*
3977 * Check if any not hidden buffer has been changed.
3978 * Postpone the check if there are characters in the stuff buffer, a global
3979 * command is being executed, a mapping is being executed or an autocommand is
3980 * busy.
3981 * Returns TRUE if some message was written (screen should be redrawn and
3982 * cursor positioned).
3983 */
3984 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003985check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003986 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 buf_T *buf;
3989 int didit = 0;
3990 int n;
3991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003992 // Don't check timestamps while system() or another low-level function may
3993 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (no_check_timestamps > 0)
3995 return FALSE;
3996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003997 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3998 // event and we would keep on checking if the file is steadily growing.
3999 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (focus && did_check_timestamps)
4001 {
4002 need_check_timestamps = TRUE;
4003 return FALSE;
4004 }
4005
4006 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004007 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004008 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 else
4010 {
4011 ++no_wait_return;
4012 did_check_timestamps = TRUE;
4013 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004014 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004016 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (buf->b_nwindows > 0)
4018 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004019 bufref_T bufref;
4020
4021 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 n = buf_check_timestamp(buf, focus);
4023 if (didit < n)
4024 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004025 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004027 // Autocommands have removed the buffer, start at the
4028 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 buf = firstbuf;
4030 continue;
4031 }
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 --no_wait_return;
4035 need_check_timestamps = FALSE;
4036 if (need_wait_return && didit == 2)
4037 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004038 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004039 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 out_flush();
4041 }
4042 }
4043 return didit;
4044}
4045
4046/*
4047 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4048 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4049 * empty.
4050 */
4051 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004052move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
4054 buf_T *tbuf = curbuf;
4055 int retval = OK;
4056 linenr_T lnum;
4057 char_u *p;
4058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004059 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 curbuf = tobuf;
4061 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4062 {
4063 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4064 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4065 {
4066 vim_free(p);
4067 retval = FAIL;
4068 break;
4069 }
4070 vim_free(p);
4071 }
4072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004073 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 if (retval != FAIL)
4075 {
4076 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004077 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004078 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004080 // Oops! We could try putting back the saved lines, but that
4081 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 retval = FAIL;
4083 break;
4084 }
4085 }
4086
4087 curbuf = tbuf;
4088 return retval;
4089}
4090
4091/*
4092 * Check if buffer "buf" has been changed.
4093 * Also check if the file for a new buffer unexpectedly appeared.
4094 * return 1 if a changed buffer was found.
4095 * return 2 if a message has been displayed.
4096 * return 0 otherwise.
4097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004099buf_check_timestamp(
4100 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004101 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004103 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 int stat_res;
4105 int retval = 0;
4106 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004107 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004109 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004111 enum {
4112 RELOAD_NONE,
4113 RELOAD_NORMAL,
4114 RELOAD_DETECT
4115 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004116 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4118 int can_reload = FALSE;
4119#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004120 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 int orig_mode = buf->b_orig_mode;
4122#ifdef FEAT_GUI
4123 int save_mouse_correct = need_mouse_correct;
4124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004126 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004127#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004128 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004129#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004130 bufref_T bufref;
4131
4132 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004134 // If there is no file name, the buffer is not loaded, 'buftype' is
4135 // set, we are in the middle of a save or being called recursively: ignore
4136 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (buf->b_ffname == NULL
4138 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004139 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004142#ifdef FEAT_NETBEANS_INTG
4143 || isNetbeansBuffer(buf)
4144#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004145#ifdef FEAT_TERMINAL
4146 || buf->b_term != NULL
4147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 )
4149 return 0;
4150
4151 if ( !(buf->b_flags & BF_NOTEDITED)
4152 && buf->b_mtime != 0
4153 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004154 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004155 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#ifdef HAVE_ST_MODE
4157 || (int)st.st_mode != buf->b_orig_mode
4158#else
4159 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4160#endif
4161 ))
4162 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004163 long prev_b_mtime = buf->b_mtime;
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 retval = 1;
4166
Bram Moolenaar386bc822018-07-07 18:34:12 +02004167 // set b_mtime to stop further warnings (e.g., when executing
4168 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 if (stat_res < 0)
4170 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004171 // Check the file again later to see if it re-appears.
4172 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 buf->b_orig_size = 0;
4174 buf->b_orig_mode = 0;
4175 }
4176 else
4177 buf_store_time(buf, &st, buf->b_ffname);
4178
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004179 // Don't do anything for a directory. Might contain the file
4180 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (mch_isdir(buf->b_fname))
4182 ;
4183
4184 /*
4185 * If 'autoread' is set, the buffer has no changes and the file still
4186 * exists, reload the buffer. Use the buffer-local option value if it
4187 * was set, the global option value otherwise.
4188 */
4189 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4190 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004191 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else
4193 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004194 if (stat_res < 0)
4195 reason = "deleted";
4196 else if (bufIsChanged(buf))
4197 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004198 /*
4199 * Check if the file contents really changed to avoid giving a
4200 * warning when only the timestamp was set (e.g., checked out of
4201 * CVS). Always warn when the buffer was changed.
4202 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004203 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4204 reason = "changed";
4205 else if (orig_mode != buf->b_orig_mode)
4206 reason = "mode";
4207 else
4208 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 /*
4211 * Only give the warning if there are no FileChangedShell
4212 * autocommands.
4213 * Avoid being called recursively by setting "busy".
4214 */
4215 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004216#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004217 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4218 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004219#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004220 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4222 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004223 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 busy = FALSE;
4225 if (n)
4226 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004227 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004228 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004229#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230 s = get_vim_var_str(VV_FCS_CHOICE);
4231 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004232 reload = RELOAD_NORMAL;
4233 else if (STRCMP(s, "edit") == 0)
4234 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004235 else if (STRCMP(s, "ask") == 0)
4236 n = FALSE;
4237 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004238#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004239 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004241 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004243 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004244 {
4245 // Only give the message once.
4246 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004247 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 else
4250 {
4251 helpmesg = TRUE;
4252#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4253 can_reload = TRUE;
4254#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004255 if (reason[2] == 'n')
4256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004258 mesg2 = _("See \":help W12\" for more info.");
4259 }
4260 else if (reason[1] == 'h')
4261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263 mesg2 = _("See \":help W11\" for more info.");
4264 }
4265 else if (*reason == 'm')
4266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004268 mesg2 = _("See \":help W16\" for more info.");
4269 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004270 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004272 // Only timestamp changed, store it to avoid a warning
4273 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004274 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004275 buf->b_mtime_read_ns = buf->b_mtime_ns;
4276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 }
4279 }
4280
4281 }
4282 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4283 && vim_fexists(buf->b_ffname))
4284 {
4285 retval = 1;
4286 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4287 buf->b_flags |= BF_NEW_W;
4288#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4289 can_reload = TRUE;
4290#endif
4291 }
4292
4293 if (mesg != NULL)
4294 {
4295 path = home_replace_save(buf, buf->b_fname);
4296 if (path != NULL)
4297 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004298 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004300 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004301 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004302#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // Set warningmsg here, before the unimportant and output-specific
4304 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004305 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4308 if (can_reload)
4309 {
4310 if (*mesg2 != NUL)
4311 {
4312 STRCAT(tbuf, "\n");
4313 STRCAT(tbuf, mesg2);
4314 }
Rob Pilling8196e942022-02-11 15:12:10 +00004315 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4316 (char_u *)tbuf,
4317 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4318 1, NULL, TRUE))
4319 {
4320 case 2:
4321 reload = RELOAD_NORMAL;
4322 break;
4323 case 3:
4324 reload = RELOAD_DETECT;
4325 break;
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004330 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4331 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 if (*mesg2 != NUL)
4334 {
4335 STRCAT(tbuf, "; ");
4336 STRCAT(tbuf, mesg2);
4337 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004338 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 retval = 2;
4340 }
4341 else
4342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004346 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004348 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 msg_clr_eos();
4350 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004351 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004354#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004356#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004358 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 redraw_cmdline = FALSE;
4362 }
4363 }
4364 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366
4367 vim_free(path);
4368 vim_free(tbuf);
4369 }
4370 }
4371
Rob Pilling8196e942022-02-11 15:12:10 +00004372 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004375 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004376#ifdef FEAT_PERSISTENT_UNDO
4377 if (buf->b_p_udf && buf->b_ffname != NULL)
4378 {
4379 char_u hash[UNDO_HASH_SIZE];
4380 buf_T *save_curbuf = curbuf;
4381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004383 curbuf = buf;
4384 u_compute_hash(hash);
4385 u_write_undo(NULL, FALSE, buf, hash);
4386 curbuf = save_curbuf;
4387 }
4388#endif
4389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004392 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004393 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4394 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 // restore this in case an autocommand has set it; it would break
4397 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 need_mouse_correct = save_mouse_correct;
4399#endif
4400
4401 return retval;
4402}
4403
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004404/*
4405 * Reload a buffer that is already loaded.
4406 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004407 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4408 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 */
4410 void
Rob Pilling8196e942022-02-11 15:12:10 +00004411buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412{
4413 exarg_T ea;
4414 pos_T old_cursor;
4415 linenr_T old_topline;
4416 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004417 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004418 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004419 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004420 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004421 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004422 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423
Bram Moolenaare76062c2022-11-28 18:51:43 +00004424 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004425 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004426 if (curbuf != buf)
4427 {
4428 // Failed to find a window for "buf", it is dangerous to continue,
4429 // better bail out.
4430 return;
4431 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004432
Rob Pilling8196e942022-02-11 15:12:10 +00004433 // Unless reload_options is set, we only want to read the text from the
4434 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4435 // Force the fileformat and encoding to be the same.
4436 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004437 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004438 else
4439 prepped = prep_exarg(&ea, buf);
4440
4441 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 {
4443 old_cursor = curwin->w_cursor;
4444 old_topline = curwin->w_topline;
4445
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004446 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004447 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 // Save all the text, so that the reload can be undone.
4449 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004450 u_sync(FALSE);
4451 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4452 flags |= READ_KEEP_UNDO;
4453 }
4454
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004455 /*
4456 * To behave like when a new file is edited (matters for
4457 * BufReadPost autocommands) we first need to delete the current
4458 * buffer contents. But if reading the file fails we should keep
4459 * the old contents. Can't use memory only, the file might be
4460 * too big. Use a hidden buffer to move the buffer contents to.
4461 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004462 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004463 savebuf = NULL;
4464 else
4465 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004467 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004468 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004469 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004471 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004472 curbuf = savebuf;
4473 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004474 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004475 curbuf = buf;
4476 curwin->w_buffer = buf;
4477 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004478 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004479 || move_lines(buf, savebuf) == FAIL)
4480 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004481 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004482 saved = FAIL;
4483 }
4484 }
4485
4486 if (saved == OK)
4487 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004488 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4489 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004490 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4491 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004492 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004493 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004494#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004495 if (!aborting())
4496#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004497 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004498 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004500 // Put the text back from the save buffer. First
4501 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004502 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004503 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004504 break;
4505 (void)move_lines(savebuf, buf);
4506 }
4507 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004510 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004511 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004512 if ((flags & READ_KEEP_UNDO) == 0)
4513 {
4514 u_blockfree(buf);
4515 u_clearall(buf);
4516 }
4517 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004520 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004521 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004522 }
4523 }
4524 vim_free(ea.cmd);
4525
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004526 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004527 wipe_buffer(savebuf, FALSE);
4528
4529#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004531 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004532#endif
4533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004534 // Restore the topline and cursor position and check it (lines may
4535 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004536 if (old_topline > curbuf->b_ml.ml_line_count)
4537 curwin->w_topline = curbuf->b_ml.ml_line_count;
4538 else
4539 curwin->w_topline = old_topline;
4540 curwin->w_cursor = old_cursor;
4541 check_cursor();
4542 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004543 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004544#ifdef FEAT_FOLDING
4545 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004546 win_T *wp;
4547 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004550 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004551 if (wp->w_buffer == curwin->w_buffer
4552 && !foldmethodIsManual(wp))
4553 foldUpdateAll(wp);
4554 }
4555#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004556 // If the mode didn't change and 'readonly' was set, keep the old
4557 // value; the user probably used the ":view" command. But don't
4558 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004559 if (orig_mode == curbuf->b_orig_mode)
4560 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004562 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004563 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004564 }
4565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004567 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004568 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004569}
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004572buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
4574 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004575#ifdef ST_MTIM_NSEC
4576 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4577#else
4578 buf->b_mtime_ns = 0;
4579#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004580 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581#ifdef HAVE_ST_MODE
4582 buf->b_orig_mode = (int)st->st_mode;
4583#else
4584 buf->b_orig_mode = mch_getperm(fname);
4585#endif
4586}
4587
4588/*
4589 * Adjust the line with missing eol, used for the next write.
4590 * Used for do_filter(), when the input lines for the filter are deleted.
4591 */
4592 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004593write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004595 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004596 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597}
4598
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004599// Subfuncions for readdirex()
4600#ifdef FEAT_EVAL
4601# ifdef MSWIN
4602 static char_u *
4603getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4604{
4605 stat_T st;
4606 unsigned short st_mode;
4607 DWORD flag = wfd->dwFileAttributes;
4608 WCHAR *wp;
4609
4610 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4611 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4612 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4613 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4614
4615 wp = wcsrchr(wfd->cFileName, L'.');
4616 if (wp != NULL)
4617 {
4618 if (_wcsicmp(wp, L".exe") == 0 ||
4619 _wcsicmp(wp, L".com") == 0 ||
4620 _wcsicmp(wp, L".cmd") == 0 ||
4621 _wcsicmp(wp, L".bat") == 0)
4622 st_mode |= _S_IEXEC;
4623 }
4624
4625 // Copy user bits to group/other.
4626 st_mode |= (st_mode & 0700) >> 3;
4627 st_mode |= (st_mode & 0700) >> 6;
4628
4629 st.st_mode = st_mode;
4630 return getfpermst(&st, perm);
4631}
4632
4633 static char_u *
4634getftypewfd(WIN32_FIND_DATAW *wfd)
4635{
4636 DWORD flag = wfd->dwFileAttributes;
4637 DWORD tag = wfd->dwReserved0;
4638
4639 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4640 {
4641 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4642 return (char_u*)"junction";
4643 else if (tag == IO_REPARSE_TAG_SYMLINK)
4644 {
4645 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4646 return (char_u*)"linkd";
4647 else
4648 return (char_u*)"link";
4649 }
4650 return (char_u*)"reparse"; // unknown reparse point type
4651 }
4652 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4653 return (char_u*)"dir";
4654 else
4655 return (char_u*)"file";
4656}
4657
4658 static dict_T *
4659create_readdirex_item(WIN32_FIND_DATAW *wfd)
4660{
4661 dict_T *item;
4662 char_u *p;
4663 varnumber_T size, time;
4664 char_u permbuf[] = "---------";
4665
4666 item = dict_alloc();
4667 if (item == NULL)
4668 return NULL;
4669 item->dv_refcount++;
4670
4671 p = utf16_to_enc(wfd->cFileName, NULL);
4672 if (p == NULL)
4673 goto theend;
4674 if (dict_add_string(item, "name", p) == FAIL)
4675 {
4676 vim_free(p);
4677 goto theend;
4678 }
4679 vim_free(p);
4680
4681 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4682 if (dict_add_number(item, "size", size) == FAIL)
4683 goto theend;
4684
4685 // Convert FILETIME to unix time.
4686 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4687 wfd->ftLastWriteTime.dwLowDateTime)
4688 - 116444736000000000) / 10000000;
4689 if (dict_add_number(item, "time", time) == FAIL)
4690 goto theend;
4691
4692 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4693 goto theend;
4694 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4695 goto theend;
4696
4697 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4698 goto theend;
4699 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4700 goto theend;
4701
4702 return item;
4703
4704theend:
4705 dict_unref(item);
4706 return NULL;
4707}
4708# else
4709 static dict_T *
4710create_readdirex_item(char_u *path, char_u *name)
4711{
4712 dict_T *item;
4713 char *p;
4714 size_t len;
4715 stat_T st;
4716 int ret, link = FALSE;
4717 varnumber_T size;
4718 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004719 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004720 struct passwd *pw;
4721 struct group *gr;
4722
4723 item = dict_alloc();
4724 if (item == NULL)
4725 return NULL;
4726 item->dv_refcount++;
4727
4728 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4729 p = alloc(len);
4730 if (p == NULL)
4731 goto theend;
4732 vim_snprintf(p, len, "%s/%s", path, name);
4733 ret = mch_lstat(p, &st);
4734 if (ret >= 0 && S_ISLNK(st.st_mode))
4735 {
4736 link = TRUE;
4737 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004738 if (ret < 0)
4739 q = (char_u*)"link";
4740
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004741 }
4742 vim_free(p);
4743
4744 if (dict_add_string(item, "name", name) == FAIL)
4745 goto theend;
4746
4747 if (ret >= 0)
4748 {
4749 size = (varnumber_T)st.st_size;
4750 if (S_ISDIR(st.st_mode))
4751 size = 0;
4752 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004753 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004754 size = -2;
4755 if (dict_add_number(item, "size", size) == FAIL)
4756 goto theend;
4757 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4758 goto theend;
4759
4760 if (link)
4761 {
4762 if (S_ISDIR(st.st_mode))
4763 q = (char_u*)"linkd";
4764 else
4765 q = (char_u*)"link";
4766 }
4767 else
4768 q = getftypest(&st);
4769 if (dict_add_string(item, "type", q) == FAIL)
4770 goto theend;
4771 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4772 goto theend;
4773
4774 pw = getpwuid(st.st_uid);
4775 if (pw == NULL)
4776 q = (char_u*)"";
4777 else
4778 q = (char_u*)pw->pw_name;
4779 if (dict_add_string(item, "user", q) == FAIL)
4780 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004781# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004782 gr = getgrgid(st.st_gid);
4783 if (gr == NULL)
4784 q = (char_u*)"";
4785 else
4786 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004787# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004788 if (dict_add_string(item, "group", q) == FAIL)
4789 goto theend;
4790 }
4791 else
4792 {
4793 if (dict_add_number(item, "size", -1) == FAIL)
4794 goto theend;
4795 if (dict_add_number(item, "time", -1) == FAIL)
4796 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004797 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004798 goto theend;
4799 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4800 goto theend;
4801 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4802 goto theend;
4803 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4804 goto theend;
4805 }
4806 return item;
4807
4808theend:
4809 dict_unref(item);
4810 return NULL;
4811}
4812# endif
4813
4814 static int
4815compare_readdirex_item(const void *p1, const void *p2)
4816{
4817 char_u *name1, *name2;
4818
Bram Moolenaard61efa52022-07-23 09:52:04 +01004819 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4820 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004821 if (readdirex_sort == READDIR_SORT_BYTE)
4822 return STRCMP(name1, name2);
4823 else if (readdirex_sort == READDIR_SORT_IC)
4824 return STRICMP(name1, name2);
4825 else
4826 return STRCOLL(name1, name2);
4827}
4828
4829 static int
4830compare_readdir_item(const void *s1, const void *s2)
4831{
4832 if (readdirex_sort == READDIR_SORT_BYTE)
4833 return STRCMP(*(char **)s1, *(char **)s2);
4834 else if (readdirex_sort == READDIR_SORT_IC)
4835 return STRICMP(*(char **)s1, *(char **)s2);
4836 else
4837 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004838}
4839#endif
4840
Bram Moolenaarda440d22016-01-16 21:27:23 +01004841#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4842/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004843 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004844 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004845 * If "withattr" is TRUE, retrieve the names and their attributes.
4846 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004847 * Return OK for success, FAIL for failure.
4848 */
4849 int
4850readdir_core(
4851 garray_T *gap,
4852 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004853 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004854 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004855 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004856 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004857{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004858 int failed = FALSE;
4859 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004860# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004861 char_u *buf;
4862 int ok;
4863 HANDLE hFind = INVALID_HANDLE_VALUE;
4864 WIN32_FIND_DATAW wfd;
4865 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004866# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004867 DIR *dirp;
4868 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004869# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004870
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004871 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004872
4873# ifdef FEAT_EVAL
4874# define FREE_ITEM(item) do { \
4875 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004876 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004877 else \
4878 vim_free(item); \
4879 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004880
4881 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004882# else
4883# define FREE_ITEM(item) vim_free(item)
4884# endif
4885
4886# ifdef MSWIN
4887 buf = alloc(MAXPATHL);
4888 if (buf == NULL)
4889 return FAIL;
4890 STRNCPY(buf, path, MAXPATHL-5);
4891 p = buf + STRLEN(buf);
4892 MB_PTR_BACK(buf, p);
4893 if (*p == '\\' || *p == '/')
4894 *p = NUL;
4895 STRCAT(p, "\\*");
4896
4897 wn = enc_to_utf16(buf, NULL);
4898 if (wn != NULL)
4899 hFind = FindFirstFileW(wn, &wfd);
4900 ok = (hFind != INVALID_HANDLE_VALUE);
4901 if (!ok)
4902 {
4903 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004904 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004905 }
4906 else
4907 {
4908 while (ok)
4909 {
4910 int ignore;
4911 void *item;
4912 WCHAR *wp;
4913
4914 wp = wfd.cFileName;
4915 ignore = wp[0] == L'.' &&
4916 (wp[1] == NUL ||
4917 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004918 if (ignore)
4919 {
4920 ok = FindNextFileW(hFind, &wfd);
4921 continue;
4922 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004923# ifdef FEAT_EVAL
4924 if (withattr)
4925 item = (void*)create_readdirex_item(&wfd);
4926 else
4927# endif
4928 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4929 if (item == NULL)
4930 {
4931 failed = TRUE;
4932 break;
4933 }
4934
4935 if (!ignore && checkitem != NULL)
4936 {
4937 int r = checkitem(context, item);
4938
4939 if (r < 0)
4940 {
4941 FREE_ITEM(item);
4942 break;
4943 }
4944 if (r == 0)
4945 ignore = TRUE;
4946 }
4947
4948 if (!ignore)
4949 {
4950 if (ga_grow(gap, 1) == OK)
4951 ((void**)gap->ga_data)[gap->ga_len++] = item;
4952 else
4953 {
4954 failed = TRUE;
4955 FREE_ITEM(item);
4956 break;
4957 }
4958 }
4959 else
4960 FREE_ITEM(item);
4961
4962 ok = FindNextFileW(hFind, &wfd);
4963 }
4964 FindClose(hFind);
4965 }
4966
4967 vim_free(buf);
4968 vim_free(wn);
4969# else // MSWIN
4970 dirp = opendir((char *)path);
4971 if (dirp == NULL)
4972 {
4973 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004974 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004975 }
4976 else
4977 {
4978 for (;;)
4979 {
4980 int ignore;
4981 void *item;
4982
4983 dp = readdir(dirp);
4984 if (dp == NULL)
4985 break;
4986 p = (char_u *)dp->d_name;
4987
4988 ignore = p[0] == '.' &&
4989 (p[1] == NUL ||
4990 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004991 if (ignore)
4992 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004993# ifdef FEAT_EVAL
4994 if (withattr)
4995 item = (void*)create_readdirex_item(path, p);
4996 else
4997# endif
4998 item = (void*)vim_strsave(p);
4999 if (item == NULL)
5000 {
5001 failed = TRUE;
5002 break;
5003 }
5004
Bram Moolenaarfe154992022-03-22 20:42:12 +00005005 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005006 {
5007 int r = checkitem(context, item);
5008
5009 if (r < 0)
5010 {
5011 FREE_ITEM(item);
5012 break;
5013 }
5014 if (r == 0)
5015 ignore = TRUE;
5016 }
5017
5018 if (!ignore)
5019 {
5020 if (ga_grow(gap, 1) == OK)
5021 ((void**)gap->ga_data)[gap->ga_len++] = item;
5022 else
5023 {
5024 failed = TRUE;
5025 FREE_ITEM(item);
5026 break;
5027 }
5028 }
5029 else
5030 FREE_ITEM(item);
5031 }
5032
5033 closedir(dirp);
5034 }
5035# endif // MSWIN
5036
5037# undef FREE_ITEM
5038
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005039 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005040 {
5041# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005042 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005043 if (withattr)
5044 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5045 compare_readdirex_item);
5046 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005047 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5048 compare_readdir_item);
5049# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005050 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005051# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005052 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005053
5054 return failed ? FAIL : OK;
5055}
5056
5057/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005058 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005059 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005060 */
5061 int
5062delete_recursive(char_u *name)
5063{
5064 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005065 int i;
5066 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005067 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005068
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005069 // A symbolic link to a directory itself is deleted, not the directory it
5070 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005071 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005072# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005073 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005074# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005075 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005076# endif
5077 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005078 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005079 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005080 if (exp == NULL)
5081 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005082 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005083 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005084 for (i = 0; i < ga.ga_len; ++i)
5085 {
5086 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5087 ((char_u **)ga.ga_data)[i]);
5088 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005089 // Remember the failure but continue deleting any further
5090 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005091 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005092 }
5093 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005094 if (mch_rmdir(exp) != 0)
5095 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005096 }
5097 else
5098 result = -1;
5099 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005100 }
5101 else
5102 result = mch_remove(name) == 0 ? 0 : -1;
5103
5104 return result;
5105}
5106#endif
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005111# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5112/*
5113 * Open temporary directory and take file lock to prevent
5114 * to be auto-cleaned.
5115 */
5116 static void
5117vim_opentempdir(void)
5118{
5119 DIR *dp = NULL;
5120
5121 if (vim_tempdir_dp != NULL)
5122 return;
5123
5124 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005125 if (dp == NULL)
5126 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005127
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005128 vim_tempdir_dp = dp;
5129 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005130}
5131
5132/*
5133 * Close temporary directory - it automatically release file lock.
5134 */
5135 static void
5136vim_closetempdir(void)
5137{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005138 if (vim_tempdir_dp == NULL)
5139 return;
5140
5141 closedir(vim_tempdir_dp);
5142 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005143}
5144# endif
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * Delete the temp directory and all files it contains.
5148 */
5149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005152 if (vim_tempdir == NULL)
5153 return;
5154
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005155# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005156 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005157# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005158 // remove the trailing path separator
5159 gettail(vim_tempdir)[-1] = NUL;
5160 delete_recursive(vim_tempdir);
5161 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005165 * Directory "tempdir" was created. Expand this name to a full path and put
5166 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5167 * "tempdir" must be no longer than MAXPATHL.
5168 */
5169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005171{
5172 char_u *buf;
5173
Bram Moolenaar964b3742019-05-24 18:54:09 +02005174 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005175 if (buf == NULL)
5176 return;
5177
5178 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5179 STRCPY(buf, tempdir);
5180 add_pathsep(buf);
5181 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005182# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005183 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005184# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005185 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005186}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005187#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005188
5189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 * vim_tempname(): Return a unique name that can be used for a temp file.
5191 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005192 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5193 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 *
5195 * The returned pointer is to allocated memory.
5196 * The returned pointer is NULL if no valid name was found.
5197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005199vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005201 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005204 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005205#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005206 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#else
5208 char_u itmp[TEMPNAMELEN];
5209#endif
5210
5211#ifdef TEMPDIRNAMES
5212 static char *(tempdirs[]) = {TEMPDIRNAMES};
5213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005215 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216# endif
5217
5218 /*
5219 * This will create a directory for private use by this instance of Vim.
5220 * This is done once, and the same directory is used for all temp files.
5221 * This method avoids security problems because of symlink attacks et al.
5222 * It's also a bit faster, because we only need to check for an existing
5223 * file when creating the directory and not for each temp file.
5224 */
5225 if (vim_tempdir == NULL)
5226 {
5227 /*
5228 * Try the entries in TEMPDIRNAMES to create the temp directory.
5229 */
K.Takataeeec2542021-06-02 13:28:16 +02005230 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005232# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005233 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005234 long nr;
5235 long off;
5236# endif
5237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 // Expand $TMP, leave room for "/v1100000/999999999".
5239 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005241 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005243 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005244 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaareaf03392009-11-17 11:08:52 +00005246# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005247 {
5248# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 // Make sure the umask doesn't remove the executable bit.
5250 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005251 mode_t umask_save = umask(077);
5252# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005254 STRCAT(itmp, "vXXXXXX");
5255 if (mkdtemp((char *)itmp) != NULL)
5256 vim_settempdir(itmp);
5257# if defined(UNIX) || defined(VMS)
5258 (void)umask(umask_save);
5259# endif
5260 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005261# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005262 // Get an arbitrary number of up to 6 digits. When it's
5263 // unlikely that it already exists it will be faster,
5264 // otherwise it doesn't matter. The use of mkdir() avoids any
5265 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005267 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // Try up to 10000 different values until we find a name that
5270 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 for (off = 0; off < 10000L; ++off)
5272 {
5273 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005274# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
Bram Moolenaareaf03392009-11-17 11:08:52 +00005278 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5279# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005280 // If mkdir() does not set errno to EEXIST, check for
5281 // existing file here. There is a race condition then,
5282 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (mch_stat((char *)itmp, &st) >= 0)
5284 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005285# endif
5286# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287 // Make sure the umask doesn't remove the executable bit.
5288 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005292# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (r == 0)
5296 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005297 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 break;
5299 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005300# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005301 // If the mkdir() didn't fail because the file/dir exists,
5302 // we probably can't create any dir here, try another
5303 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 break;
5307 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (vim_tempdir != NULL)
5310 break;
5311 }
5312 }
5313 }
5314
5315 if (vim_tempdir != NULL)
5316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005317 // There is no need to check if the file exists, because we own the
5318 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5320 return vim_strsave(itmp);
5321 }
5322
5323 return NULL;
5324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005325#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
Bram Moolenaar4f974752019-02-17 17:44:42 +01005327# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005328 WCHAR wszTempFile[_MAX_PATH + 1];
5329 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005330 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 char_u *retval;
5332 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005333 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005334 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005336 wcscpy(itmp, L"");
5337 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005338 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005339 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005340 wszTempFile[1] = L'\\';
5341 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005342 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005343 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005344
5345 // randomize the name to avoid collisions
5346 i = mch_get_pid() + extra_char;
5347 buf4[1] = chartab[i % 36];
5348 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005349 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005351 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005352 // GetTempFileName() will create the file, we don't want that
5353 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005355 // Backslashes in a temp file name cause problems when filtering with
5356 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005357 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005358 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005359 shname = gettail(p_sh);
5360 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5361 || strstr((char *)shname, "pwsh") != NULL ))
5362 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 for (p = retval; *p; ++p)
5364 if (*p == '\\')
5365 *p = '/';
5366 return retval;
5367
Bram Moolenaar4f974752019-02-17 17:44:42 +01005368# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005371 char_u *p;
5372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005374 p = tmpnam((char *)itmp);
5375 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 return NULL;
5377# else
5378 char_u *p;
5379
5380# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005381 // mktemp() is not working on VMS. It seems to be
5382 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 sprintf((char *)itmp, "VIM%c", extra_char);
5384 p = (char_u *)tempnam("tmp:", (char *)itmp);
5385 if (p != NULL)
5386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005387 // VMS will use '.LIS' if we don't explicitly specify an extension,
5388 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 STRCPY(itmp, p);
5390 STRCAT(itmp, ".txt");
5391 free(p);
5392 }
5393 else
5394 return NULL;
5395# else
5396 STRCPY(itmp, TEMPNAME);
5397 if ((p = vim_strchr(itmp, '?')) != NULL)
5398 *p = extra_char;
5399 if (mktemp((char *)itmp) == NULL)
5400 return NULL;
5401# endif
5402# endif
5403
5404 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005405# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005406#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407}
5408
5409#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5410/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005411 * Convert all backslashes in fname to forward slashes in-place, unless when
5412 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 */
5414 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 char_u *p;
5418
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005419 if (path_with_url(fname))
5420 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005423 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005425 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 *p = '/';
5427}
5428#endif
5429
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005430/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005431 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5432 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5433 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 * Used for autocommands and 'wildignore'.
5435 * Returns TRUE if there is a match, FALSE otherwise.
5436 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005437 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005439 char_u *pattern, // pattern to match with
5440 regprog_T **prog, // pre-compiled regprog or NULL
5441 char_u *fname, // full path of file name
5442 char_u *sfname, // short file name or NULL
5443 char_u *tail, // tail of path
5444 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 regmatch_T regmatch;
5447 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005450 if (prog != NULL)
5451 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005453 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 /*
5456 * Try for a match with the pattern with:
5457 * 1. the full file name, when the pattern has a '/'.
5458 * 2. the short file name, when the pattern has a '/'.
5459 * 3. the tail of the file name, when the pattern has no '/'.
5460 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005461 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 && ((allow_dirs
5463 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5464 || (sfname != NULL
5465 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005466 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 result = TRUE;
5468
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005469 if (prog != NULL)
5470 *prog = regmatch.regprog;
5471 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005472 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 return result;
5474}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476/*
5477 * Return TRUE if a file matches with a pattern in "list".
5478 * "list" is a comma-separated list of patterns, like 'wildignore'.
5479 * "sfname" is the short file name or NULL, "ffname" the long file name.
5480 */
5481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005484 char_u buf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 char_u *tail;
5486 char_u *regpat;
5487 char allow_dirs;
5488 int match;
5489 char_u *p;
5490
5491 tail = gettail(sfname);
5492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005493 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 p = list;
5495 while (*p)
5496 {
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005497 copy_option_part(&p, buf, MAXPATHL, ",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5499 if (regpat == NULL)
5500 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005501 match = match_file_pat(regpat, NULL, ffname, sfname,
5502 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 vim_free(regpat);
5504 if (match)
5505 return TRUE;
5506 }
5507 return FALSE;
5508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510/*
5511 * Convert the given pattern "pat" which has shell style wildcards in it, into
5512 * a regular expression, and return the result in allocated memory. If there
5513 * is a directory path separator to be matched, then TRUE is put in
5514 * allow_dirs, otherwise FALSE is put there -- webb.
5515 * Handle backslashes before special characters, like "\*" and "\ ".
5516 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 * Returns NULL when out of memory.
5518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520file_pat_to_reg_pat(
5521 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 char_u *pat_end, // first char after pattern or NULL
5523 char *allow_dirs, // Result passed back out in here
5524 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u *endp;
5528 char_u *reg_pat;
5529 char_u *p;
5530 int i;
5531 int nested = 0;
5532 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534 if (allow_dirs != NULL)
5535 *allow_dirs = FALSE;
5536 if (pat_end == NULL)
5537 pat_end = pat + STRLEN(pat);
5538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 for (p = pat; p < pat_end; p++)
5540 {
5541 switch (*p)
5542 {
5543 case '*':
5544 case '.':
5545 case ',':
5546 case '{':
5547 case '}':
5548 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005549 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551#ifdef BACKSLASH_IN_FILENAME
5552 case '\\':
5553 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005554 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 break;
5556#endif
5557 default:
5558 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005559 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
5561 ++p;
5562 ++size;
5563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 break;
5565 }
5566 }
5567 reg_pat = alloc(size + 1);
5568 if (reg_pat == NULL)
5569 return NULL;
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 if (pat[0] == '*')
5574 while (pat[0] == '*' && pat < pat_end - 1)
5575 pat++;
5576 else
5577 reg_pat[i++] = '^';
5578 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005579 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 while (endp - pat > 0 && *endp == '*')
5582 endp--;
5583 add_dollar = FALSE;
5584 }
5585 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5586 {
5587 switch (*p)
5588 {
5589 case '*':
5590 reg_pat[i++] = '.';
5591 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005592 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005593 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 break;
5595 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 case '~':
5597 reg_pat[i++] = '\\';
5598 reg_pat[i++] = *p;
5599 break;
5600 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 reg_pat[i++] = '.';
5602 break;
5603 case '\\':
5604 if (p[1] == NUL)
5605 break;
5606#ifdef BACKSLASH_IN_FILENAME
5607 if (!no_bslash)
5608 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005609 // translate:
5610 // "\x" to "\\x" e.g., "dir\file"
5611 // "\*" to "\\.*" e.g., "dir\*.c"
5612 // "\?" to "\\." e.g., "dir\??.c"
5613 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5615 && p[1] != '+')
5616 {
5617 reg_pat[i++] = '[';
5618 reg_pat[i++] = '\\';
5619 reg_pat[i++] = '/';
5620 reg_pat[i++] = ']';
5621 if (allow_dirs != NULL)
5622 *allow_dirs = TRUE;
5623 break;
5624 }
5625 }
5626#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 // Undo escaping from ExpandEscape():
5628 // foo\?bar -> foo?bar
5629 // foo\%bar -> foo%bar
5630 // foo\,bar -> foo,bar
5631 // foo\ bar -> foo bar
5632 // Don't unescape \, * and others that are also special in a
5633 // regexp.
5634 // An escaped { must be unescaped since we use magic not
5635 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (*++p == '?'
5637#ifdef BACKSLASH_IN_FILENAME
5638 && no_bslash
5639#endif
5640 )
5641 reg_pat[i++] = '?';
5642 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005643 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005644 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005645 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005646 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5647 {
5648 reg_pat[i++] = '\\';
5649 reg_pat[i++] = '{';
5650 p += 2;
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
5653 {
5654 if (allow_dirs != NULL && vim_ispathsep(*p)
5655#ifdef BACKSLASH_IN_FILENAME
5656 && (!no_bslash || *p != '\\')
5657#endif
5658 )
5659 *allow_dirs = TRUE;
5660 reg_pat[i++] = '\\';
5661 reg_pat[i++] = *p;
5662 }
5663 break;
5664#ifdef BACKSLASH_IN_FILENAME
5665 case '/':
5666 reg_pat[i++] = '[';
5667 reg_pat[i++] = '\\';
5668 reg_pat[i++] = '/';
5669 reg_pat[i++] = ']';
5670 if (allow_dirs != NULL)
5671 *allow_dirs = TRUE;
5672 break;
5673#endif
5674 case '{':
5675 reg_pat[i++] = '\\';
5676 reg_pat[i++] = '(';
5677 nested++;
5678 break;
5679 case '}':
5680 reg_pat[i++] = '\\';
5681 reg_pat[i++] = ')';
5682 --nested;
5683 break;
5684 case ',':
5685 if (nested)
5686 {
5687 reg_pat[i++] = '\\';
5688 reg_pat[i++] = '|';
5689 }
5690 else
5691 reg_pat[i++] = ',';
5692 break;
5693 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005694 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005696 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 *allow_dirs = TRUE;
5698 reg_pat[i++] = *p;
5699 break;
5700 }
5701 }
5702 if (add_dollar)
5703 reg_pat[i++] = '$';
5704 reg_pat[i] = NUL;
5705 if (nested != 0)
5706 {
5707 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005708 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005710 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005711 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 return reg_pat;
5714}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005715
5716#if defined(EINTR) || defined(PROTO)
5717/*
5718 * Version of read() that retries when interrupted by EINTR (possibly
5719 * by a SIGWINCH).
5720 */
5721 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005723{
5724 long ret;
5725
5726 for (;;)
5727 {
5728 ret = vim_read(fd, buf, bufsize);
5729 if (ret >= 0 || errno != EINTR)
5730 break;
5731 }
5732 return ret;
5733}
5734
5735/*
5736 * Version of write() that retries when interrupted by EINTR (possibly
5737 * by a SIGWINCH).
5738 */
5739 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005741{
5742 long ret = 0;
5743 long wlen;
5744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005745 // Repeat the write() so long it didn't fail, other than being interrupted
5746 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005747 while (ret < (long)bufsize)
5748 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005749 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005750 if (wlen < 0)
5751 {
5752 if (errno != EINTR)
5753 break;
5754 }
5755 else
5756 ret += wlen;
5757 }
5758 return ret;
5759}
5760#endif