blob: 180fe3906c2305c6bc371544643056af575b2036 [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;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100152#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200153 off_T filesize_disk = 0; // file size read from disk
154 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 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;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100428#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200429 filesize_disk = st.st_size;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef UNIX
432 /*
433 * Use the protection bits of the original file for the swap file.
434 * This makes it possible for others to read the name of the
435 * edited file from the swapfile, but only if they can read the
436 * edited file.
437 * Remove the "write" and "execute" bits for group and others
438 * (they must not write the swapfile).
439 * Add the "read" and "write" bits for the user, otherwise we may
440 * not be able to write to the file ourselves.
441 * Setting the bits is done below, after creating the swap file.
442 */
443 swap_mode = (st.st_mode & 0644) | 0600;
444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#ifdef VMS
446 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000447 curbuf->b_fab_rat = st.st_fab_rat;
448 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449#endif
450 }
451 else
452 {
453 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100454 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100456 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 curbuf->b_orig_size = 0;
458 curbuf->b_orig_mode = 0;
459 }
460
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100461 // Reset the "new file" flag. It will be set again below when the
462 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
464 }
465
466/*
467 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100468 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
470 file_readonly = FALSE;
471 if (read_stdin)
472 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100473#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 setmode(0, O_BINARY);
476#endif
477 }
478 else if (!read_buffer)
479 {
480#ifdef USE_MCH_ACCESS
481 if (
482# ifdef UNIX
483 !(perm & 0222) ||
484# endif
485 mch_access((char *)fname, W_OK))
486 file_readonly = TRUE;
487 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
488#else
489 if (!newfile
490 || readonlymode
491 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
492 {
493 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
496 }
497#endif
498 }
499
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100500 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {
502#ifndef UNIX
503 int isdir_f;
504#endif
505 msg_scroll = msg_save;
506#ifndef UNIX
507 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100508 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 */
510 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100511 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (isdir_f)
513 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200514 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100515 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 }
517 else
518#endif
519 if (newfile)
520 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200521 if (perm < 0
522#ifdef ENOENT
523 && errno == ENOENT
524#endif
525 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
527 /*
528 * Set the 'new-file' flag, so that when the file has
529 * been created by someone else, a ":w" will complain.
530 */
531 curbuf->b_flags |= BF_NEW;
532
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // Create a swap file now, so that other Vims are warned
534 // that we are editing this file. Don't do this for a
535 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100539 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000540 if (curbuf != old_curbuf
541 || (using_b_ffname
542 && (old_b_ffname != curbuf->b_ffname))
543 || (using_b_fname
544 && (old_b_fname != curbuf->b_fname)))
545 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000546 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100547 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000548 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000549 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000550 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200551 filemess(curbuf, sfname,
552 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000553 else
554 filemess(curbuf, sfname,
555 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100557 // Even though this is a new file, it might have been
558 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 check_marks_read();
560#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100561 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200562 if (eap != NULL)
563 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
565 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100566 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 save_file_ff(curbuf);
568
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100569#if defined(FEAT_EVAL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100570 if (!aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100572 retval = OK; // a new file is not an error
573 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575 else
576 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000577 filemess(curbuf, sfname, (char_u *)(
578# ifdef EFBIG
579 (errno == EFBIG) ? _("[File too big]") :
580# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200581# ifdef EOVERFLOW
582 (errno == EOVERFLOW) ? _("[File too big]") :
583# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000584 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100585 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
588
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100589 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591
592 /*
593 * Only set the 'ro' flag for readonly files the first time they are
594 * loaded. Help files always get readonly mode
595 */
596 if ((check_readonly && file_readonly) || curbuf->b_help)
597 curbuf->b_p_ro = TRUE;
598
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000599 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100601 // Don't change 'eol' if reading from buffer as it will already be
602 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000603 if (!read_buffer)
604 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100605 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100606 curbuf->b_start_eof = FALSE;
607 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000608 curbuf->b_start_eol = TRUE;
609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000611 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
613
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100614 // Create a swap file now, so that other Vims are warned that we are
615 // editing this file.
616 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
619 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000620 if (!read_stdin && (curbuf != old_curbuf
621 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
622 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
623 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000624 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000625 if (!read_buffer)
626 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100627 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000631 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
632 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100633 {
634 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
635
636 /*
637 * If the group-read bit is set but not the world-read bit, then
638 * the group must be equal to the group of the original file. If
639 * we can't make that happen then reset the group-read bit. This
640 * avoids making the swap file readable to more users when the
641 * primary group of the user is too permissive.
642 */
643 if ((swap_mode & 044) == 040)
644 {
645 stat_T swap_st;
646
647 if (mch_stat((char *)swap_fname, &swap_st) >= 0
648 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200649# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100650 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200651 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200652# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200653 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100654 swap_mode &= 0600;
655 }
656
657 (void)mch_setperm(swap_fname, (long)swap_mode);
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#endif
660 }
661
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200662 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (swap_exists_action == SEA_QUIT)
664 {
665 if (!read_buffer && !read_stdin)
666 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100667 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672 /*
673 * Set '[ mark to the line above where the lines go (line 1 if zero).
674 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100675 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
677 curbuf->b_op_start.col = 0;
678
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100679 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
680 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
681 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if (!read_buffer)
684 {
685 int m = msg_scroll;
686 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 /*
689 * The file must be closed again, the autocommands may want to change
690 * the file before reading it.
691 */
692 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100693 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * The output from the autocommands should not overwrite anything and
697 * should not be overwritten: Set msg_scroll, restore its value if no
698 * output was done.
699 */
700 msg_scroll = TRUE;
701 if (filtering)
702 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
703 FALSE, curbuf, eap);
704 else if (read_stdin)
705 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
706 FALSE, curbuf, eap);
707 else if (newfile)
708 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
709 FALSE, curbuf, eap);
710 else
711 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
712 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100714 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
715 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
716 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100717 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 if (msg_scrolled == n)
720 msg_scroll = m;
721
722#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100723 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 --no_wait_return;
726 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100727 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100728 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730#endif
731 /*
732 * Don't allow the autocommands to change the current buffer.
733 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000734 *
735 * Don't allow the autocommands to change the buffer name either
736 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
738 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000739 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
740 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
742 {
743 --no_wait_return;
744 msg_scroll = msg_save;
745 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000746 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000748 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100749 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100750 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100754 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
756
757 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
758 {
759 /*
760 * Show the user that we are busy reading the input. Sometimes this
761 * may take a while. When reading from stdin another program may
762 * still be running, don't move the cursor to the last line, unless
763 * always using the GUI.
764 */
765 if (read_stdin)
766 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100767 if (!is_not_a_term())
768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200770# ifdef VIMDLL
771 if (!gui.in_use)
772# endif
773 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100777 if (gui.in_use && !gui.dying && !gui.starting)
778 {
Amon Sha10197932022-02-21 15:07:12 +0000779 // make a copy, gui_write() may try to change it
780 p = vim_strsave((char_u *)_("Reading from stdin..."));
781 if (p != NULL)
782 {
783 gui_write(p, (int)STRLEN(p));
784 vim_free(p);
785 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790 else if (!read_buffer)
791 filemess(curbuf, sfname, (char_u *)"", 0);
792 }
793
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
797 * Set linecnt now, before the "retry" caused by a wrong guess for
798 * fileformat, and after the autocommands, which may change them.
799 */
800 linecnt = curbuf->b_ml.ml_line_count;
801
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100802 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000803 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000804 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000805 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000806 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000807 curbuf->b_bad_char = eap->bad_char;
808 }
809 else
810 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000813 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 */
815 if (eap != NULL && eap->force_enc != 0)
816 {
817 fenc = enc_canonize(eap->cmd + eap->force_enc);
818 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000819 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
821 else if (curbuf->b_p_bin)
822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 fenc_alloced = FALSE;
825 }
826 else if (curbuf->b_help)
827 {
828 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000829 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Help files are either utf-8 or latin1. Try utf-8 first, if this
832 // fails it must be latin1.
833 // Always do this when 'encoding' is "utf-8". Otherwise only do
834 // this when needed to avoid [converted] remarks all the time.
835 // It is needed when the first line contains non-ASCII characters.
836 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 fenc = (char_u *)"latin1";
838 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000839 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000841 fc = fname[STRLEN(fname) - 1];
842 if (TOLOWER_ASC(fc) == 'x')
843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 // Read the first line (and a bit more). Immediately rewind to
845 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100846 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200847 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000848 for (p = firstline; p < firstline + len; ++p)
849 if (*p >= 0x80)
850 {
851 c = TRUE;
852 break;
853 }
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856
857 if (c)
858 {
859 fenc_next = fenc;
860 fenc = (char_u *)"utf-8";
861
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100862 // When the file is utf-8 but a character doesn't fit in
863 // 'encoding' don't retry. In help text editing utf-8 bytes
864 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000865 if (!enc_utf8)
866 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 fenc_alloced = FALSE;
869 }
870 else if (*p_fencs == NUL)
871 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100872 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 fenc_alloced = FALSE;
874 }
875 else
876 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200878 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
881 /*
882 * Jump back here to retry reading the file in different ways.
883 * Reasons to retry:
884 * - encoding conversion failed: try another one from "fenc_next"
885 * - BOM detected and fenc was set, need to setup conversion
886 * - "fileformat" check failed: try another
887 *
888 * Variables set for special retry actions:
889 * "file_rewind" Rewind the file to start reading it again.
890 * "advance_fenc" Advance "fenc" using "fenc_next".
891 * "skip_read" Re-use already read bytes (BOM detected).
892 * "did_iconv" iconv() conversion failed, try 'charconvert'.
893 * "keep_fileformat" Don't reset "fileformat".
894 *
895 * Other status indicators:
896 * "tmpname" When != NULL did conversion with 'charconvert'.
897 * Output file has to be deleted afterwards.
898 * "iconv_fd" When != -1 did conversion with iconv().
899 */
900retry:
901
902 if (file_rewind)
903 {
904 if (read_buffer)
905 {
906 read_buf_lnum = 1;
907 read_buf_col = 0;
908 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200909 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100911 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 error = TRUE;
913 goto failed;
914 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200917 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000919 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000922 curbuf->b_start_bomb = FALSE;
923 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000924 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926
927 /*
928 * When retrying with another "fenc" and the first time "fileformat"
929 * will be reset.
930 */
931 if (keep_fileformat)
932 keep_fileformat = FALSE;
933 else
934 {
935 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000938 try_unix = try_dos = try_mac = FALSE;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100941 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100945 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
Bram Moolenaar13505972019-01-24 15:04:48 +0100948#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (iconv_fd != (iconv_t)-1)
950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100951 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 iconv_close(iconv_fd);
953 iconv_fd = (iconv_t)-1;
954 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 if (advance_fenc)
958 {
959 /*
960 * Try the next entry in 'fileencodings'.
961 */
962 advance_fenc = FALSE;
963
964 if (eap != NULL && eap->force_enc != 0)
965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 // Conversion given with "++cc=" wasn't possible, read
967 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000969 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (fenc_alloced)
971 vim_free(fenc);
972 fenc = (char_u *)"";
973 fenc_alloced = FALSE;
974 }
975 else
976 {
977 if (fenc_alloced)
978 vim_free(fenc);
979 if (fenc_next != NULL)
980 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200981 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 else
984 {
985 fenc = (char_u *)"";
986 fenc_alloced = FALSE;
987 }
988 }
989 if (tmpname != NULL)
990 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100991 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100992 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994 }
995
996 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000997 * Conversion may be required when the encoding of the file is different
998 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 */
1000 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001001 converted = need_conversion(fenc);
1002 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 {
1004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001005 // "ucs-bom" means we need to check the first bytes of the file
1006 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1008 fio_flags = FIO_UCSBOM;
1009
1010 /*
1011 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1012 * done. This is handled below after read(). Prepare the
1013 * fio_flags to avoid having to parse the string each time.
1014 * Also check for Unicode to Latin1 conversion, because iconv()
1015 * appears not to handle this correctly. This works just like
1016 * conversion to UTF-8 except how the resulting character is put in
1017 * the buffer.
1018 */
1019 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1020 fio_flags = get_fio_flags(fenc);
1021
Bram Moolenaar4f974752019-02-17 17:44:42 +01001022#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /*
1024 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1025 * is handled with MultiByteToWideChar().
1026 */
1027 if (fio_flags == 0)
1028 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar13505972019-01-24 15:04:48 +01001031#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (fio_flags == 0)
1034 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar13505972019-01-24 15:04:48 +01001037#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 /*
1039 * Try using iconv() if we can't convert internally.
1040 */
1041 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 )
1046 iconv_fd = (iconv_t)my_iconv_open(
1047 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /*
1052 * Use the 'charconvert' expression when conversion is required
1053 * and we can't do it internally or with iconv().
1054 */
1055 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001056 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001057# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 )
1061 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001062# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001064# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // Skip conversion when it's already done (retry for wrong
1066 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (tmpname == NULL)
1068 {
1069 tmpname = readfile_charconvert(fname, fenc, &fd);
1070 if (tmpname == NULL)
1071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 advance_fenc = TRUE;
1074 if (fd < 0)
1075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001077 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 error = TRUE;
1079 goto failed;
1080 }
1081 goto retry;
1082 }
1083 }
1084 }
1085 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
1088 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001089#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 )
1093 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001094 // Conversion wanted but we can't.
1095 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 advance_fenc = TRUE;
1097 goto retry;
1098 }
1099 }
1100 }
1101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Set "can_retry" when it's possible to rewind the file and try with
1103 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1104 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001105 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
1107 if (!skip_read)
1108 {
1109 linerest = 0;
1110 filesize = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001111#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001112 filesize_count = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 skip_count = lines_to_skip;
1115 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001117#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001118 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1119 && curbuf->b_ffname != NULL
1120 && curbuf->b_p_udf
1121 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001122 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001123 && !read_stdin
1124 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001125 if (read_undo_file)
1126 sha256_start(&sha_ctx);
1127#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001128#ifdef FEAT_CRYPT
1129 if (curbuf->b_cryptstate != NULL)
1130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001131 // Need to free the state, but keep the key, don't want to ask for
1132 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001133 crypt_free_state(curbuf->b_cryptstate);
1134 curbuf->b_cryptstate = NULL;
1135 }
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138
1139 while (!error && !got_int)
1140 {
1141 /*
1142 * We allocate as much space for the file as we can get, plus
1143 * space for the old line plus room for one terminating NUL.
1144 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001145 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001147 if (!skip_read)
1148 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001149#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001151#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001152 // Use buffer >= 64K. Add linerest to double the size if the
1153 // line gets very long, to avoid a lot of copying. But don't
1154 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001155 size = 0x10000L + linerest;
1156 if (size > 0x100000L)
1157 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001158#endif
1159 }
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001162 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 size = 1;
1167 }
1168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 if (!skip_read)
1171 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001172 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001174 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 FALSE)) != NULL)
1176 break;
1177 }
1178 if (new_buffer == NULL)
1179 {
1180 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1181 error = TRUE;
1182 break;
1183 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001184 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1186 vim_free(buffer);
1187 buffer = new_buffer;
1188 ptr = buffer + linerest;
1189 line_start = buffer;
1190
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001191 // May need room to translate into.
1192 // For iconv() we don't really know the required space, use a
1193 // factor ICONV_MULT.
1194 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1195 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1196 // become up to 4 bytes, size must be multiple of 2
1197 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1198 // multiple of 2
1199 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1200 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001201 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001202#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (iconv_fd != (iconv_t)-1)
1204 size = size / ICONV_MULT;
1205 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (fio_flags & FIO_LATIN1)
1208 size = size / 2;
1209 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1210 size = (size * 2 / 3) & ~1;
1211 else if (fio_flags & FIO_UCS4)
1212 size = (size * 2 / 3) & ~3;
1213 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001214 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001215#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001217 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001218#endif
1219#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (conv_restlen > 0)
1225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 mch_memmove(ptr, conv_rest, conv_restlen);
1228 ptr += conv_restlen;
1229 size -= conv_restlen;
1230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
1232 if (read_buffer)
1233 {
1234 /*
1235 * Read bytes from curbuf. Used for converting text read
1236 * from stdin.
1237 */
1238 if (read_buf_lnum > from)
1239 size = 0;
1240 else
1241 {
1242 int n, ni;
1243 long tlen;
1244
1245 tlen = 0;
1246 for (;;)
1247 {
1248 p = ml_get(read_buf_lnum) + read_buf_col;
1249 n = (int)STRLEN(p);
1250 if ((int)tlen + n + 1 > size)
1251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001252 // Filled up to "size", append partial line.
1253 // Change NL to NUL to reverse the effect done
1254 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001255 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 for (ni = 0; ni < n; ++ni)
1257 {
1258 if (p[ni] == NL)
1259 ptr[tlen++] = NUL;
1260 else
1261 ptr[tlen++] = p[ni];
1262 }
1263 read_buf_col += n;
1264 break;
1265 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001266
1267 // Append whole line and new-line. Change NL
1268 // to NUL to reverse the effect done below.
1269 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001271 if (p[ni] == NL)
1272 ptr[tlen++] = NUL;
1273 else
1274 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001276 ptr[tlen++] = NL;
1277 read_buf_col = 0;
1278 if (++read_buf_lnum > from)
1279 {
1280 // When the last line didn't have an
1281 // end-of-line don't add it now either.
1282 if (!curbuf->b_p_eol)
1283 --tlen;
1284 size = tlen;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001285#ifdef FEAT_CRYPT
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001286 eof = TRUE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001287#endif
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001288 break;
1289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 }
1292 }
1293 else
1294 {
1295 /*
1296 * Read bytes from the file.
1297 */
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001298#ifdef FEAT_SODIUM
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001299 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001300 //
1301 // Sodium encryption requires a fixed block size to
1302 // successfully decrypt. However, unfortunately the file
1303 // header size changes between xchacha20 and xchacha20v2 by
1304 // 'add_len' bytes.
1305 // So we will now read the maximum header size + encryption
1306 // metadata, but after determining to read an xchacha20
1307 // encrypted file, we have to rewind the file descriptor by
1308 // 'add_len' bytes in the second round.
1309 //
1310 // Be careful with changing it, it needs to stay the same
1311 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001312 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001313 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001314 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001315 size = WRITEBUFSIZE + crypt_get_max_header_len()
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001316 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1317 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001318 may_need_lseek = TRUE;
1319 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001320
Christian Brabandtaae58342023-04-23 17:50:22 +01001321 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1322 && crypt_method_is_sodium(
1323 curbuf->b_cryptstate->method_nr)))
1324 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001325 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001326 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1327 // description above)
1328 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1329 && !eof && may_need_lseek)
1330 {
1331 lseek(fd, crypt_get_header_len(
1332 curbuf->b_cryptstate->method_nr)
1333 - crypt_get_max_header_len(), SEEK_CUR);
1334 may_need_lseek = FALSE;
1335 }
1336 }
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001337#endif
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001338 long read_size = size;
1339 size = read_eintr(fd, ptr, read_size);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001340#ifdef FEAT_CRYPT
Bram Moolenaar7f291222023-06-17 16:19:30 +01001341 // Did we reach end of file?
1342 filesize_count += size;
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001343 eof = (size < read_size || filesize_count == filesize_disk);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001347#ifdef FEAT_CRYPT
1348 /*
1349 * At start of file: Check for magic number of encryption.
1350 */
1351 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001352 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001353 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1354 &filesize, newfile, sfname,
1355 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001356# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001357 if (curbuf->b_cryptstate != NULL
1358 && !crypt_works_inplace(curbuf->b_cryptstate))
1359 // reading undo file requires crypt_decode_inplace()
1360 read_undo_file = FALSE;
1361# endif
1362 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001363 /*
1364 * Decrypt the read bytes. This is done before checking for
1365 * EOF because the crypt layer may be buffering.
1366 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001367 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1368 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001369 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001370# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001371 if (crypt_works_inplace(curbuf->b_cryptstate))
1372 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001373# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001374 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1375 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001376# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 }
1378 else
1379 {
1380 char_u *newptr = NULL;
1381 int decrypted_size;
1382
1383 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001384 curbuf->b_cryptstate, ptr, size,
1385 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001386
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001387 if (decrypted_size < 0)
1388 {
1389 // error message already given
1390 error = TRUE;
1391 vim_free(newptr);
1392 break;
1393 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001394 // If the crypt layer is buffering, not producing
1395 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001396 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001397 continue;
1398
1399 if (linerest == 0)
1400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001401 // Simple case: reuse returned buffer (may be
1402 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001403 new_buffer = newptr;
1404 }
1405 else
1406 {
1407 long_u new_size;
1408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001409 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001410 new_size = (long_u)(decrypted_size + linerest + 1);
1411 new_buffer = lalloc(new_size, FALSE);
1412 if (new_buffer == NULL)
1413 {
1414 do_outofmem_msg(new_size);
1415 error = TRUE;
1416 break;
1417 }
1418
1419 mch_memmove(new_buffer, buffer, linerest);
1420 if (newptr != NULL)
1421 mch_memmove(new_buffer + linerest, newptr,
1422 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001423 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001424 }
1425
1426 if (new_buffer != NULL)
1427 {
1428 vim_free(buffer);
1429 buffer = new_buffer;
1430 new_buffer = NULL;
1431 line_start = buffer;
1432 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001433 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001434 }
1435 size = decrypted_size;
1436 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001437# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001438 }
1439#endif
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (size <= 0)
1442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001443 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001446 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001447 /*
1448 * Reached end-of-file but some trailing bytes could
1449 * not be converted. Truncated file?
1450 */
1451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001452 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001453 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001454#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001455 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001456#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001457 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001458 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001459 if (can_retry)
1460 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001461 if (conv_error == 0)
1462 conv_error = curbuf->b_ml.ml_line_count
1463 - linecnt + 1;
1464 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001466 else if (illegal_byte == 0)
1467 illegal_byte = curbuf->b_ml.ml_line_count
1468 - linecnt + 1;
1469 if (bad_char_behavior == BAD_DROP)
1470 {
1471 *(ptr - conv_restlen) = NUL;
1472 conv_restlen = 0;
1473 }
1474 else
1475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001476 // Replace the trailing bytes with the replacement
1477 // character if we were converting; if we weren't,
1478 // leave the UTF8 checking code to do it, as it
1479 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001480 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001481#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001482 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001483#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001484 ))
1485 {
1486 while (conv_restlen > 0)
1487 {
1488 *(--ptr) = bad_char_behavior;
1489 --conv_restlen;
1490 }
1491 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001492 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001493#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001494 if (iconv_fd != (iconv_t)-1)
1495 {
1496 iconv_close(iconv_fd);
1497 iconv_fd = (iconv_t)-1;
1498 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001499#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001500 }
1501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 skip_read = FALSE;
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 /*
1507 * At start of file (or after crypt magic number): Check for BOM.
1508 * Also check for a BOM for other Unicode encodings, but not after
1509 * converting with 'charconvert' or when a BOM has already been
1510 * found.
1511 */
1512 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001513#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001514 || (cryptkey != NULL
1515 && filesize == crypt_get_header_len(
1516 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 )
1519 && (fio_flags == FIO_UCSBOM
1520 || (!curbuf->b_p_bomb
1521 && tmpname == NULL
1522 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1523 {
1524 char_u *ccname;
1525 int blen;
1526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (size < 2 || curbuf->b_p_bin)
1529 ccname = NULL;
1530 else
1531 ccname = check_for_bom(ptr, size, &blen,
1532 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1533 if (ccname != NULL)
1534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001535 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 filesize += blen;
1537 size -= blen;
1538 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001539 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001542 curbuf->b_start_bomb = TRUE;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545
1546 if (fio_flags == FIO_UCSBOM)
1547 {
1548 if (ccname == NULL)
1549 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 advance_fenc = TRUE;
1552 }
1553 else
1554 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (fenc_alloced)
1557 vim_free(fenc);
1558 fenc = ccname;
1559 fenc_alloced = FALSE;
1560 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001561 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 skip_read = TRUE;
1563 goto retry;
1564 }
1565 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001568 ptr -= conv_restlen;
1569 size += conv_restlen;
1570 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 /*
1572 * Break here for a read error or end-of-file.
1573 */
1574 if (size <= 0)
1575 break;
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar13505972019-01-24 15:04:48 +01001578#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (iconv_fd != (iconv_t)-1)
1580 {
1581 /*
1582 * Attempt conversion of the read bytes to 'encoding' using
1583 * iconv().
1584 */
1585 const char *fromp;
1586 char *top;
1587 size_t from_size;
1588 size_t to_size;
1589
1590 fromp = (char *)ptr;
1591 from_size = size;
1592 ptr += size;
1593 top = (char *)ptr;
1594 to_size = real_size - size;
1595
1596 /*
1597 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001598 * another conversion. Except for when there is no
1599 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001601 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1602 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1604 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001605 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001606 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001607 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001608 if (conv_error == 0)
1609 conv_error = readfile_linenr(linecnt,
1610 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001612 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001613 ++fromp;
1614 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001615 if (bad_char_behavior == BAD_KEEP)
1616 {
1617 *top++ = *(fromp - 1);
1618 --to_size;
1619 }
1620 else if (bad_char_behavior != BAD_DROP)
1621 {
1622 *top++ = bad_char_behavior;
1623 --to_size;
1624 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 if (from_size > 0)
1628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629 // Some remaining characters, keep them for the next
1630 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1632 conv_restlen = (int)from_size;
1633 }
1634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 line_start = ptr - linerest;
1637 mch_memmove(line_start, buffer, (size_t)linerest);
1638 size = (long)((char_u *)top - ptr);
1639 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar4f974752019-02-17 17:44:42 +01001642#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (fio_flags & FIO_CODEPAGE)
1644 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001645 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 WCHAR ucs2buf[3];
1647 int ucs2len;
1648 int codepage = FIO_GET_CP(fio_flags);
1649 int bytelen;
1650 int found_bad;
1651 char replstr[2];
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /*
1654 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001655 * a codepage, using standard MS-Windows functions. This
1656 * requires two steps:
1657 * 1. convert from 'fileencoding' to ucs-2
1658 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001660 * Because there may be illegal bytes AND an incomplete byte
1661 * sequence at the end, we may have to do the conversion one
1662 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001665 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001666 if (bad_char_behavior > 0)
1667 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 replstr[0] = '?';
1670 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001673 * Move the bytes to the end of the buffer, so that we have
1674 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001676 src = ptr + real_size - size;
1677 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001679 /*
1680 * Do the conversion.
1681 */
1682 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001683 while (size > 0)
1684 {
1685 found_bad = FALSE;
1686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001688 if (codepage == CP_UTF8)
1689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001690 // Handle CP_UTF8 input ourselves to be able to handle
1691 // trailing bytes properly.
1692 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001693 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001694 if (bytelen > size)
1695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 // Only got some bytes of a character. Normally
1697 // it's put in "conv_rest", but if it's too long
1698 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001699 if (bytelen <= CONV_RESTLEN)
1700 break;
1701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001703 bytelen = size;
1704 found_bad = TRUE;
1705 }
1706 else
1707 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001708 int u8c = utf_ptr2char(src);
1709
Bram Moolenaar86e01082005-12-29 22:45:34 +00001710 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001711 found_bad = TRUE;
1712 ucs2buf[0] = u8c;
1713 ucs2len = 1;
1714 }
1715 }
1716 else
1717# endif
1718 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001719 // We don't know how long the byte sequence is, try
1720 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001721 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1722 ++bytelen)
1723 {
1724 ucs2len = MultiByteToWideChar(codepage,
1725 MB_ERR_INVALID_CHARS,
1726 (LPCSTR)src, bytelen,
1727 ucs2buf, 3);
1728 if (ucs2len > 0)
1729 break;
1730 }
1731 if (ucs2len == 0)
1732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // If we have only one byte then it's probably an
1734 // incomplete byte sequence. Otherwise discard
1735 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001736 if (size == 1)
1737 break;
1738 found_bad = TRUE;
1739 bytelen = 1;
1740 }
1741 }
1742
1743 if (!found_bad)
1744 {
1745 int i;
1746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001748 if (enc_utf8)
1749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001750 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001751 for (i = 0; i < ucs2len; ++i)
1752 dst += utf_char2bytes(ucs2buf[i], dst);
1753 }
1754 else
1755 {
1756 BOOL bad = FALSE;
1757 int dstlen;
1758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // From UCS-2 to "enc_codepage". If the
1760 // conversion uses the default character "?",
1761 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001762 dstlen = WideCharToMultiByte(enc_codepage, 0,
1763 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001764 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001765 replstr, &bad);
1766 if (bad)
1767 found_bad = TRUE;
1768 else
1769 dst += dstlen;
1770 }
1771 }
1772
1773 if (found_bad)
1774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001775 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001776 if (can_retry)
1777 goto rewind_retry;
1778 if (conv_error == 0)
1779 conv_error = readfile_linenr(linecnt, ptr, dst);
1780 if (bad_char_behavior != BAD_DROP)
1781 {
1782 if (bad_char_behavior == BAD_KEEP)
1783 {
1784 mch_memmove(dst, src, bytelen);
1785 dst += bytelen;
1786 }
1787 else
1788 *dst++ = bad_char_behavior;
1789 }
1790 }
1791
1792 src += bytelen;
1793 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001795
1796 if (size > 0)
1797 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001799 mch_memmove(conv_rest, src, size);
1800 conv_restlen = size;
1801 }
1802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001803 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001804 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001807#endif
1808#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (fio_flags & FIO_MACROMAN)
1810 {
1811 /*
1812 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001813 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001815 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (fio_flags != 0)
1821 {
1822 int u8c;
1823 char_u *dest;
1824 char_u *tail = NULL;
1825
1826 /*
1827 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1828 * "enc_utf8" not set: Convert Unicode to Latin1.
1829 * Go from end to start through the buffer, because the number
1830 * of bytes may increase.
1831 * "dest" points to after where the UTF-8 bytes go, "p" points
1832 * to after the next character to convert.
1833 */
1834 dest = ptr + real_size;
1835 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1836 {
1837 p = ptr + size;
1838 if (fio_flags == FIO_UTF8)
1839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001840 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 tail = ptr + size - 1;
1842 while (tail > ptr && (*tail & 0xc0) == 0x80)
1843 --tail;
1844 if (tail + utf_byte2len(*tail) <= ptr + size)
1845 tail = NULL;
1846 else
1847 p = tail;
1848 }
1849 }
1850 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001852 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 p = ptr + (size & ~1);
1854 if (size & 1)
1855 tail = p;
1856 if ((fio_flags & FIO_UTF16) && p > ptr)
1857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001858 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (fio_flags & FIO_ENDIAN_L)
1860 {
1861 u8c = (*--p << 8);
1862 u8c += *--p;
1863 }
1864 else
1865 {
1866 u8c = *--p;
1867 u8c += (*--p << 8);
1868 }
1869 if (u8c >= 0xd800 && u8c <= 0xdbff)
1870 tail = p;
1871 else
1872 p += 2;
1873 }
1874 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001875 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001877 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 p = ptr + (size & ~3);
1879 if (size & 3)
1880 tail = p;
1881 }
1882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001883 // If there is a trailing incomplete sequence move it to
1884 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (tail != NULL)
1886 {
1887 conv_restlen = (int)((ptr + size) - tail);
1888 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1889 size -= conv_restlen;
1890 }
1891
1892
1893 while (p > ptr)
1894 {
1895 if (fio_flags & FIO_LATIN1)
1896 u8c = *--p;
1897 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1898 {
1899 if (fio_flags & FIO_ENDIAN_L)
1900 {
1901 u8c = (*--p << 8);
1902 u8c += *--p;
1903 }
1904 else
1905 {
1906 u8c = *--p;
1907 u8c += (*--p << 8);
1908 }
1909 if ((fio_flags & FIO_UTF16)
1910 && u8c >= 0xdc00 && u8c <= 0xdfff)
1911 {
1912 int u16c;
1913
1914 if (p == ptr)
1915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (can_retry)
1918 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001919 if (conv_error == 0)
1920 conv_error = readfile_linenr(linecnt,
1921 ptr, p);
1922 if (bad_char_behavior == BAD_DROP)
1923 continue;
1924 if (bad_char_behavior != BAD_KEEP)
1925 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
1927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // found second word of double-word, get the first
1929 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (fio_flags & FIO_ENDIAN_L)
1931 {
1932 u16c = (*--p << 8);
1933 u16c += *--p;
1934 }
1935 else
1936 {
1937 u16c = *--p;
1938 u16c += (*--p << 8);
1939 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001940 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1941 + (u8c & 0x3ff);
1942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (u16c < 0xd800 || u16c > 0xdbff)
1945 {
1946 if (can_retry)
1947 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001948 if (conv_error == 0)
1949 conv_error = readfile_linenr(linecnt,
1950 ptr, p);
1951 if (bad_char_behavior == BAD_DROP)
1952 continue;
1953 if (bad_char_behavior != BAD_KEEP)
1954 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958 else if (fio_flags & FIO_UCS4)
1959 {
1960 if (fio_flags & FIO_ENDIAN_L)
1961 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001962 u8c = (unsigned)*--p << 24;
1963 u8c += (unsigned)*--p << 16;
1964 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 u8c += *--p;
1966 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001970 u8c += (unsigned)*--p << 8;
1971 u8c += (unsigned)*--p << 16;
1972 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 if (*--p < 0x80)
1978 u8c = *p;
1979 else
1980 {
1981 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001982 p -= len;
1983 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (len == 0)
1985 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 // Not a valid UTF-8 character, retry with
1987 // another fenc when possible, otherwise just
1988 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (can_retry)
1990 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001991 if (conv_error == 0)
1992 conv_error = readfile_linenr(linecnt,
1993 ptr, p);
1994 if (bad_char_behavior == BAD_DROP)
1995 continue;
1996 if (bad_char_behavior != BAD_KEEP)
1997 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 dest -= utf_char2len(u8c);
2004 (void)utf_char2bytes(u8c, dest);
2005 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002006 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 --dest;
2009 if (u8c >= 0x100)
2010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // character doesn't fit in latin1, retry with
2012 // another fenc when possible, otherwise just
2013 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002014 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002016 if (conv_error == 0)
2017 conv_error = readfile_linenr(linecnt, ptr, p);
2018 if (bad_char_behavior == BAD_DROP)
2019 ++dest;
2020 else if (bad_char_behavior == BAD_KEEP)
2021 *dest = u8c;
2022 else if (eap != NULL && eap->bad_char != 0)
2023 *dest = bad_char_behavior;
2024 else
2025 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 else
2028 *dest = u8c;
2029 }
2030 }
2031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 line_start = dest - linerest;
2034 mch_memmove(line_start, buffer, (size_t)linerest);
2035 size = (long)((ptr + real_size) - dest);
2036 ptr = dest;
2037 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002038 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002040 int incomplete_tail = FALSE;
2041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002043 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002045 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002046 int l;
2047
2048 if (todo <= 0)
2049 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (*p >= 0x80)
2051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // A length of 1 means it's an illegal byte. Accept
2053 // an incomplete character at the end though, the next
2054 // read() will get the next bytes, we'll check it
2055 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002056 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002057 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // Avoid retrying with a different encoding when
2060 // a truncated file is more likely, or attempting
2061 // to read the rest of an incomplete sequence when
2062 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002063 if (p > ptr || filesize > 0)
2064 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Incomplete byte sequence, move it to conv_rest[]
2066 // and try to read the rest of it, unless we've
2067 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002068 if (p > ptr)
2069 {
2070 conv_restlen = todo;
2071 mch_memmove(conv_rest, p, conv_restlen);
2072 size -= conv_restlen;
2073 break;
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002076 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // Illegal byte. If we can try another encoding
2079 // do that, unless at EOF where a truncated
2080 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002081 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002082 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002083#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002085 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2086 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002087#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002089 if (conv_error == 0 && illegal_byte == 0)
2090 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002093 if (bad_char_behavior == BAD_DROP)
2094 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002095 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002096 --p;
2097 --size;
2098 }
2099 else if (bad_char_behavior != BAD_KEEP)
2100 *p = bad_char_behavior;
2101 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002102 else
2103 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002106 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002111#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002112 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002113 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002114 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002116#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002117 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002118 advance_fenc = TRUE;
2119 file_rewind = TRUE;
2120 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 filesize += size;
2126
2127 /*
2128 * when reading the first part of a file: guess EOL type
2129 */
2130 if (fileformat == EOL_UNKNOWN)
2131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (try_dos || try_unix)
2134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002136 if (try_mac)
2137 try_mac = 1;
2138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 for (p = ptr; p < ptr + size; ++p)
2140 {
2141 if (*p == NL)
2142 {
2143 if (!try_unix
2144 || (try_dos && p > ptr && p[-1] == CAR))
2145 fileformat = EOL_DOS;
2146 else
2147 fileformat = EOL_UNIX;
2148 break;
2149 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002150 else if (*p == CAR && try_mac)
2151 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (fileformat == EOL_UNIX && try_mac)
2156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002157 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 try_mac = 1;
2159 try_unix = 1;
2160 for (; p >= ptr && *p != CAR; p--)
2161 ;
2162 if (p >= ptr)
2163 {
2164 for (p = ptr; p < ptr + size; ++p)
2165 {
2166 if (*p == NL)
2167 try_unix++;
2168 else if (*p == CAR)
2169 try_mac++;
2170 }
2171 if (try_mac > try_unix)
2172 fileformat = EOL_MAC;
2173 }
2174 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002175 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002176 // Looking for CR but found no end-of-line markers at
2177 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002178 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002181 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (fileformat == EOL_UNKNOWN && try_mac)
2183 fileformat = EOL_MAC;
2184
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002185 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (fileformat == EOL_UNKNOWN)
2187 fileformat = default_fileformat();
2188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002189 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 set_fileformat(fileformat, OPT_LOCAL);
2192 }
2193 }
2194
2195 /*
2196 * This loop is executed once for every character read.
2197 * Keep it fast!
2198 */
2199 if (fileformat == EOL_MAC)
2200 {
2201 --ptr;
2202 while (++ptr, --size >= 0)
2203 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if ((c = *ptr) != NUL && c != CAR && c != NL)
2206 continue;
2207 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002210 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else
2212 {
2213 if (skip_count == 0)
2214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 len = (colnr_T) (ptr - line_start + 1);
2217 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2218 {
2219 error = TRUE;
2220 break;
2221 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002222#ifdef FEAT_PERSISTENT_UNDO
2223 if (read_undo_file)
2224 sha256_update(&sha_ctx, line_start, len);
2225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 ++lnum;
2227 if (--read_count == 0)
2228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 error = TRUE; // break loop
2230 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 break;
2232 }
2233 }
2234 else
2235 --skip_count;
2236 line_start = ptr + 1;
2237 }
2238 }
2239 }
2240 else
2241 {
2242 --ptr;
2243 while (++ptr, --size >= 0)
2244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 continue;
2247 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 else
2250 {
2251 if (skip_count == 0)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 len = (colnr_T)(ptr - line_start + 1);
2255 if (fileformat == EOL_DOS)
2256 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002257 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ptr[-1] = NUL;
2261 --len;
2262 }
2263 /*
2264 * Reading in Dos format, but no CR-LF found!
2265 * When 'fileformats' includes "unix", delete all
2266 * the lines read so far and start all over again.
2267 * Otherwise give an error message later.
2268 */
2269 else if (ff_error != EOL_DOS)
2270 {
2271 if ( try_unix
2272 && !read_stdin
2273 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002274 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2275 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002278 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 set_fileformat(EOL_UNIX, OPT_LOCAL);
2280 file_rewind = TRUE;
2281 keep_fileformat = TRUE;
2282 goto retry;
2283 }
2284 ff_error = EOL_DOS;
2285 }
2286 }
2287 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2288 {
2289 error = TRUE;
2290 break;
2291 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002292#ifdef FEAT_PERSISTENT_UNDO
2293 if (read_undo_file)
2294 sha256_update(&sha_ctx, line_start, len);
2295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ++lnum;
2297 if (--read_count == 0)
2298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002299 error = TRUE; // break loop
2300 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 break;
2302 }
2303 }
2304 else
2305 --skip_count;
2306 line_start = ptr + 1;
2307 }
2308 }
2309 }
2310 linerest = (long)(ptr - line_start);
2311 ui_breakcheck();
2312 }
2313
2314failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (error && read_count == 0)
2317 error = FALSE;
2318
K.Takata3af98212022-11-01 20:36:19 +00002319 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2320 // In old days the file length was in sector count and the CTRL-Z the
2321 // marker where the file really ended. Assuming we write it to a file
2322 // system that keeps file length properly the CTRL-Z should be dropped.
2323 // Set the 'endoffile' option so the user can decide what to write later.
2324 // In Unix format the CTRL-Z is just another character.
2325 if (linerest != 0
2326 && !curbuf->b_p_bin
2327 && fileformat == EOL_DOS
2328 && ptr[-1] == Ctrl_Z)
2329 {
2330 ptr--;
2331 linerest--;
2332 if (set_options)
2333 curbuf->b_p_eof = TRUE;
2334 }
2335
2336 // If we get EOF in the middle of a line, note the fact by resetting
2337 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (!error
2339 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002340 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002343 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 curbuf->b_p_eol = FALSE;
2345 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002346 len = (colnr_T)(ptr - line_start + 1);
2347 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 error = TRUE;
2349 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002350 {
2351#ifdef FEAT_PERSISTENT_UNDO
2352 if (read_undo_file)
2353 sha256_update(&sha_ctx, line_start, len);
2354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002359 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002363 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002364 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002365 crypt_free_state(curbuf->b_cryptstate);
2366 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002367 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002368 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2369 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // Don't set cryptkey to NULL, it's used below as a flag that
2371 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#endif
2373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002374 // If editing a new file: set 'fenc' for the current buffer.
2375 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002376 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002378 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (fenc_alloced)
2380 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002381#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#endif
2385
2386 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002387 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002388#ifdef HAVE_FD_CLOEXEC
2389 else
2390 {
2391 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002392
Bram Moolenaarf05da212009-11-17 16:13:15 +00002393 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002394 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002395 }
2396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 vim_free(buffer);
2398
2399#ifdef HAVE_DUP
2400 if (read_stdin)
2401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002402 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002404 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406#endif
2407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (tmpname != NULL)
2409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002410 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 vim_free(tmpname);
2412 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002413 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /*
2416 * In recovery mode everything but autocommands is skipped.
2417 */
2418 if (!recoverymode)
2419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002420 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2422 {
2423#ifdef FEAT_NETBEANS_INTG
2424 netbeansFireChanges = 0;
2425#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002426 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#ifdef FEAT_NETBEANS_INTG
2428 netbeansFireChanges = 1;
2429#endif
2430 --linecnt;
2431 }
2432 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2433 if (filesize == 0)
2434 linecnt = 0;
2435 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002436 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002437 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002438#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002439 // After reading the text into the buffer the diff info needs to
2440 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002441 diff_invalidate(curbuf);
2442#endif
2443#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002444 // All folds in the window are invalid now. Mark them for update
2445 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002446 foldUpdateAll(curwin);
2447#endif
2448 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002449 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 appended_lines_mark(from, linecnt);
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452#ifndef ALWAYS_USE_GUI
2453 /*
2454 * If we were reading from the same terminal as where messages go,
2455 * the screen will have been messed up.
2456 * Switch on raw mode now and clear the screen.
2457 */
2458 if (read_stdin)
2459 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002460 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 starttermcap();
2462 screenclear();
2463 }
2464#endif
2465
2466 if (got_int)
2467 {
2468 if (!(flags & READ_DUMMY))
2469 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002470 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002472 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 msg_scroll = msg_save;
2475#ifdef FEAT_VIMINFO
2476 check_marks_read();
2477#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002478 retval = OK; // an interrupt isn't really an error
2479 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
2482 if (!filtering && !(flags & READ_DUMMY))
2483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002484 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 c = FALSE;
2486
2487#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002488 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 STRCAT(IObuff, _("[fifo]"));
2491 c = TRUE;
2492 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002493 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 STRCAT(IObuff, _("[socket]"));
2496 c = TRUE;
2497 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002498# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002499 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002500 {
2501 STRCAT(IObuff, _("[character special]"));
2502 c = TRUE;
2503 }
2504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#endif
2506 if (curbuf->b_p_ro)
2507 {
2508 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2509 c = TRUE;
2510 }
2511 if (read_no_eol_lnum)
2512 {
2513 msg_add_eol();
2514 c = TRUE;
2515 }
2516 if (ff_error == EOL_DOS)
2517 {
2518 STRCAT(IObuff, _("[CR missing]"));
2519 c = TRUE;
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (split)
2522 {
2523 STRCAT(IObuff, _("[long lines split]"));
2524 c = TRUE;
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (notconverted)
2527 {
2528 STRCAT(IObuff, _("[NOT converted]"));
2529 c = TRUE;
2530 }
2531 else if (converted)
2532 {
2533 STRCAT(IObuff, _("[converted]"));
2534 c = TRUE;
2535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#ifdef FEAT_CRYPT
2537 if (cryptkey != NULL)
2538 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002539 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 c = TRUE;
2541 }
2542#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002543 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002545 sprintf((char *)IObuff + STRLEN(IObuff),
2546 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 c = TRUE;
2548 }
2549 else if (illegal_byte > 0)
2550 {
2551 sprintf((char *)IObuff + STRLEN(IObuff),
2552 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2553 c = TRUE;
2554 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002555 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 STRCAT(IObuff, _("[READ ERRORS]"));
2558 c = TRUE;
2559 }
2560 if (msg_add_fileformat(fileformat))
2561 c = TRUE;
2562#ifdef FEAT_CRYPT
2563 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002564 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002565 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 else
2567#endif
2568 msg_add_lines(c, (long)linecnt, filesize);
2569
Bram Moolenaard23a8232018-02-10 18:45:26 +01002570 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 msg_scrolled_ign = TRUE;
2572#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002573 // Don't show the message when reading stdin, it would end up in a
2574 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (read_stdin || read_buffer)
2576 p = msg_may_trunc(FALSE, IObuff);
2577 else
2578#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002579 {
2580 if (msg_col > 0)
2581 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002582 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002585 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002586 // Need to repeat the message after redrawing when:
2587 // - When reading from stdin (the screen will be cleared next).
2588 // - When restart_edit is set (otherwise there will be a delay
2589 // before redrawing).
2590 // - When the screen was scrolled but there is no wait-return
2591 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002592 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 msg_scrolled_ign = FALSE;
2594 }
2595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002596 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002598 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002599 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 curbuf->b_p_ro = TRUE;
2601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002602 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 /*
2605 * In Ex mode: cursor at last new line.
2606 * Otherwise: cursor at first new line.
2607 */
2608 if (exmode_active)
2609 curwin->w_cursor.lnum = from + linecnt;
2610 else
2611 curwin->w_cursor.lnum = from + 1;
2612 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002613 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
Bram Moolenaare1004402020-10-24 20:49:43 +02002615 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002616 {
2617 // Set '[ and '] marks to the newly read lines.
2618 curbuf->b_op_start.lnum = from + 1;
2619 curbuf->b_op_start.col = 0;
2620 curbuf->b_op_end.lnum = from + linecnt;
2621 curbuf->b_op_end.col = 0;
2622 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002623
Bram Moolenaar4f974752019-02-17 17:44:42 +01002624#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002625 /*
2626 * Work around a weird problem: When a file has two links (only
2627 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002628 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002629 * It's correct again after reading the file, thus reset the timestamp
2630 * here.
2631 */
2632 if (newfile && !read_stdin && !read_buffer
2633 && mch_stat((char *)fname, &st) >= 0)
2634 {
2635 buf_store_time(curbuf, &st, fname);
2636 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002637 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002638 }
2639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 msg_scroll = msg_save;
2642
2643#ifdef FEAT_VIMINFO
2644 /*
2645 * Get the marks before executing autocommands, so they can be used there.
2646 */
2647 check_marks_read();
2648#endif
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002651 * We remember if the last line of the read didn't have
2652 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2653 * or writing the read again with 'binary' on. The latter is required
2654 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002656 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002658 // When reloading a buffer put the cursor at the first line that is
2659 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002660 if (flags & READ_KEEP_UNDO)
2661 u_find_first_changed();
2662
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002663#ifdef FEAT_PERSISTENT_UNDO
2664 /*
2665 * When opening a new file locate undo info and read it.
2666 */
2667 if (read_undo_file)
2668 {
2669 char_u hash[UNDO_HASH_SIZE];
2670
2671 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002672 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002673 }
2674#endif
2675
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002676 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 int m = msg_scroll;
2679 int n = msg_scrolled;
2680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 // Save the fileformat now, otherwise the buffer will be considered
2682 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002683 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 save_file_ff(curbuf);
2685
2686 /*
2687 * The output from the autocommands should not overwrite anything and
2688 * should not be overwritten: Set msg_scroll, restore its value if no
2689 * output was done.
2690 */
2691 msg_scroll = TRUE;
2692 if (filtering)
2693 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2694 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002695 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2698 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002699 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2700 /*
2701 * EVENT_FILETYPE was not triggered but the buffer already has a
2702 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2703 */
2704 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2705 TRUE, curbuf);
2706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
2708 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2709 FALSE, NULL, eap);
2710 if (msg_scrolled == n)
2711 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002712# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002713 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002714 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002718 if (!(recoverymode && error))
2719 retval = OK;
2720
2721theend:
2722 if (curbuf->b_ml.ml_mfp != NULL
2723 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2724 // OK to sync the swap file now
2725 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2726
2727 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728}
2729
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002730#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002731/*
2732 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2733 * which is the name of files used for process substitution output by
2734 * some shells on some operating systems, e.g., bash on SunOS.
2735 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2736 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002737 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002738is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002739{
2740 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2741 && VIM_ISDIGIT(fname[8])
2742 && *skipdigits(fname + 9) == NUL
2743 && (fname[9] != NUL
2744 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2745}
2746#endif
2747
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002748/*
2749 * From the current line count and characters read after that, estimate the
2750 * line number where we are now.
2751 * Used for error messages that include a line number.
2752 */
2753 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002754readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002755 linenr_T linecnt, // line count before reading more bytes
2756 char_u *p, // start of more bytes read
2757 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002758{
2759 char_u *s;
2760 linenr_T lnum;
2761
2762 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2763 for (s = p; s < endp; ++s)
2764 if (*s == '\n')
2765 ++lnum;
2766 return lnum;
2767}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769/*
Rob Pilling8196e942022-02-11 15:12:10 +00002770 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002771 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 * Returns OK or FAIL.
2773 */
2774 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002775prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaar13505972019-01-24 15:04:48 +01002777 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (eap->cmd == NULL)
2779 return FAIL;
2780
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002781 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2782 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002783 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002784 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002785
2786 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002787 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002788 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 return OK;
2790}
2791
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002792/*
2793 * Set default or forced 'fileformat' and 'binary'.
2794 */
2795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002796set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002797{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002798 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002799 if (set_options)
2800 {
2801 if (eap != NULL && eap->force_ff != 0)
2802 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2803 else if (*p_ffs != NUL)
2804 set_fileformat(default_fileformat(), OPT_LOCAL);
2805 }
2806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002807 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002808 if (eap != NULL && eap->force_bin != 0)
2809 {
2810 int oldval = curbuf->b_p_bin;
2811
2812 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2813 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2814 }
2815}
2816
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002817/*
2818 * Set forced 'fileencoding'.
2819 */
2820 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002821set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002822{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002823 if (eap->force_enc == 0)
2824 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002825
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002826 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2827
2828 if (fenc != NULL)
2829 set_string_option_direct((char_u *)"fenc", -1,
2830 fenc, OPT_FREE|OPT_LOCAL, 0);
2831 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002832}
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834/*
2835 * Find next fileencoding to use from 'fileencodings'.
2836 * "pp" points to fenc_next. It's advanced to the next item.
2837 * When there are no more items, an empty string is returned and *pp is set to
2838 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002839 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2840 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 */
2842 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002843next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 char_u *p;
2846 char_u *r;
2847
Bram Moolenaarf077db22019-08-13 00:18:24 +02002848 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (**pp == NUL)
2850 {
2851 *pp = NULL;
2852 return (char_u *)"";
2853 }
2854 p = vim_strchr(*pp, ',');
2855 if (p == NULL)
2856 {
2857 r = enc_canonize(*pp);
2858 *pp += STRLEN(*pp);
2859 }
2860 else
2861 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002862 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 *pp = p + 1;
2864 if (r != NULL)
2865 {
2866 p = enc_canonize(r);
2867 vim_free(r);
2868 r = p;
2869 }
2870 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002871 if (r != NULL)
2872 *alloced = TRUE;
2873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002875 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 r = (char_u *)"";
2877 *pp = NULL;
2878 }
2879 return r;
2880}
2881
Bram Moolenaar13505972019-01-24 15:04:48 +01002882#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
2884 * Convert a file with the 'charconvert' expression.
2885 * This closes the file which is to be read, converts it and opens the
2886 * resulting file for reading.
2887 * Returns name of the resulting converted file (the caller should delete it
2888 * after reading it).
2889 * Returns NULL if the conversion failed ("*fdp" is not set) .
2890 */
2891 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002892readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002893 char_u *fname, // name of input file
2894 char_u *fenc, // converted from
2895 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002898 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002900 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002902 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 else
2904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002905 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 *fdp = -1;
2907 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2908 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002909 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2911 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002912 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914
2915 if (errmsg != NULL)
2916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 // Don't use emsg(), it breaks mappings, the retry with
2918 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002919 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (tmpname != NULL)
2921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002923 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
2925 }
2926
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002927 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (*fdp < 0)
2929 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2930
2931 return tmpname;
2932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#endif
2934
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002935#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002937 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2939 * *filesizep are updated.
2940 * Return the (new) encryption key, NULL for no encryption.
2941 */
2942 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002943check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002944 char_u *cryptkey, // previous encryption key or NULL
2945 char_u *ptr, // pointer to read bytes
2946 long *sizep, // length of read bytes
2947 off_T *filesizep, // nr of bytes used from file
2948 int newfile, // editing a new buffer
2949 char_u *fname, // file name to display
2950 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002952 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002953 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002954
2955 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 // Mark the buffer as read-only until the decryption has taken place.
2958 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002959 curbuf->b_p_ro = TRUE;
2960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002961 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002962 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002963 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002968 crypt_check_swapfile_curbuf();
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
2971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 // When newfile is TRUE, store the typed key in the 'key'
2973 // option and don't free it. bf needs hash of the key saved.
2974 // Don't ask for the key again when first time Enter was hit.
2975 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002977 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002978 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002979 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002980 *did_ask = TRUE;
2981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002982 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (cryptkey != NULL && *cryptkey == NUL)
2984 {
2985 if (cryptkey != curbuf->b_p_key)
2986 vim_free(cryptkey);
2987 cryptkey = NULL;
2988 }
2989 }
2990 }
2991
2992 if (cryptkey != NULL)
2993 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002994 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002995
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002996 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002997 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002998 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002999 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02003000
3001 curbuf->b_cryptstate = crypt_create_from_header(
3002 method, cryptkey, ptr);
3003 crypt_set_cm_option(curbuf, method);
3004
3005 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003006 *filesizep += header_len;
3007 *sizep -= header_len;
3008 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003010 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003011 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003014 // When starting to edit a new file which does not have encryption, clear
3015 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003016 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003017 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 return cryptkey;
3020}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003021#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003024 * Return TRUE if a file appears to be read-only from the file permissions.
3025 */
3026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003027check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003028 char_u *fname, // full path to file
3029 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003030{
3031#ifndef USE_MCH_ACCESS
3032 int fd = 0;
3033#endif
3034
3035 return (
3036#ifdef USE_MCH_ACCESS
3037# ifdef UNIX
3038 (perm & 0222) == 0 ||
3039# endif
3040 mch_access((char *)fname, W_OK)
3041#else
3042 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3043 ? TRUE : (close(fd), FALSE)
3044#endif
3045 );
3046}
3047
Bram Moolenaara7870192019-02-14 12:56:36 +01003048#if defined(HAVE_FSYNC) || defined(PROTO)
3049/*
3050 * Call fsync() with Mac-specific exception.
3051 * Return fsync() result: zero for success.
3052 */
3053 int
3054vim_fsync(int fd)
3055{
3056 int r;
3057
3058# ifdef MACOS_X
3059 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003060 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003061# endif
3062 r = fsync(fd);
3063 return r;
3064}
3065#endif
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003068 * Set the name of the current buffer. Use when the buffer doesn't have a
3069 * name and a ":r" or ":w" command with a file name is used.
3070 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003071 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003073{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003074 buf_T *buf = curbuf;
3075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003076 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003077 if (curbuf->b_p_bl)
3078 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3079 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003080#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003081 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003082 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003083#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003084 if (curbuf != buf)
3085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003086 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003087 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003088 return FAIL;
3089 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003090
3091 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3092 curbuf->b_flags |= BF_NOTEDITED;
3093
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003094 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003095 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3096 if (curbuf->b_p_bl)
3097 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003098#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003099 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003100 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003101#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003103 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003104 if (*curbuf->b_p_ft == NUL)
3105 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003106 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003107 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003108 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003109 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003110
3111 return OK;
3112}
3113
3114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * Put file name into IObuff with quotes.
3116 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003118msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 if (fname == NULL)
3121 fname = (char_u *)"-stdin-";
3122 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3123 IObuff[0] = '"';
3124 STRCAT(IObuff, "\" ");
3125}
3126
3127/*
3128 * Append message for text mode to IObuff.
3129 * Return TRUE if something appended.
3130 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003131 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003132msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134#ifndef USE_CRNL
3135 if (eol_type == EOL_DOS)
3136 {
3137 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3138 return TRUE;
3139 }
3140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (eol_type == EOL_MAC)
3142 {
3143 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3144 return TRUE;
3145 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003146#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (eol_type == EOL_UNIX)
3148 {
3149 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3150 return TRUE;
3151 }
3152#endif
3153 return FALSE;
3154}
3155
3156/*
3157 * Append line and character count to IObuff.
3158 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003159 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003160msg_add_lines(
3161 int insert_space,
3162 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003163 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 char_u *p;
3166
3167 p = IObuff + STRLEN(IObuff);
3168
3169 if (insert_space)
3170 *p++ = ' ';
3171 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003172 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003173 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
3175 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003176 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003178 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003179 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003180 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182}
3183
3184/*
3185 * Append message for missing line separator to IObuff.
3186 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003188msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3191}
3192
Bram Moolenaar473952e2019-09-28 16:30:04 +02003193 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003194time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
ichizokdef69df2021-10-15 17:23:12 +01003196 return
3197#ifdef ST_MTIM_NSEC
3198 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#endif
ichizokdef69df2021-10-15 17:23:12 +01003200#if defined(__linux__) || defined(MSWIN)
3201 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3202 // the seconds. Since the roundoff is done when flushing the inode, the
3203 // time may change unexpectedly by one second!!!
3204 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3205#else
3206 (long)st->st_mtime != mtime
3207#endif
3208 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
3210
3211/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003212 * Return TRUE if file encoding "fenc" requires conversion from or to
3213 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003215 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003216need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003218 int same_encoding;
3219 int enc_flags;
3220 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003222 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003223 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003224 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003225 fenc_flags = 0;
3226 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003227 else
3228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3230 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003231 enc_flags = get_fio_flags(p_enc);
3232 fenc_flags = get_fio_flags(fenc);
3233 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3234 }
3235 if (same_encoding)
3236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003237 // Specified encoding matches with 'encoding'. This requires
3238 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003239 return enc_unicode != 0;
3240 }
3241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242 // Encodings differ. However, conversion is not needed when 'enc' is any
3243 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003244 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245}
3246
3247/*
3248 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3249 * internal conversion.
3250 * if "ptr" is an empty string, use 'encoding'.
3251 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003252 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003253get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 int prop;
3256
3257 if (*ptr == NUL)
3258 ptr = p_enc;
3259
3260 prop = enc_canon_props(ptr);
3261 if (prop & ENC_UNICODE)
3262 {
3263 if (prop & ENC_2BYTE)
3264 {
3265 if (prop & ENC_ENDIAN_L)
3266 return FIO_UCS2 | FIO_ENDIAN_L;
3267 return FIO_UCS2;
3268 }
3269 if (prop & ENC_4BYTE)
3270 {
3271 if (prop & ENC_ENDIAN_L)
3272 return FIO_UCS4 | FIO_ENDIAN_L;
3273 return FIO_UCS4;
3274 }
3275 if (prop & ENC_2WORD)
3276 {
3277 if (prop & ENC_ENDIAN_L)
3278 return FIO_UTF16 | FIO_ENDIAN_L;
3279 return FIO_UTF16;
3280 }
3281 return FIO_UTF8;
3282 }
3283 if (prop & ENC_LATIN1)
3284 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003285 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return 0;
3287}
3288
Bram Moolenaar473952e2019-09-28 16:30:04 +02003289#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290/*
3291 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3292 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3293 * Used for conversion between 'encoding' and 'fileencoding'.
3294 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003295 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003296get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 int cp;
3299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003300 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (!enc_utf8 && enc_codepage <= 0)
3302 return 0;
3303
3304 cp = encname2codepage(ptr);
3305 if (cp == 0)
3306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003307# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (STRCMP(ptr, "utf-8") == 0)
3309 cp = CP_UTF8;
3310 else
3311# endif
3312 return 0;
3313 }
3314 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3315}
3316#endif
3317
Bram Moolenaar473952e2019-09-28 16:30:04 +02003318#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319/*
3320 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3321 * needed for the internal conversion to/from utf-8 or latin1.
3322 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003323 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003324get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3327 && (enc_canon_props(ptr) & ENC_MACROMAN))
3328 return FIO_MACROMAN;
3329 return 0;
3330}
3331#endif
3332
3333/*
3334 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3335 * "size" must be at least 2.
3336 * Return the name of the encoding and set "*lenp" to the length.
3337 * Returns NULL when no BOM found.
3338 */
3339 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003340check_for_bom(
3341 char_u *p,
3342 long size,
3343 int *lenp,
3344 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 char *name = NULL;
3347 int len = 2;
3348
3349 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003350 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003352 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 len = 3;
3354 }
3355 else if (p[0] == 0xff && p[1] == 0xfe)
3356 {
3357 if (size >= 4 && p[2] == 0 && p[3] == 0
3358 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003360 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 len = 4;
3362 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003363 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003364 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003365 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003366 // utf-16le is preferred, it also works for ucs-2le text
3367 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 else if (p[0] == 0xfe && p[1] == 0xff
3370 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003373 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003374 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003375 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003376 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3379 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003381 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 len = 4;
3383 }
3384
3385 *lenp = len;
3386 return (char_u *)name;
3387}
3388
3389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * Try to find a shortname by comparing the fullname with the current
3391 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003392 * Returns "full_path" or pointer into "full_path" if shortened.
3393 */
3394 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003395shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003396{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003397 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003398 char_u *p = full_path;
3399
Bram Moolenaard9462e32011-04-11 21:35:11 +02003400 dirname = alloc(MAXPATHL);
3401 if (dirname == NULL)
3402 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003403 if (mch_dirname(dirname, MAXPATHL) == OK)
3404 {
3405 p = shorten_fname(full_path, dirname);
3406 if (p == NULL || *p == NUL)
3407 p = full_path;
3408 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003409 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003410 return p;
3411}
3412
3413/*
3414 * Try to find a shortname by comparing the fullname with the current
3415 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 * Returns NULL if not shorter name possible, pointer into "full_path"
3417 * otherwise.
3418 */
3419 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003420shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 int len;
3423 char_u *p;
3424
3425 if (full_path == NULL)
3426 return NULL;
3427 len = (int)STRLEN(dir_name);
3428 if (fnamencmp(dir_name, full_path, len) == 0)
3429 {
3430 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003431#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003433 * MS-Windows: when a file is in the root directory, dir_name will end
3434 * in a slash, since C: by itself does not define a specific dir. In
3435 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 if (!((len > 2) && (*(p - 2) == ':')))
3438#endif
3439 {
3440 if (vim_ispathsep(*p))
3441 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003442#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else
3444 p = NULL;
3445#endif
3446 }
3447 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003448#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 /*
3450 * When using a file in the current drive, remove the drive name:
3451 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3452 * a floppy from "A:\dir" to "B:\dir".
3453 */
3454 else if (len > 3
3455 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3456 && full_path[1] == ':'
3457 && vim_ispathsep(full_path[2]))
3458 p = full_path + 2;
3459#endif
3460 else
3461 p = NULL;
3462 return p;
3463}
3464
3465/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003466 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 * When "force" is TRUE: Use full path from now on for files currently being
3468 * edited, both for file name and swap file name. Try to shorten the file
3469 * names a bit, if safe to do so.
3470 * When "force" is FALSE: Only try to shorten absolute file names.
3471 * For buffers that have buftype "nofile" or "scratch": never change the file
3472 * name.
3473 */
3474 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003475shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3476{
3477 char_u *p;
3478
3479 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003480 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003481 && !path_with_url(buf->b_fname)
3482 && (force
3483 || buf->b_sfname == NULL
3484 || mch_isFullName(buf->b_sfname)))
3485 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003486 if (buf->b_sfname != buf->b_ffname)
3487 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003488 p = shorten_fname(buf->b_ffname, dirname);
3489 if (p != NULL)
3490 {
3491 buf->b_sfname = vim_strsave(p);
3492 buf->b_fname = buf->b_sfname;
3493 }
3494 if (p == NULL || buf->b_fname == NULL)
3495 buf->b_fname = buf->b_ffname;
3496 }
3497}
3498
3499/*
3500 * Shorten filenames for all buffers.
3501 */
3502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 char_u dirname[MAXPATHL];
3506 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003509 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003511 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003513 // Always make the swap file name a full path, a "nofile" buffer may
3514 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003515 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003518 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003519#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003520 popup_update_preview_title();
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3525 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003526 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 || defined(PROTO)
3528/*
3529 * Shorten all filenames in "fnames[count]" by current directory.
3530 */
3531 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003532shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 int i;
3535 char_u dirname[MAXPATHL];
3536 char_u *p;
3537
3538 if (fnames == NULL || count < 1)
3539 return;
3540 mch_dirname(dirname, sizeof(dirname));
3541 for (i = 0; i < count; ++i)
3542 {
3543 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3544 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 // shorten_fname() returns pointer in given "fnames[i]". If free
3546 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3547 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 p = vim_strsave(p);
3549 vim_free(fnames[i]);
3550 fnames[i] = p;
3551 }
3552 }
3553}
3554#endif
3555
3556/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003557 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * fo_o_h.ext for MSDOS or when shortname option set.
3559 *
3560 * Assumed that fname is a valid name found in the filesystem we assure that
3561 * the return value is a different name and ends in 'ext'.
3562 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3563 * characters otherwise.
3564 * Space for the returned name is allocated, must be freed later.
3565 * Returns NULL when out of memory.
3566 */
3567 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003568modname(
3569 char_u *fname,
3570 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003571 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003573 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 fname, ext, prepend_dot);
3575}
3576
3577 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003578buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003579 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003580 char_u *fname,
3581 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003582 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
3584 char_u *retval;
3585 char_u *s;
3586 char_u *e;
3587 char_u *ptr;
3588 int fnamelen, extlen;
3589
3590 extlen = (int)STRLEN(ext);
3591
3592 /*
3593 * If there is no file name we must get the name of the current directory
3594 * (we need the full path in case :cd is used).
3595 */
3596 if (fname == NULL || *fname == NUL)
3597 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003598 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (retval == NULL)
3600 return NULL;
3601 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3602 (fnamelen = (int)STRLEN(retval)) == 0)
3603 {
3604 vim_free(retval);
3605 return NULL;
3606 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003607 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 retval[fnamelen++] = PATHSEP;
3610 retval[fnamelen] = NUL;
3611 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003612 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 else
3615 {
3616 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003617 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (retval == NULL)
3619 return NULL;
3620 STRCPY(retval, fname);
3621#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003622 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#endif
3624 }
3625
3626 /*
3627 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3628 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3629 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3630 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3631 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003632 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003634 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003635 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003638 {
3639 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003644 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3646 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 s = ptr + STRLEN(ptr);
3649
3650 /*
3651 * For 8.3 file names we may have to reduce the length.
3652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 /*
3656 * If there is no file name, or the file name ends in '/', and the
3657 * extension starts with '.', put a '_' before the dot, because just
3658 * ".ext" is invalid.
3659 */
3660 if (fname == NULL || *fname == NUL
3661 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 *s++ = '_';
3665 }
3666 /*
3667 * If the extension starts with '.', truncate the base name at 8
3668 * characters
3669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003672 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 s = ptr + 8;
3675 *s = '\0';
3676 }
3677 }
3678 /*
3679 * If the extension doesn't start with '.', and the file name
3680 * doesn't have an extension yet, append a '.'
3681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 else if ((e = vim_strchr(ptr, '.')) == NULL)
3683 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003686 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
3688 else if ((int)STRLEN(e) + extlen > 4)
3689 s = e + 4 - extlen;
3690 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003691#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 /*
3693 * If there is no file name, and the extension starts with '.', put a
3694 * '_' before the dot, because just ".ext" may be invalid if it's on a
3695 * FAT partition, and on HPFS it doesn't matter.
3696 */
3697 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3698 *s++ = '_';
3699#endif
3700
3701 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003702 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * ext can start with '.' and cannot exceed 3 more characters.
3704 */
3705 STRCPY(s, ext);
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 /*
3708 * Prepend the dot.
3709 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003710 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003712 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 /*
3717 * Check that, after appending the extension, the file name is really
3718 * different.
3719 */
3720 if (fname != NULL && STRCMP(fname, retval) == 0)
3721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 while (--s >= ptr)
3724 {
3725 if (*s != '_')
3726 {
3727 *s = '_';
3728 break;
3729 }
3730 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 *ptr = 'v';
3733 }
3734 return retval;
3735}
3736
3737/*
3738 * Like fgets(), but if the file line is too long, it is truncated and the
3739 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003740 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
3742 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003743vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 char *eof;
3746#define FGETS_SIZE 200
3747 char tbuf[FGETS_SIZE];
3748
3749 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003753 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003755 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 do
3757 {
3758 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003759 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3761 }
3762 return (eof == NULL);
3763}
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765/*
3766 * rename() only works if both files are on the same file system, this
3767 * function will (attempts to?) copy the file across if rename fails -- webb
3768 * Return -1 for failure, 0 for success.
3769 */
3770 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003771vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 int fd_in;
3774 int fd_out;
3775 int n;
3776 char *errmsg = NULL;
3777 char *buffer;
3778#ifdef AMIGA
3779 BPTR flock;
3780#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003781 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003782 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003783#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003784 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003785#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003786 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003789 * When the names are identical, there is nothing to do. When they refer
3790 * to the same file (ignoring case and slash/backslash differences) but
3791 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 */
3793 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003794 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003795 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003796 use_tmp_file = TRUE;
3797 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003798 return 0;
3799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 /*
3802 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3803 */
3804 if (mch_stat((char *)from, &st) < 0)
3805 return -1;
3806
Bram Moolenaar3576da72008-12-30 15:15:57 +00003807#ifdef UNIX
3808 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003809 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003811 // It's possible for the source and destination to be the same file.
3812 // This happens when "from" and "to" differ in case and are on a FAT32
3813 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003814 if (mch_stat((char *)to, &st_to) >= 0
3815 && st.st_dev == st_to.st_dev
3816 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003817 use_tmp_file = TRUE;
3818 }
3819#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003820#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003821 {
3822 BY_HANDLE_FILE_INFORMATION info1, info2;
3823
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003824 // It's possible for the source and destination to be the same file.
3825 // In that case go through a temp file name. This makes rename("foo",
3826 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003827 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3828 && win32_fileinfo(to, &info2) == FILEINFO_OK
3829 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3830 && info1.nFileIndexHigh == info2.nFileIndexHigh
3831 && info1.nFileIndexLow == info2.nFileIndexLow)
3832 use_tmp_file = TRUE;
3833 }
3834#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003835
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003836 if (use_tmp_file)
3837 {
3838 char tempname[MAXPATHL + 1];
3839
3840 /*
3841 * Find a name that doesn't exist and is in the same directory.
3842 * Rename "from" to "tempname" and then rename "tempname" to "to".
3843 */
3844 if (STRLEN(from) >= MAXPATHL - 5)
3845 return -1;
3846 STRCPY(tempname, from);
3847 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003848 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003849 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3850 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003851 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003852 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003853 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003854 if (mch_rename(tempname, (char *)to) == 0)
3855 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003856 // Strange, the second step failed. Try moving the
3857 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003858 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003859 return -1;
3860 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003861 // If it fails for one temp name it will most likely fail
3862 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003863 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003864 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003865 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003866 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003867 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 /*
3870 * Delete the "to" file, this is required on some systems to make the
3871 * mch_rename() work, on other systems it makes sure that we don't have
3872 * two files when the mch_rename() fails.
3873 */
3874
3875#ifdef AMIGA
3876 /*
3877 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3878 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003879 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 * deleting the "from" file (horror!) we lock it during the remove.
3881 *
3882 * When used for making a backup before writing the file: This should not
3883 * happen with ":w", because startscript() should detect this problem and
3884 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3885 * name. This problem does exist with ":w filename", but then the
3886 * original file will be somewhere else so the backup isn't really
3887 * important. If autoscripting is off the rename may fail.
3888 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003889 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890#endif
3891 mch_remove(to);
3892#ifdef AMIGA
3893 if (flock)
3894 UnLock(flock);
3895#endif
3896
3897 /*
3898 * First try a normal rename, return if it works.
3899 */
3900 if (mch_rename((char *)from, (char *)to) == 0)
3901 return 0;
3902
3903 /*
3904 * Rename() failed, try copying the file.
3905 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003906 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003907#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003908 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003909 acl = mch_get_acl(from);
3910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3912 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003913 {
3914#ifdef HAVE_ACL
3915 mch_free_acl(acl);
3916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003918 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003921 fd_out = mch_open((char *)to,
3922 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (fd_out == -1)
3924 {
3925 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003926#ifdef HAVE_ACL
3927 mch_free_acl(acl);
3928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return -1;
3930 }
3931
Bram Moolenaar473952e2019-09-28 16:30:04 +02003932 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (buffer == NULL)
3934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003936 close(fd_in);
3937#ifdef HAVE_ACL
3938 mch_free_acl(acl);
3939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return -1;
3941 }
3942
Bram Moolenaar473952e2019-09-28 16:30:04 +02003943 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003944 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003946 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 }
3949
3950 vim_free(buffer);
3951 close(fd_in);
3952 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003953 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (n < 0)
3955 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003956 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 to = from;
3958 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003960 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003961#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003962#ifdef HAVE_ACL
3963 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003964 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003965#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003966#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003967 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (errmsg != NULL)
3970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003971 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return -1;
3973 }
3974 mch_remove(from);
3975 return 0;
3976}
3977
3978static int already_warned = FALSE;
3979
3980/*
3981 * Check if any not hidden buffer has been changed.
3982 * Postpone the check if there are characters in the stuff buffer, a global
3983 * command is being executed, a mapping is being executed or an autocommand is
3984 * busy.
3985 * Returns TRUE if some message was written (screen should be redrawn and
3986 * cursor positioned).
3987 */
3988 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003989check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003990 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 buf_T *buf;
3993 int didit = 0;
3994 int n;
3995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003996 // Don't check timestamps while system() or another low-level function may
3997 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (no_check_timestamps > 0)
3999 return FALSE;
4000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004001 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
4002 // event and we would keep on checking if the file is steadily growing.
4003 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (focus && did_check_timestamps)
4005 {
4006 need_check_timestamps = TRUE;
4007 return FALSE;
4008 }
4009
4010 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004011 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
4014 {
4015 ++no_wait_return;
4016 did_check_timestamps = TRUE;
4017 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004018 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004020 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (buf->b_nwindows > 0)
4022 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004023 bufref_T bufref;
4024
4025 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 n = buf_check_timestamp(buf, focus);
4027 if (didit < n)
4028 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004029 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004031 // Autocommands have removed the buffer, start at the
4032 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 buf = firstbuf;
4034 continue;
4035 }
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 --no_wait_return;
4039 need_check_timestamps = FALSE;
4040 if (need_wait_return && didit == 2)
4041 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004042 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004043 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 out_flush();
4045 }
4046 }
4047 return didit;
4048}
4049
4050/*
4051 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4052 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4053 * empty.
4054 */
4055 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004056move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 buf_T *tbuf = curbuf;
4059 int retval = OK;
4060 linenr_T lnum;
4061 char_u *p;
4062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 curbuf = tobuf;
4065 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4066 {
4067 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4068 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4069 {
4070 vim_free(p);
4071 retval = FAIL;
4072 break;
4073 }
4074 vim_free(p);
4075 }
4076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004077 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (retval != FAIL)
4079 {
4080 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004081 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004082 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004084 // Oops! We could try putting back the saved lines, but that
4085 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 retval = FAIL;
4087 break;
4088 }
4089 }
4090
4091 curbuf = tbuf;
4092 return retval;
4093}
4094
4095/*
4096 * Check if buffer "buf" has been changed.
4097 * Also check if the file for a new buffer unexpectedly appeared.
4098 * return 1 if a changed buffer was found.
4099 * return 2 if a message has been displayed.
4100 * return 0 otherwise.
4101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004103buf_check_timestamp(
4104 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004105 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004107 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int stat_res;
4109 int retval = 0;
4110 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004111 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004113 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004115 enum {
4116 RELOAD_NONE,
4117 RELOAD_NORMAL,
4118 RELOAD_DETECT
4119 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004120 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4122 int can_reload = FALSE;
4123#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004124 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 int orig_mode = buf->b_orig_mode;
4126#ifdef FEAT_GUI
4127 int save_mouse_correct = need_mouse_correct;
4128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004130 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004131#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004132 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004133#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004134 bufref_T bufref;
4135
4136 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004138 // If there is no file name, the buffer is not loaded, 'buftype' is
4139 // set, we are in the middle of a save or being called recursively: ignore
4140 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (buf->b_ffname == NULL
4142 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004143 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004146#ifdef FEAT_NETBEANS_INTG
4147 || isNetbeansBuffer(buf)
4148#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004149#ifdef FEAT_TERMINAL
4150 || buf->b_term != NULL
4151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 )
4153 return 0;
4154
4155 if ( !(buf->b_flags & BF_NOTEDITED)
4156 && buf->b_mtime != 0
4157 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004158 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004159 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160#ifdef HAVE_ST_MODE
4161 || (int)st.st_mode != buf->b_orig_mode
4162#else
4163 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4164#endif
4165 ))
4166 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004167 long prev_b_mtime = buf->b_mtime;
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 retval = 1;
4170
Bram Moolenaar386bc822018-07-07 18:34:12 +02004171 // set b_mtime to stop further warnings (e.g., when executing
4172 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (stat_res < 0)
4174 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004175 // Check the file again later to see if it re-appears.
4176 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 buf->b_orig_size = 0;
4178 buf->b_orig_mode = 0;
4179 }
4180 else
4181 buf_store_time(buf, &st, buf->b_ffname);
4182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004183 // Don't do anything for a directory. Might contain the file
4184 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (mch_isdir(buf->b_fname))
4186 ;
4187
4188 /*
4189 * If 'autoread' is set, the buffer has no changes and the file still
4190 * exists, reload the buffer. Use the buffer-local option value if it
4191 * was set, the global option value otherwise.
4192 */
4193 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4194 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004195 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 else
4197 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004198 if (stat_res < 0)
4199 reason = "deleted";
4200 else if (bufIsChanged(buf))
4201 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004202 /*
4203 * Check if the file contents really changed to avoid giving a
4204 * warning when only the timestamp was set (e.g., checked out of
4205 * CVS). Always warn when the buffer was changed.
4206 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004207 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4208 reason = "changed";
4209 else if (orig_mode != buf->b_orig_mode)
4210 reason = "mode";
4211 else
4212 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 /*
4215 * Only give the warning if there are no FileChangedShell
4216 * autocommands.
4217 * Avoid being called recursively by setting "busy".
4218 */
4219 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004220#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004221 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4222 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004223#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004224 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4226 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004227 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 busy = FALSE;
4229 if (n)
4230 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004231 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004232 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004233#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 s = get_vim_var_str(VV_FCS_CHOICE);
4235 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004236 reload = RELOAD_NORMAL;
4237 else if (STRCMP(s, "edit") == 0)
4238 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004239 else if (STRCMP(s, "ask") == 0)
4240 n = FALSE;
4241 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004242#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004243 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004245 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004247 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004248 {
4249 // Only give the message once.
4250 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004251 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 else
4254 {
4255 helpmesg = TRUE;
4256#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4257 can_reload = TRUE;
4258#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004259 if (reason[2] == 'n')
4260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004262 mesg2 = _("See \":help W12\" for more info.");
4263 }
4264 else if (reason[1] == 'h')
4265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004267 mesg2 = _("See \":help W11\" for more info.");
4268 }
4269 else if (*reason == 'm')
4270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004272 mesg2 = _("See \":help W16\" for more info.");
4273 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004274 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 // Only timestamp changed, store it to avoid a warning
4277 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004278 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004279 buf->b_mtime_read_ns = buf->b_mtime_ns;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283 }
4284
4285 }
4286 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4287 && vim_fexists(buf->b_ffname))
4288 {
4289 retval = 1;
4290 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4291 buf->b_flags |= BF_NEW_W;
4292#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4293 can_reload = TRUE;
4294#endif
4295 }
4296
4297 if (mesg != NULL)
4298 {
4299 path = home_replace_save(buf, buf->b_fname);
4300 if (path != NULL)
4301 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004302 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004304 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004305 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004306#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 // Set warningmsg here, before the unimportant and output-specific
4308 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004309 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4312 if (can_reload)
4313 {
4314 if (*mesg2 != NUL)
4315 {
4316 STRCAT(tbuf, "\n");
4317 STRCAT(tbuf, mesg2);
4318 }
Rob Pilling8196e942022-02-11 15:12:10 +00004319 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4320 (char_u *)tbuf,
4321 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4322 1, NULL, TRUE))
4323 {
4324 case 2:
4325 reload = RELOAD_NORMAL;
4326 break;
4327 case 3:
4328 reload = RELOAD_DETECT;
4329 break;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 else
4333#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004334 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4335 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 if (*mesg2 != NUL)
4338 {
4339 STRCAT(tbuf, "; ");
4340 STRCAT(tbuf, mesg2);
4341 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004342 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 retval = 2;
4344 }
4345 else
4346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004350 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004352 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 msg_clr_eos();
4354 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004355 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004358#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004360#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004362 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 redraw_cmdline = FALSE;
4366 }
4367 }
4368 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 vim_free(path);
4372 vim_free(tbuf);
4373 }
4374 }
4375
Rob Pilling8196e942022-02-11 15:12:10 +00004376 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004377 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004379 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004380#ifdef FEAT_PERSISTENT_UNDO
4381 if (buf->b_p_udf && buf->b_ffname != NULL)
4382 {
4383 char_u hash[UNDO_HASH_SIZE];
4384 buf_T *save_curbuf = curbuf;
4385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004387 curbuf = buf;
4388 u_compute_hash(hash);
4389 u_write_undo(NULL, FALSE, buf, hash);
4390 curbuf = save_curbuf;
4391 }
4392#endif
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004395 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004396 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004397 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4398 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 // restore this in case an autocommand has set it; it would break
4401 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 need_mouse_correct = save_mouse_correct;
4403#endif
4404
4405 return retval;
4406}
4407
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004408/*
4409 * Reload a buffer that is already loaded.
4410 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004411 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4412 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004413 */
4414 void
Rob Pilling8196e942022-02-11 15:12:10 +00004415buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004416{
4417 exarg_T ea;
4418 pos_T old_cursor;
4419 linenr_T old_topline;
4420 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004421 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004422 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004424 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004425 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004426 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004427
Bram Moolenaare76062c2022-11-28 18:51:43 +00004428 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004429 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004430 if (curbuf != buf)
4431 {
4432 // Failed to find a window for "buf", it is dangerous to continue,
4433 // better bail out.
4434 return;
4435 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004436
Rob Pilling8196e942022-02-11 15:12:10 +00004437 // Unless reload_options is set, we only want to read the text from the
4438 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4439 // Force the fileformat and encoding to be the same.
4440 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004441 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004442 else
4443 prepped = prep_exarg(&ea, buf);
4444
4445 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004446 {
4447 old_cursor = curwin->w_cursor;
4448 old_topline = curwin->w_topline;
4449
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004450 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 // Save all the text, so that the reload can be undone.
4453 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004454 u_sync(FALSE);
4455 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4456 flags |= READ_KEEP_UNDO;
4457 }
4458
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004459 /*
4460 * To behave like when a new file is edited (matters for
4461 * BufReadPost autocommands) we first need to delete the current
4462 * buffer contents. But if reading the file fails we should keep
4463 * the old contents. Can't use memory only, the file might be
4464 * too big. Use a hidden buffer to move the buffer contents to.
4465 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004466 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004467 savebuf = NULL;
4468 else
4469 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004470 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004471 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004472 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004473 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004476 curbuf = savebuf;
4477 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004478 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004479 curbuf = buf;
4480 curwin->w_buffer = buf;
4481 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004482 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004483 || move_lines(buf, savebuf) == FAIL)
4484 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004485 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004486 saved = FAIL;
4487 }
4488 }
4489
4490 if (saved == OK)
4491 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4493 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004494 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4495 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004496 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004497 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004498#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004499 if (!aborting())
4500#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004501 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004502 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 // Put the text back from the save buffer. First
4505 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004506 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004507 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004508 break;
4509 (void)move_lines(savebuf, buf);
4510 }
4511 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004512 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004515 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004516 if ((flags & READ_KEEP_UNDO) == 0)
4517 {
4518 u_blockfree(buf);
4519 u_clearall(buf);
4520 }
4521 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004522 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004523 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004524 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004525 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004526 }
4527 }
4528 vim_free(ea.cmd);
4529
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004530 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004531 wipe_buffer(savebuf, FALSE);
4532
4533#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004534 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004535 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004536#endif
4537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004538 // Restore the topline and cursor position and check it (lines may
4539 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004540 if (old_topline > curbuf->b_ml.ml_line_count)
4541 curwin->w_topline = curbuf->b_ml.ml_line_count;
4542 else
4543 curwin->w_topline = old_topline;
4544 curwin->w_cursor = old_cursor;
4545 check_cursor();
4546 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004547 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004548#ifdef FEAT_FOLDING
4549 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004550 win_T *wp;
4551 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004553 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004554 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004555 if (wp->w_buffer == curwin->w_buffer
4556 && !foldmethodIsManual(wp))
4557 foldUpdateAll(wp);
4558 }
4559#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004560 // If the mode didn't change and 'readonly' was set, keep the old
4561 // value; the user probably used the ":view" command. But don't
4562 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004563 if (orig_mode == curbuf->b_orig_mode)
4564 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004567 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004568 }
4569
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004570 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004571 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004572 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004573}
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004576buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004579#ifdef ST_MTIM_NSEC
4580 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4581#else
4582 buf->b_mtime_ns = 0;
4583#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004584 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#ifdef HAVE_ST_MODE
4586 buf->b_orig_mode = (int)st->st_mode;
4587#else
4588 buf->b_orig_mode = mch_getperm(fname);
4589#endif
4590}
4591
4592/*
4593 * Adjust the line with missing eol, used for the next write.
4594 * Used for do_filter(), when the input lines for the filter are deleted.
4595 */
4596 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004597write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004599 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004600 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601}
4602
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004603// Subfuncions for readdirex()
4604#ifdef FEAT_EVAL
4605# ifdef MSWIN
4606 static char_u *
4607getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4608{
4609 stat_T st;
4610 unsigned short st_mode;
4611 DWORD flag = wfd->dwFileAttributes;
4612 WCHAR *wp;
4613
4614 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4615 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4616 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4617 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4618
4619 wp = wcsrchr(wfd->cFileName, L'.');
4620 if (wp != NULL)
4621 {
4622 if (_wcsicmp(wp, L".exe") == 0 ||
4623 _wcsicmp(wp, L".com") == 0 ||
4624 _wcsicmp(wp, L".cmd") == 0 ||
4625 _wcsicmp(wp, L".bat") == 0)
4626 st_mode |= _S_IEXEC;
4627 }
4628
4629 // Copy user bits to group/other.
4630 st_mode |= (st_mode & 0700) >> 3;
4631 st_mode |= (st_mode & 0700) >> 6;
4632
4633 st.st_mode = st_mode;
4634 return getfpermst(&st, perm);
4635}
4636
4637 static char_u *
4638getftypewfd(WIN32_FIND_DATAW *wfd)
4639{
4640 DWORD flag = wfd->dwFileAttributes;
4641 DWORD tag = wfd->dwReserved0;
4642
4643 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4644 {
4645 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4646 return (char_u*)"junction";
4647 else if (tag == IO_REPARSE_TAG_SYMLINK)
4648 {
4649 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4650 return (char_u*)"linkd";
4651 else
4652 return (char_u*)"link";
4653 }
4654 return (char_u*)"reparse"; // unknown reparse point type
4655 }
4656 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4657 return (char_u*)"dir";
4658 else
4659 return (char_u*)"file";
4660}
4661
4662 static dict_T *
4663create_readdirex_item(WIN32_FIND_DATAW *wfd)
4664{
4665 dict_T *item;
4666 char_u *p;
4667 varnumber_T size, time;
4668 char_u permbuf[] = "---------";
4669
4670 item = dict_alloc();
4671 if (item == NULL)
4672 return NULL;
4673 item->dv_refcount++;
4674
4675 p = utf16_to_enc(wfd->cFileName, NULL);
4676 if (p == NULL)
4677 goto theend;
4678 if (dict_add_string(item, "name", p) == FAIL)
4679 {
4680 vim_free(p);
4681 goto theend;
4682 }
4683 vim_free(p);
4684
4685 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4686 if (dict_add_number(item, "size", size) == FAIL)
4687 goto theend;
4688
4689 // Convert FILETIME to unix time.
4690 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4691 wfd->ftLastWriteTime.dwLowDateTime)
4692 - 116444736000000000) / 10000000;
4693 if (dict_add_number(item, "time", time) == FAIL)
4694 goto theend;
4695
4696 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4697 goto theend;
4698 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4699 goto theend;
4700
4701 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4702 goto theend;
4703 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4704 goto theend;
4705
4706 return item;
4707
4708theend:
4709 dict_unref(item);
4710 return NULL;
4711}
4712# else
4713 static dict_T *
4714create_readdirex_item(char_u *path, char_u *name)
4715{
4716 dict_T *item;
4717 char *p;
4718 size_t len;
4719 stat_T st;
4720 int ret, link = FALSE;
4721 varnumber_T size;
4722 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004723 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004724 struct passwd *pw;
4725 struct group *gr;
4726
4727 item = dict_alloc();
4728 if (item == NULL)
4729 return NULL;
4730 item->dv_refcount++;
4731
4732 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4733 p = alloc(len);
4734 if (p == NULL)
4735 goto theend;
4736 vim_snprintf(p, len, "%s/%s", path, name);
4737 ret = mch_lstat(p, &st);
4738 if (ret >= 0 && S_ISLNK(st.st_mode))
4739 {
4740 link = TRUE;
4741 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004742 if (ret < 0)
4743 q = (char_u*)"link";
4744
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004745 }
4746 vim_free(p);
4747
4748 if (dict_add_string(item, "name", name) == FAIL)
4749 goto theend;
4750
4751 if (ret >= 0)
4752 {
4753 size = (varnumber_T)st.st_size;
4754 if (S_ISDIR(st.st_mode))
4755 size = 0;
4756 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004757 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004758 size = -2;
4759 if (dict_add_number(item, "size", size) == FAIL)
4760 goto theend;
4761 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4762 goto theend;
4763
4764 if (link)
4765 {
4766 if (S_ISDIR(st.st_mode))
4767 q = (char_u*)"linkd";
4768 else
4769 q = (char_u*)"link";
4770 }
4771 else
4772 q = getftypest(&st);
4773 if (dict_add_string(item, "type", q) == FAIL)
4774 goto theend;
4775 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4776 goto theend;
4777
4778 pw = getpwuid(st.st_uid);
4779 if (pw == NULL)
4780 q = (char_u*)"";
4781 else
4782 q = (char_u*)pw->pw_name;
4783 if (dict_add_string(item, "user", q) == FAIL)
4784 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004785# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004786 gr = getgrgid(st.st_gid);
4787 if (gr == NULL)
4788 q = (char_u*)"";
4789 else
4790 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004791# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004792 if (dict_add_string(item, "group", q) == FAIL)
4793 goto theend;
4794 }
4795 else
4796 {
4797 if (dict_add_number(item, "size", -1) == FAIL)
4798 goto theend;
4799 if (dict_add_number(item, "time", -1) == FAIL)
4800 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004801 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004802 goto theend;
4803 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4804 goto theend;
4805 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4806 goto theend;
4807 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4808 goto theend;
4809 }
4810 return item;
4811
4812theend:
4813 dict_unref(item);
4814 return NULL;
4815}
4816# endif
4817
4818 static int
4819compare_readdirex_item(const void *p1, const void *p2)
4820{
4821 char_u *name1, *name2;
4822
Bram Moolenaard61efa52022-07-23 09:52:04 +01004823 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4824 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004825 if (readdirex_sort == READDIR_SORT_BYTE)
4826 return STRCMP(name1, name2);
4827 else if (readdirex_sort == READDIR_SORT_IC)
4828 return STRICMP(name1, name2);
4829 else
4830 return STRCOLL(name1, name2);
4831}
4832
4833 static int
4834compare_readdir_item(const void *s1, const void *s2)
4835{
4836 if (readdirex_sort == READDIR_SORT_BYTE)
4837 return STRCMP(*(char **)s1, *(char **)s2);
4838 else if (readdirex_sort == READDIR_SORT_IC)
4839 return STRICMP(*(char **)s1, *(char **)s2);
4840 else
4841 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004842}
4843#endif
4844
Bram Moolenaarda440d22016-01-16 21:27:23 +01004845#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4846/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004847 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004848 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004849 * If "withattr" is TRUE, retrieve the names and their attributes.
4850 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004851 * Return OK for success, FAIL for failure.
4852 */
4853 int
4854readdir_core(
4855 garray_T *gap,
4856 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004857 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004858 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004859 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004860 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004861{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004862 int failed = FALSE;
4863 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004864# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004865 char_u *buf;
4866 int ok;
4867 HANDLE hFind = INVALID_HANDLE_VALUE;
4868 WIN32_FIND_DATAW wfd;
4869 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004870# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004871 DIR *dirp;
4872 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004873# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004874
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004875 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004876
4877# ifdef FEAT_EVAL
4878# define FREE_ITEM(item) do { \
4879 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004880 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004881 else \
4882 vim_free(item); \
4883 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004884
4885 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004886# else
4887# define FREE_ITEM(item) vim_free(item)
4888# endif
4889
4890# ifdef MSWIN
4891 buf = alloc(MAXPATHL);
4892 if (buf == NULL)
4893 return FAIL;
4894 STRNCPY(buf, path, MAXPATHL-5);
4895 p = buf + STRLEN(buf);
4896 MB_PTR_BACK(buf, p);
4897 if (*p == '\\' || *p == '/')
4898 *p = NUL;
4899 STRCAT(p, "\\*");
4900
4901 wn = enc_to_utf16(buf, NULL);
4902 if (wn != NULL)
4903 hFind = FindFirstFileW(wn, &wfd);
4904 ok = (hFind != INVALID_HANDLE_VALUE);
4905 if (!ok)
4906 {
4907 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004908 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004909 }
4910 else
4911 {
4912 while (ok)
4913 {
4914 int ignore;
4915 void *item;
4916 WCHAR *wp;
4917
4918 wp = wfd.cFileName;
4919 ignore = wp[0] == L'.' &&
4920 (wp[1] == NUL ||
4921 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004922 if (ignore)
4923 {
4924 ok = FindNextFileW(hFind, &wfd);
4925 continue;
4926 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004927# ifdef FEAT_EVAL
4928 if (withattr)
4929 item = (void*)create_readdirex_item(&wfd);
4930 else
4931# endif
4932 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4933 if (item == NULL)
4934 {
4935 failed = TRUE;
4936 break;
4937 }
4938
4939 if (!ignore && checkitem != NULL)
4940 {
4941 int r = checkitem(context, item);
4942
4943 if (r < 0)
4944 {
4945 FREE_ITEM(item);
4946 break;
4947 }
4948 if (r == 0)
4949 ignore = TRUE;
4950 }
4951
4952 if (!ignore)
4953 {
4954 if (ga_grow(gap, 1) == OK)
4955 ((void**)gap->ga_data)[gap->ga_len++] = item;
4956 else
4957 {
4958 failed = TRUE;
4959 FREE_ITEM(item);
4960 break;
4961 }
4962 }
4963 else
4964 FREE_ITEM(item);
4965
4966 ok = FindNextFileW(hFind, &wfd);
4967 }
4968 FindClose(hFind);
4969 }
4970
4971 vim_free(buf);
4972 vim_free(wn);
4973# else // MSWIN
4974 dirp = opendir((char *)path);
4975 if (dirp == NULL)
4976 {
4977 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004978 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004979 }
4980 else
4981 {
4982 for (;;)
4983 {
4984 int ignore;
4985 void *item;
4986
4987 dp = readdir(dirp);
4988 if (dp == NULL)
4989 break;
4990 p = (char_u *)dp->d_name;
4991
4992 ignore = p[0] == '.' &&
4993 (p[1] == NUL ||
4994 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004995 if (ignore)
4996 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004997# ifdef FEAT_EVAL
4998 if (withattr)
4999 item = (void*)create_readdirex_item(path, p);
5000 else
5001# endif
5002 item = (void*)vim_strsave(p);
5003 if (item == NULL)
5004 {
5005 failed = TRUE;
5006 break;
5007 }
5008
Bram Moolenaarfe154992022-03-22 20:42:12 +00005009 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005010 {
5011 int r = checkitem(context, item);
5012
5013 if (r < 0)
5014 {
5015 FREE_ITEM(item);
5016 break;
5017 }
5018 if (r == 0)
5019 ignore = TRUE;
5020 }
5021
5022 if (!ignore)
5023 {
5024 if (ga_grow(gap, 1) == OK)
5025 ((void**)gap->ga_data)[gap->ga_len++] = item;
5026 else
5027 {
5028 failed = TRUE;
5029 FREE_ITEM(item);
5030 break;
5031 }
5032 }
5033 else
5034 FREE_ITEM(item);
5035 }
5036
5037 closedir(dirp);
5038 }
5039# endif // MSWIN
5040
5041# undef FREE_ITEM
5042
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005043 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005044 {
5045# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005046 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005047 if (withattr)
5048 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5049 compare_readdirex_item);
5050 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005051 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5052 compare_readdir_item);
5053# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005054 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005055# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005056 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005057
5058 return failed ? FAIL : OK;
5059}
5060
5061/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005062 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005063 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005064 */
5065 int
5066delete_recursive(char_u *name)
5067{
5068 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005069 int i;
5070 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005071 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005072
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005073 // A symbolic link to a directory itself is deleted, not the directory it
5074 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005075 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005076# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005077 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005078# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005079 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005080# endif
5081 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005082 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005083 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005084 if (exp == NULL)
5085 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005086 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005087 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005088 for (i = 0; i < ga.ga_len; ++i)
5089 {
5090 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5091 ((char_u **)ga.ga_data)[i]);
5092 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005093 // Remember the failure but continue deleting any further
5094 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005095 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005096 }
5097 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005098 if (mch_rmdir(exp) != 0)
5099 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005100 }
5101 else
5102 result = -1;
5103 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005104 }
5105 else
5106 result = mch_remove(name) == 0 ? 0 : -1;
5107
5108 return result;
5109}
5110#endif
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005115# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5116/*
5117 * Open temporary directory and take file lock to prevent
5118 * to be auto-cleaned.
5119 */
5120 static void
5121vim_opentempdir(void)
5122{
5123 DIR *dp = NULL;
5124
5125 if (vim_tempdir_dp != NULL)
5126 return;
5127
5128 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005129 if (dp == NULL)
5130 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005131
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005132 vim_tempdir_dp = dp;
5133 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005134}
5135
5136/*
5137 * Close temporary directory - it automatically release file lock.
5138 */
5139 static void
5140vim_closetempdir(void)
5141{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005142 if (vim_tempdir_dp == NULL)
5143 return;
5144
5145 closedir(vim_tempdir_dp);
5146 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005147}
5148# endif
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150/*
5151 * Delete the temp directory and all files it contains.
5152 */
5153 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005154vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005156 if (vim_tempdir == NULL)
5157 return;
5158
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005159# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005160 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005161# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005162 // remove the trailing path separator
5163 gettail(vim_tempdir)[-1] = NUL;
5164 delete_recursive(vim_tempdir);
5165 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
5168/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005169 * Directory "tempdir" was created. Expand this name to a full path and put
5170 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5171 * "tempdir" must be no longer than MAXPATHL.
5172 */
5173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005174vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005175{
5176 char_u *buf;
5177
Bram Moolenaar964b3742019-05-24 18:54:09 +02005178 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005179 if (buf == NULL)
5180 return;
5181
5182 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5183 STRCPY(buf, tempdir);
5184 add_pathsep(buf);
5185 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005186# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005187 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005188# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005189 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005190}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005191#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005192
5193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 * vim_tempname(): Return a unique name that can be used for a temp file.
5195 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005196 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5197 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 *
5199 * The returned pointer is to allocated memory.
5200 * The returned pointer is NULL if no valid name was found.
5201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005203vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005204 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005208 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005209#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005210 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#else
5212 char_u itmp[TEMPNAMELEN];
5213#endif
5214
5215#ifdef TEMPDIRNAMES
5216 static char *(tempdirs[]) = {TEMPDIRNAMES};
5217 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005219 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220# endif
5221
5222 /*
5223 * This will create a directory for private use by this instance of Vim.
5224 * This is done once, and the same directory is used for all temp files.
5225 * This method avoids security problems because of symlink attacks et al.
5226 * It's also a bit faster, because we only need to check for an existing
5227 * file when creating the directory and not for each temp file.
5228 */
5229 if (vim_tempdir == NULL)
5230 {
5231 /*
5232 * Try the entries in TEMPDIRNAMES to create the temp directory.
5233 */
K.Takataeeec2542021-06-02 13:28:16 +02005234 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005236# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005237 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005238 long nr;
5239 long off;
5240# endif
5241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005242 // Expand $TMP, leave room for "/v1100000/999999999".
5243 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005245 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005248 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
Bram Moolenaareaf03392009-11-17 11:08:52 +00005250# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005251 {
5252# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // Make sure the umask doesn't remove the executable bit.
5254 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005255 mode_t umask_save = umask(077);
5256# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005258 STRCAT(itmp, "vXXXXXX");
5259 if (mkdtemp((char *)itmp) != NULL)
5260 vim_settempdir(itmp);
5261# if defined(UNIX) || defined(VMS)
5262 (void)umask(umask_save);
5263# endif
5264 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005265# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005266 // Get an arbitrary number of up to 6 digits. When it's
5267 // unlikely that it already exists it will be faster,
5268 // otherwise it doesn't matter. The use of mkdir() avoids any
5269 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005271 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005273 // Try up to 10000 different values until we find a name that
5274 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 for (off = 0; off < 10000L; ++off)
5276 {
5277 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005278# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaareaf03392009-11-17 11:08:52 +00005282 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5283# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005284 // If mkdir() does not set errno to EEXIST, check for
5285 // existing file here. There is a race condition then,
5286 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (mch_stat((char *)itmp, &st) >= 0)
5288 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005289# endif
5290# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005291 // Make sure the umask doesn't remove the executable bit.
5292 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005296# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (r == 0)
5300 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005301 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 break;
5303 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005304# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005305 // If the mkdir() didn't fail because the file/dir exists,
5306 // we probably can't create any dir here, try another
5307 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 break;
5311 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (vim_tempdir != NULL)
5314 break;
5315 }
5316 }
5317 }
5318
5319 if (vim_tempdir != NULL)
5320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321 // There is no need to check if the file exists, because we own the
5322 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5324 return vim_strsave(itmp);
5325 }
5326
5327 return NULL;
5328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005329#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar4f974752019-02-17 17:44:42 +01005331# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005332 WCHAR wszTempFile[_MAX_PATH + 1];
5333 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005334 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 char_u *retval;
5336 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005337 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005338 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005340 wcscpy(itmp, L"");
5341 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005342 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005343 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005344 wszTempFile[1] = L'\\';
5345 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005346 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005347 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005348
5349 // randomize the name to avoid collisions
5350 i = mch_get_pid() + extra_char;
5351 buf4[1] = chartab[i % 36];
5352 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005353 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005355 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005356 // GetTempFileName() will create the file, we don't want that
5357 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005359 // Backslashes in a temp file name cause problems when filtering with
5360 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005361 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005362 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005363 shname = gettail(p_sh);
5364 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5365 || strstr((char *)shname, "pwsh") != NULL ))
5366 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 for (p = retval; *p; ++p)
5368 if (*p == '\\')
5369 *p = '/';
5370 return retval;
5371
Bram Moolenaar4f974752019-02-17 17:44:42 +01005372# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005375 char_u *p;
5376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005378 p = tmpnam((char *)itmp);
5379 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 return NULL;
5381# else
5382 char_u *p;
5383
5384# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 // mktemp() is not working on VMS. It seems to be
5386 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 sprintf((char *)itmp, "VIM%c", extra_char);
5388 p = (char_u *)tempnam("tmp:", (char *)itmp);
5389 if (p != NULL)
5390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 // VMS will use '.LIS' if we don't explicitly specify an extension,
5392 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 STRCPY(itmp, p);
5394 STRCAT(itmp, ".txt");
5395 free(p);
5396 }
5397 else
5398 return NULL;
5399# else
5400 STRCPY(itmp, TEMPNAME);
5401 if ((p = vim_strchr(itmp, '?')) != NULL)
5402 *p = extra_char;
5403 if (mktemp((char *)itmp) == NULL)
5404 return NULL;
5405# endif
5406# endif
5407
5408 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005409# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411}
5412
5413#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5414/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005415 * Convert all backslashes in fname to forward slashes in-place, unless when
5416 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 */
5418 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005419forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 char_u *p;
5422
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005423 if (path_with_url(fname))
5424 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005426 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005427 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005429 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 *p = '/';
5431}
5432#endif
5433
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005434/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005435 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5436 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5437 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 * Used for autocommands and 'wildignore'.
5439 * Returns TRUE if there is a match, FALSE otherwise.
5440 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005441 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005443 char_u *pattern, // pattern to match with
5444 regprog_T **prog, // pre-compiled regprog or NULL
5445 char_u *fname, // full path of file name
5446 char_u *sfname, // short file name or NULL
5447 char_u *tail, // tail of path
5448 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 regmatch_T regmatch;
5451 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005454 if (prog != NULL)
5455 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005457 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 /*
5460 * Try for a match with the pattern with:
5461 * 1. the full file name, when the pattern has a '/'.
5462 * 2. the short file name, when the pattern has a '/'.
5463 * 3. the tail of the file name, when the pattern has no '/'.
5464 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005465 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 && ((allow_dirs
5467 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5468 || (sfname != NULL
5469 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005470 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 result = TRUE;
5472
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005473 if (prog != NULL)
5474 *prog = regmatch.regprog;
5475 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005476 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return result;
5478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480/*
5481 * Return TRUE if a file matches with a pattern in "list".
5482 * "list" is a comma-separated list of patterns, like 'wildignore'.
5483 * "sfname" is the short file name or NULL, "ffname" the long file name.
5484 */
5485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005486match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005488 char_u buf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 char_u *tail;
5490 char_u *regpat;
5491 char allow_dirs;
5492 int match;
5493 char_u *p;
5494
5495 tail = gettail(sfname);
5496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 p = list;
5499 while (*p)
5500 {
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005501 copy_option_part(&p, buf, MAXPATHL, ",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5503 if (regpat == NULL)
5504 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005505 match = match_file_pat(regpat, NULL, ffname, sfname,
5506 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 vim_free(regpat);
5508 if (match)
5509 return TRUE;
5510 }
5511 return FALSE;
5512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514/*
5515 * Convert the given pattern "pat" which has shell style wildcards in it, into
5516 * a regular expression, and return the result in allocated memory. If there
5517 * is a directory path separator to be matched, then TRUE is put in
5518 * allow_dirs, otherwise FALSE is put there -- webb.
5519 * Handle backslashes before special characters, like "\*" and "\ ".
5520 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * Returns NULL when out of memory.
5522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524file_pat_to_reg_pat(
5525 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 char_u *pat_end, // first char after pattern or NULL
5527 char *allow_dirs, // Result passed back out in here
5528 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 char_u *endp;
5532 char_u *reg_pat;
5533 char_u *p;
5534 int i;
5535 int nested = 0;
5536 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 if (allow_dirs != NULL)
5539 *allow_dirs = FALSE;
5540 if (pat_end == NULL)
5541 pat_end = pat + STRLEN(pat);
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 for (p = pat; p < pat_end; p++)
5544 {
5545 switch (*p)
5546 {
5547 case '*':
5548 case '.':
5549 case ',':
5550 case '{':
5551 case '}':
5552 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005553 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 break;
5555#ifdef BACKSLASH_IN_FILENAME
5556 case '\\':
5557 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005558 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 break;
5560#endif
5561 default:
5562 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005563 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 ++p;
5566 ++size;
5567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 break;
5569 }
5570 }
5571 reg_pat = alloc(size + 1);
5572 if (reg_pat == NULL)
5573 return NULL;
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 if (pat[0] == '*')
5578 while (pat[0] == '*' && pat < pat_end - 1)
5579 pat++;
5580 else
5581 reg_pat[i++] = '^';
5582 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005583 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 while (endp - pat > 0 && *endp == '*')
5586 endp--;
5587 add_dollar = FALSE;
5588 }
5589 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5590 {
5591 switch (*p)
5592 {
5593 case '*':
5594 reg_pat[i++] = '.';
5595 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005596 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005597 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 break;
5599 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 case '~':
5601 reg_pat[i++] = '\\';
5602 reg_pat[i++] = *p;
5603 break;
5604 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 reg_pat[i++] = '.';
5606 break;
5607 case '\\':
5608 if (p[1] == NUL)
5609 break;
5610#ifdef BACKSLASH_IN_FILENAME
5611 if (!no_bslash)
5612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 // translate:
5614 // "\x" to "\\x" e.g., "dir\file"
5615 // "\*" to "\\.*" e.g., "dir\*.c"
5616 // "\?" to "\\." e.g., "dir\??.c"
5617 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5619 && p[1] != '+')
5620 {
5621 reg_pat[i++] = '[';
5622 reg_pat[i++] = '\\';
5623 reg_pat[i++] = '/';
5624 reg_pat[i++] = ']';
5625 if (allow_dirs != NULL)
5626 *allow_dirs = TRUE;
5627 break;
5628 }
5629 }
5630#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 // Undo escaping from ExpandEscape():
5632 // foo\?bar -> foo?bar
5633 // foo\%bar -> foo%bar
5634 // foo\,bar -> foo,bar
5635 // foo\ bar -> foo bar
5636 // Don't unescape \, * and others that are also special in a
5637 // regexp.
5638 // An escaped { must be unescaped since we use magic not
5639 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (*++p == '?'
5641#ifdef BACKSLASH_IN_FILENAME
5642 && no_bslash
5643#endif
5644 )
5645 reg_pat[i++] = '?';
5646 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005647 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005648 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005649 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005650 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5651 {
5652 reg_pat[i++] = '\\';
5653 reg_pat[i++] = '{';
5654 p += 2;
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
5657 {
5658 if (allow_dirs != NULL && vim_ispathsep(*p)
5659#ifdef BACKSLASH_IN_FILENAME
5660 && (!no_bslash || *p != '\\')
5661#endif
5662 )
5663 *allow_dirs = TRUE;
5664 reg_pat[i++] = '\\';
5665 reg_pat[i++] = *p;
5666 }
5667 break;
5668#ifdef BACKSLASH_IN_FILENAME
5669 case '/':
5670 reg_pat[i++] = '[';
5671 reg_pat[i++] = '\\';
5672 reg_pat[i++] = '/';
5673 reg_pat[i++] = ']';
5674 if (allow_dirs != NULL)
5675 *allow_dirs = TRUE;
5676 break;
5677#endif
5678 case '{':
5679 reg_pat[i++] = '\\';
5680 reg_pat[i++] = '(';
5681 nested++;
5682 break;
5683 case '}':
5684 reg_pat[i++] = '\\';
5685 reg_pat[i++] = ')';
5686 --nested;
5687 break;
5688 case ',':
5689 if (nested)
5690 {
5691 reg_pat[i++] = '\\';
5692 reg_pat[i++] = '|';
5693 }
5694 else
5695 reg_pat[i++] = ',';
5696 break;
5697 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005698 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005700 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 *allow_dirs = TRUE;
5702 reg_pat[i++] = *p;
5703 break;
5704 }
5705 }
5706 if (add_dollar)
5707 reg_pat[i++] = '$';
5708 reg_pat[i] = NUL;
5709 if (nested != 0)
5710 {
5711 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005712 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005714 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005715 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 return reg_pat;
5718}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005719
5720#if defined(EINTR) || defined(PROTO)
5721/*
5722 * Version of read() that retries when interrupted by EINTR (possibly
5723 * by a SIGWINCH).
5724 */
5725 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005727{
5728 long ret;
5729
5730 for (;;)
5731 {
5732 ret = vim_read(fd, buf, bufsize);
5733 if (ret >= 0 || errno != EINTR)
5734 break;
5735 }
5736 return ret;
5737}
5738
5739/*
5740 * Version of write() that retries when interrupted by EINTR (possibly
5741 * by a SIGWINCH).
5742 */
5743 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005745{
5746 long ret = 0;
5747 long wlen;
5748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005749 // Repeat the write() so long it didn't fail, other than being interrupted
5750 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005751 while (ret < (long)bufsize)
5752 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005753 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005754 if (wlen < 0)
5755 {
5756 if (errno != EINTR)
5757 break;
5758 }
5759 else
5760 ret += wlen;
5761 }
5762 return ret;
5763}
5764#endif