blob: 1af0b4c59fdce821a6d45396a12f6da9e3cc2bbe [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 Moolenaarbf1b7a72009-03-05 02:15:53 +000044static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000045
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020046#ifdef FEAT_EVAL
47static int readdirex_sort;
48#endif
49
Bram Moolenaar473952e2019-09-28 16:30:04 +020050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010051filemess(
52 buf_T *buf,
53 char_u *name,
54 char_u *s,
55 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020058 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60 if (msg_silent != 0)
61 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010062 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020063
Bram Moolenaar217e1b82019-12-01 21:41:28 +010064 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020065 if (STRLEN(IObuff) > IOSIZE - 100)
66 IObuff[IOSIZE - 100] = NUL;
67
68 // Avoid an over-long translation to cause trouble.
69 STRNCAT(IObuff, s, 99);
70
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 /*
72 * For the first message may have to start a new line.
73 * For further ones overwrite the previous one, reset msg_scroll before
74 * calling filemess().
75 */
76 msg_scroll_save = msg_scroll;
77 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
78 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010079 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 check_for_delay(FALSE);
81 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020082 if (prev_msg_col != 0 && msg_col == 0)
83 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 msg_scroll = msg_scroll_save;
85 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010086 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
88 msg_clr_eos();
89 out_flush();
90 msg_scrolled_ign = FALSE;
91}
92
93/*
94 * Read lines from file "fname" into the buffer after line "from".
95 *
96 * 1. We allocate blocks with lalloc, as big as possible.
97 * 2. Each block is filled with characters from the file with a single read().
98 * 3. The lines are inserted in the buffer with ml_append().
99 *
100 * (caller must check that fname != NULL, unless READ_STDIN is used)
101 *
102 * "lines_to_skip" is the number of lines that must be skipped
103 * "lines_to_read" is the number of lines that are appended
104 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
105 *
106 * flags:
107 * READ_NEW starting to edit a new buffer
108 * READ_FILTER reading filter output
109 * READ_STDIN read from stdin instead of a file
110 * READ_BUFFER read from curbuf instead of a file (converting after reading
111 * stdin)
112 * 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{
128 int fd = 0;
129 int newfile = (flags & READ_NEW);
130 int check_readonly;
131 int filtering = (flags & READ_FILTER);
132 int read_stdin = (flags & READ_STDIN);
133 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200134 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000135 int set_options = newfile || read_buffer
136 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100137 linenr_T read_buf_lnum = 1; // next line to read from curbuf
138 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 char_u c;
140 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100141 char_u *ptr = NULL; // pointer into read buffer
142 char_u *buffer = NULL; // read buffer
143 char_u *new_buffer = NULL; // init to shut up gcc
144 char_u *line_start = NULL; // init to shut up gcc
145 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 colnr_T len;
147 long size = 0;
148 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200149 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 int skip_read = FALSE;
151#ifdef FEAT_CRYPT
152 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200153 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200155#ifdef FEAT_PERSISTENT_UNDO
156 context_sha256_T sha_ctx;
157 int read_undo_file = FALSE;
158#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100159 int split = 0; // number of split lines
160#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100162 int error = FALSE; // errors encountered
163 int ff_error = EOL_UNKNOWN; // file format with errors
164 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#ifdef UNIX
166 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100167 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#else
169 int perm;
170#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100171 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200173 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 int file_readonly;
175 linenr_T skip_count = 0;
176 linenr_T read_count = 0;
177 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100178 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
179 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100180 int try_mac;
181 int try_dos;
182 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100185 linenr_T conv_error = 0; // line nr with conversion error
186 linenr_T illegal_byte = 0; // line nr with illegal byte
187 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
188 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000189 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100190 // BAD_KEEP, BAD_DROP or character to
191 // replace with
192 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 char_u *fenc; // fileencoding to use
195 int fenc_alloced; // fenc_next is in allocated memory
196 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 int advance_fenc = FALSE;
198 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100199#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100200 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100201# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 int did_iconv = FALSE; // TRUE when iconv() failed and trying
203 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100205#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100206 int converted = FALSE; // TRUE if conversion done
207 int notconverted = FALSE; // TRUE if conversion wanted but it
208 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100210 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100211 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200212 buf_T *old_curbuf;
213 char_u *old_b_ffname;
214 char_u *old_b_fname;
215 int using_b_ffname;
216 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200217 static char *msg_is_a_directory = N_("is a directory");
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200218
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100219 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200220
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100221 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223 /*
224 * If there is no file name yet, use the one for the read file.
225 * BF_NOTEDITED is set to reflect this.
226 * Don't do this for a read from a filter.
227 * Only do this when 'cpoptions' contains the 'f' flag.
228 */
229 if (curbuf->b_ffname == NULL
230 && !filtering
231 && fname != NULL
232 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
233 && !(flags & READ_DUMMY))
234 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000235 if (set_rw_fname(fname, sfname) == FAIL)
236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 }
238
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100239 // Remember the initial values of curbuf, curbuf->b_ffname and
240 // curbuf->b_fname to detect whether they are altered as a result of
241 // executing nasty autocommands. Also check if "fname" and "sfname"
242 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200243 old_curbuf = curbuf;
244 old_b_ffname = curbuf->b_ffname;
245 old_b_fname = curbuf->b_fname;
246 using_b_ffname = (fname == curbuf->b_ffname)
247 || (sfname == curbuf->b_ffname);
248 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200249
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100250 // After reading a file the cursor line changes but we don't want to
251 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000252 ex_no_reprint = TRUE;
253
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100254 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000255 need_fileinfo = FALSE;
256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 /*
258 * For Unix: Use the short file name whenever possible.
259 * Avoids problems with networks and when directory names are changed.
260 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
261 * another directory, which we don't detect.
262 */
263 if (sfname == NULL)
264 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200265#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 fname = sfname;
267#endif
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 /*
270 * The BufReadCmd and FileReadCmd events intercept the reading process by
271 * executing the associated commands instead.
272 */
273 if (!filtering && !read_stdin && !read_buffer)
274 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100275 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100277 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
279 curbuf->b_op_start.col = 0;
280
281 if (newfile)
282 {
283 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
284 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200285 {
286 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200288 if (aborting())
289 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200291 // The BufReadCmd code usually uses ":read" to get the text and
292 // perhaps ":file" to change the buffer name. But we should
293 // consider this to work like ":edit", thus reset the
294 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
295 // same file.
296 if (status == OK)
297 curbuf->b_flags &= ~BF_NOTEDITED;
298 return status;
299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 }
301 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
302 FALSE, NULL, eap))
303#ifdef FEAT_EVAL
304 return aborting() ? FAIL : OK;
305#else
306 return OK;
307#endif
308
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100309 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100313 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100315 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000317 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200319 size_t namelen = STRLEN(fname);
320
321 // If the name is too long we might crash further on, quit here.
322 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000323 {
324 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
325 msg_end();
326 msg_scroll = msg_save;
327 return FAIL;
328 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200329
330 // If the name ends in a path separator, we can't open it. Check here,
331 // because reading the file may actually work, but then creating the
332 // swap file may destroy it! Reported on MS-DOS and Win 95.
333 if (after_pathsep(fname, fname + namelen))
334 {
335 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
336 msg_end();
337 msg_scroll = msg_save;
338 return FAIL;
339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
341
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200342 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100344#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200345 /*
346 * On Unix it is possible to read a directory, so we have to
347 * check for it before the mch_open().
348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100350 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
351 && !S_ISFIFO(perm) // ... or fifo
352 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000353# ifdef OPEN_CHR_FILES
354 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100355 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 )
358 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100359 int retval = FAIL;
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100362 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200363 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100364 retval = NOTDONE;
365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 else
367 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
368 msg_end();
369 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100370 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200372#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100373#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000374 /*
375 * MS-Windows allows opening a device, but we will probably get stuck
376 * trying to read it.
377 */
378 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
379 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000380 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000381 msg_end();
382 msg_scroll = msg_save;
383 return FAIL;
384 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000385#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200386 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000387
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200389 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
391 /*
392 * When opening a new file we take the readonly flag from the file.
393 * Default is r/w, can be set to r/o below.
394 * Don't reset it when in readonly mode
395 * Only set/reset b_p_ro when BF_CHECK_RO is set.
396 */
397 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000398 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 curbuf->b_p_ro = FALSE;
400
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200401 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 if (mch_stat((char *)fname, &st) >= 0)
405 {
406 buf_store_time(curbuf, &st, fname);
407 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#ifdef UNIX
409 /*
410 * Use the protection bits of the original file for the swap file.
411 * This makes it possible for others to read the name of the
412 * edited file from the swapfile, but only if they can read the
413 * edited file.
414 * Remove the "write" and "execute" bits for group and others
415 * (they must not write the swapfile).
416 * Add the "read" and "write" bits for the user, otherwise we may
417 * not be able to write to the file ourselves.
418 * Setting the bits is done below, after creating the swap file.
419 */
420 swap_mode = (st.st_mode & 0644) | 0600;
421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#ifdef VMS
423 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000424 curbuf->b_fab_rat = st.st_fab_rat;
425 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
427 }
428 else
429 {
430 curbuf->b_mtime = 0;
431 curbuf->b_mtime_read = 0;
432 curbuf->b_orig_size = 0;
433 curbuf->b_orig_mode = 0;
434 }
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Reset the "new file" flag. It will be set again below when the
437 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
439 }
440
441/*
442 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100443 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 */
445 file_readonly = FALSE;
446 if (read_stdin)
447 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100448#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100449 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 setmode(0, O_BINARY);
451#endif
452 }
453 else if (!read_buffer)
454 {
455#ifdef USE_MCH_ACCESS
456 if (
457# ifdef UNIX
458 !(perm & 0222) ||
459# endif
460 mch_access((char *)fname, W_OK))
461 file_readonly = TRUE;
462 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
463#else
464 if (!newfile
465 || readonlymode
466 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
467 {
468 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100469 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
471 }
472#endif
473 }
474
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
477#ifndef UNIX
478 int isdir_f;
479#endif
480 msg_scroll = msg_save;
481#ifndef UNIX
482 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100483 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 */
485 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100486 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 if (isdir_f)
488 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200489 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 }
492 else
493#endif
494 if (newfile)
495 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200496 if (perm < 0
497#ifdef ENOENT
498 && errno == ENOENT
499#endif
500 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {
502 /*
503 * Set the 'new-file' flag, so that when the file has
504 * been created by someone else, a ":w" will complain.
505 */
506 curbuf->b_flags |= BF_NEW;
507
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100508 // Create a swap file now, so that other Vims are warned
509 // that we are editing this file. Don't do this for a
510 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#ifdef FEAT_QUICKFIX
512 if (!bt_dontwrite(curbuf))
513#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100516 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000517 if (curbuf != old_curbuf
518 || (using_b_ffname
519 && (old_b_ffname != curbuf->b_ffname))
520 || (using_b_fname
521 && (old_b_fname != curbuf->b_fname)))
522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100523 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000524 return FAIL;
525 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000526 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000527 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200528 filemess(curbuf, sfname,
529 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000530 else
531 filemess(curbuf, sfname,
532 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // Even though this is a new file, it might have been
535 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 check_marks_read();
537#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200539 if (eap != NULL)
540 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
542 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 save_file_ff(curbuf);
545
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100546#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 return FAIL;
549#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100550 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552 else
553 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000554 filemess(curbuf, sfname, (char_u *)(
555# ifdef EFBIG
556 (errno == EFBIG) ? _("[File too big]") :
557# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200558# ifdef EOVERFLOW
559 (errno == EOVERFLOW) ? _("[File too big]") :
560# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000561 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100562 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564 }
565
566 return FAIL;
567 }
568
569 /*
570 * Only set the 'ro' flag for readonly files the first time they are
571 * loaded. Help files always get readonly mode
572 */
573 if ((check_readonly && file_readonly) || curbuf->b_help)
574 curbuf->b_p_ro = TRUE;
575
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100578 // Don't change 'eol' if reading from buffer as it will already be
579 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000580 if (!read_buffer)
581 {
582 curbuf->b_p_eol = TRUE;
583 curbuf->b_start_eol = TRUE;
584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000586 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100589 // Create a swap file now, so that other Vims are warned that we are
590 // editing this file.
591 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592#ifdef FEAT_QUICKFIX
593 if (!bt_dontwrite(curbuf))
594#endif
595 {
596 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000597 if (!read_stdin && (curbuf != old_curbuf
598 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
599 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100601 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000602 if (!read_buffer)
603 close(fd);
604 return FAIL;
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100607 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000608 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
609 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100610 {
611 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
612
613 /*
614 * If the group-read bit is set but not the world-read bit, then
615 * the group must be equal to the group of the original file. If
616 * we can't make that happen then reset the group-read bit. This
617 * avoids making the swap file readable to more users when the
618 * primary group of the user is too permissive.
619 */
620 if ((swap_mode & 044) == 040)
621 {
622 stat_T swap_st;
623
624 if (mch_stat((char *)swap_fname, &swap_st) >= 0
625 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200626# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100627 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200628 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200629# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200630 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100631 swap_mode &= 0600;
632 }
633
634 (void)mch_setperm(swap_fname, (long)swap_mode);
635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#endif
637 }
638
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200639 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (swap_exists_action == SEA_QUIT)
641 {
642 if (!read_buffer && !read_stdin)
643 close(fd);
644 return FAIL;
645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 /*
650 * Set '[ mark to the line above where the lines go (line 1 if zero).
651 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100652 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
654 curbuf->b_op_start.col = 0;
655
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100656 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
657 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
658 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (!read_buffer)
661 {
662 int m = msg_scroll;
663 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665 /*
666 * The file must be closed again, the autocommands may want to change
667 * the file before reading it.
668 */
669 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672 /*
673 * The output from the autocommands should not overwrite anything and
674 * should not be overwritten: Set msg_scroll, restore its value if no
675 * output was done.
676 */
677 msg_scroll = TRUE;
678 if (filtering)
679 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
680 FALSE, curbuf, eap);
681 else if (read_stdin)
682 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
683 FALSE, curbuf, eap);
684 else if (newfile)
685 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
686 FALSE, curbuf, eap);
687 else
688 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
689 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100691 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
692 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
693 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100694 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (msg_scrolled == n)
697 msg_scroll = m;
698
699#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
702 --no_wait_return;
703 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 return FAIL;
706 }
707#endif
708 /*
709 * Don't allow the autocommands to change the current buffer.
710 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000711 *
712 * Don't allow the autocommands to change the buffer name either
713 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 */
715 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000716 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
717 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
719 {
720 --no_wait_return;
721 msg_scroll = msg_save;
722 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100725 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100726 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 return FAIL;
728 }
729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100731 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
733
734 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
735 {
736 /*
737 * Show the user that we are busy reading the input. Sometimes this
738 * may take a while. When reading from stdin another program may
739 * still be running, don't move the cursor to the last line, unless
740 * always using the GUI.
741 */
742 if (read_stdin)
743 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100744 if (!is_not_a_term())
745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200747# ifdef VIMDLL
748 if (!gui.in_use)
749# endif
750 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#endif
752#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100753 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100754 if (gui.in_use && !gui.dying && !gui.starting)
755 {
756 p = (char_u *)_("Reading from stdin...");
757 gui_write(p, (int)STRLEN(p));
758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 else if (!read_buffer)
763 filemess(curbuf, sfname, (char_u *)"", 0);
764 }
765
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100766 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 /*
769 * Set linecnt now, before the "retry" caused by a wrong guess for
770 * fileformat, and after the autocommands, which may change them.
771 */
772 linecnt = curbuf->b_ml.ml_line_count;
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000775 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000776 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000777 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000778 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000779 curbuf->b_bad_char = eap->bad_char;
780 }
781 else
782 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000785 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 */
787 if (eap != NULL && eap->force_enc != 0)
788 {
789 fenc = enc_canonize(eap->cmd + eap->force_enc);
790 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000791 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else if (curbuf->b_p_bin)
794 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100795 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 fenc_alloced = FALSE;
797 }
798 else if (curbuf->b_help)
799 {
800 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000801 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // Help files are either utf-8 or latin1. Try utf-8 first, if this
804 // fails it must be latin1.
805 // Always do this when 'encoding' is "utf-8". Otherwise only do
806 // this when needed to avoid [converted] remarks all the time.
807 // It is needed when the first line contains non-ASCII characters.
808 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 fenc = (char_u *)"latin1";
810 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000811 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000813 fc = fname[STRLEN(fname) - 1];
814 if (TOLOWER_ASC(fc) == 'x')
815 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // Read the first line (and a bit more). Immediately rewind to
817 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100818 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200819 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000820 for (p = firstline; p < firstline + len; ++p)
821 if (*p >= 0x80)
822 {
823 c = TRUE;
824 break;
825 }
826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828
829 if (c)
830 {
831 fenc_next = fenc;
832 fenc = (char_u *)"utf-8";
833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // When the file is utf-8 but a character doesn't fit in
835 // 'encoding' don't retry. In help text editing utf-8 bytes
836 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000837 if (!enc_utf8)
838 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 fenc_alloced = FALSE;
841 }
842 else if (*p_fencs == NUL)
843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 fenc_alloced = FALSE;
846 }
847 else
848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200850 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 /*
854 * Jump back here to retry reading the file in different ways.
855 * Reasons to retry:
856 * - encoding conversion failed: try another one from "fenc_next"
857 * - BOM detected and fenc was set, need to setup conversion
858 * - "fileformat" check failed: try another
859 *
860 * Variables set for special retry actions:
861 * "file_rewind" Rewind the file to start reading it again.
862 * "advance_fenc" Advance "fenc" using "fenc_next".
863 * "skip_read" Re-use already read bytes (BOM detected).
864 * "did_iconv" iconv() conversion failed, try 'charconvert'.
865 * "keep_fileformat" Don't reset "fileformat".
866 *
867 * Other status indicators:
868 * "tmpname" When != NULL did conversion with 'charconvert'.
869 * Output file has to be deleted afterwards.
870 * "iconv_fd" When != -1 did conversion with iconv().
871 */
872retry:
873
874 if (file_rewind)
875 {
876 if (read_buffer)
877 {
878 read_buf_lnum = 1;
879 read_buf_col = 0;
880 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200881 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 error = TRUE;
885 goto failed;
886 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200889 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000891 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000894 curbuf->b_start_bomb = FALSE;
895 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000896 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
899 /*
900 * When retrying with another "fenc" and the first time "fileformat"
901 * will be reset.
902 */
903 if (keep_fileformat)
904 keep_fileformat = FALSE;
905 else
906 {
907 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000910 try_unix = try_dos = try_mac = FALSE;
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100913 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100917 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
Bram Moolenaar13505972019-01-24 15:04:48 +0100920#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (iconv_fd != (iconv_t)-1)
922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100923 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 iconv_close(iconv_fd);
925 iconv_fd = (iconv_t)-1;
926 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 if (advance_fenc)
930 {
931 /*
932 * Try the next entry in 'fileencodings'.
933 */
934 advance_fenc = FALSE;
935
936 if (eap != NULL && eap->force_enc != 0)
937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100938 // Conversion given with "++cc=" wasn't possible, read
939 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000941 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (fenc_alloced)
943 vim_free(fenc);
944 fenc = (char_u *)"";
945 fenc_alloced = FALSE;
946 }
947 else
948 {
949 if (fenc_alloced)
950 vim_free(fenc);
951 if (fenc_next != NULL)
952 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200953 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955 else
956 {
957 fenc = (char_u *)"";
958 fenc_alloced = FALSE;
959 }
960 }
961 if (tmpname != NULL)
962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100964 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 }
967
968 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000969 * Conversion may be required when the encoding of the file is different
970 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 */
972 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000973 converted = need_conversion(fenc);
974 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
976
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 // "ucs-bom" means we need to check the first bytes of the file
978 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (STRCMP(fenc, ENC_UCSBOM) == 0)
980 fio_flags = FIO_UCSBOM;
981
982 /*
983 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
984 * done. This is handled below after read(). Prepare the
985 * fio_flags to avoid having to parse the string each time.
986 * Also check for Unicode to Latin1 conversion, because iconv()
987 * appears not to handle this correctly. This works just like
988 * conversion to UTF-8 except how the resulting character is put in
989 * the buffer.
990 */
991 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
992 fio_flags = get_fio_flags(fenc);
993
Bram Moolenaar4f974752019-02-17 17:44:42 +0100994#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /*
996 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
997 * is handled with MultiByteToWideChar().
998 */
999 if (fio_flags == 0)
1000 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
Bram Moolenaar13505972019-01-24 15:04:48 +01001003#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001004 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (fio_flags == 0)
1006 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaar13505972019-01-24 15:04:48 +01001009#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * Try using iconv() if we can't convert internally.
1012 */
1013 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001014# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 )
1018 iconv_fd = (iconv_t)my_iconv_open(
1019 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaar13505972019-01-24 15:04:48 +01001022#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /*
1024 * Use the 'charconvert' expression when conversion is required
1025 * and we can't do it internally or with iconv().
1026 */
1027 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001028 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001029# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 )
1033 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001034# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001036# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001037 // Skip conversion when it's already done (retry for wrong
1038 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (tmpname == NULL)
1040 {
1041 tmpname = readfile_charconvert(fname, fenc, &fd);
1042 if (tmpname == NULL)
1043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001044 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 advance_fenc = TRUE;
1046 if (fd < 0)
1047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001048 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001049 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 error = TRUE;
1051 goto failed;
1052 }
1053 goto retry;
1054 }
1055 }
1056 }
1057 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 )
1065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // Conversion wanted but we can't.
1067 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 advance_fenc = TRUE;
1069 goto retry;
1070 }
1071 }
1072 }
1073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // Set "can_retry" when it's possible to rewind the file and try with
1075 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1076 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001077 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 if (!skip_read)
1080 {
1081 linerest = 0;
1082 filesize = 0;
1083 skip_count = lines_to_skip;
1084 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001086#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001087 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1088 && curbuf->b_ffname != NULL
1089 && curbuf->b_p_udf
1090 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001091 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001092 && !read_stdin
1093 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001094 if (read_undo_file)
1095 sha256_start(&sha_ctx);
1096#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001097#ifdef FEAT_CRYPT
1098 if (curbuf->b_cryptstate != NULL)
1099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001100 // Need to free the state, but keep the key, don't want to ask for
1101 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001102 crypt_free_state(curbuf->b_cryptstate);
1103 curbuf->b_cryptstate = NULL;
1104 }
1105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107
1108 while (!error && !got_int)
1109 {
1110 /*
1111 * We allocate as much space for the file as we can get, plus
1112 * space for the old line plus room for one terminating NUL.
1113 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001114 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001116 if (!skip_read)
1117 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001118#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001120#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001121 // Use buffer >= 64K. Add linerest to double the size if the
1122 // line gets very long, to avoid a lot of copying. But don't
1123 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001124 size = 0x10000L + linerest;
1125 if (size > 0x100000L)
1126 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001127#endif
1128 }
1129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001130 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001131 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
1133 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001134 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 size = 1;
1136 }
1137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 if (!skip_read)
1140 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001141 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001143 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 FALSE)) != NULL)
1145 break;
1146 }
1147 if (new_buffer == NULL)
1148 {
1149 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1150 error = TRUE;
1151 break;
1152 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1155 vim_free(buffer);
1156 buffer = new_buffer;
1157 ptr = buffer + linerest;
1158 line_start = buffer;
1159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 // May need room to translate into.
1161 // For iconv() we don't really know the required space, use a
1162 // factor ICONV_MULT.
1163 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1164 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1165 // become up to 4 bytes, size must be multiple of 2
1166 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1167 // multiple of 2
1168 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1169 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001170 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001171#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (iconv_fd != (iconv_t)-1)
1173 size = size / ICONV_MULT;
1174 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (fio_flags & FIO_LATIN1)
1177 size = size / 2;
1178 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1179 size = (size * 2 / 3) & ~1;
1180 else if (fio_flags & FIO_UCS4)
1181 size = (size * 2 / 3) & ~3;
1182 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001183 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001184#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001186 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001187#endif
1188#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001190 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (conv_restlen > 0)
1194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 mch_memmove(ptr, conv_rest, conv_restlen);
1197 ptr += conv_restlen;
1198 size -= conv_restlen;
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 if (read_buffer)
1202 {
1203 /*
1204 * Read bytes from curbuf. Used for converting text read
1205 * from stdin.
1206 */
1207 if (read_buf_lnum > from)
1208 size = 0;
1209 else
1210 {
1211 int n, ni;
1212 long tlen;
1213
1214 tlen = 0;
1215 for (;;)
1216 {
1217 p = ml_get(read_buf_lnum) + read_buf_col;
1218 n = (int)STRLEN(p);
1219 if ((int)tlen + n + 1 > size)
1220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 // Filled up to "size", append partial line.
1222 // Change NL to NUL to reverse the effect done
1223 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001224 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 for (ni = 0; ni < n; ++ni)
1226 {
1227 if (p[ni] == NL)
1228 ptr[tlen++] = NUL;
1229 else
1230 ptr[tlen++] = p[ni];
1231 }
1232 read_buf_col += n;
1233 break;
1234 }
1235 else
1236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001237 // Append whole line and new-line. Change NL
1238 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 for (ni = 0; ni < n; ++ni)
1240 {
1241 if (p[ni] == NL)
1242 ptr[tlen++] = NUL;
1243 else
1244 ptr[tlen++] = p[ni];
1245 }
1246 ptr[tlen++] = NL;
1247 read_buf_col = 0;
1248 if (++read_buf_lnum > from)
1249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001250 // When the last line didn't have an
1251 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (!curbuf->b_p_eol)
1253 --tlen;
1254 size = tlen;
1255 break;
1256 }
1257 }
1258 }
1259 }
1260 }
1261 else
1262 {
1263 /*
1264 * Read bytes from the file.
1265 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001266 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001269#ifdef FEAT_CRYPT
1270 /*
1271 * At start of file: Check for magic number of encryption.
1272 */
1273 if (filesize == 0 && size > 0)
1274 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1275 &filesize, newfile, sfname,
1276 &did_ask_for_key);
1277 /*
1278 * Decrypt the read bytes. This is done before checking for
1279 * EOF because the crypt layer may be buffering.
1280 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001281 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1282 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001283 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001284# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001285 if (crypt_works_inplace(curbuf->b_cryptstate))
1286 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001287# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001288 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001289# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001290 }
1291 else
1292 {
1293 char_u *newptr = NULL;
1294 int decrypted_size;
1295
1296 decrypted_size = crypt_decode_alloc(
1297 curbuf->b_cryptstate, ptr, size, &newptr);
1298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001299 // If the crypt layer is buffering, not producing
1300 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001301 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001302 continue;
1303
1304 if (linerest == 0)
1305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001306 // Simple case: reuse returned buffer (may be
1307 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 new_buffer = newptr;
1309 }
1310 else
1311 {
1312 long_u new_size;
1313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001314 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001315 new_size = (long_u)(decrypted_size + linerest + 1);
1316 new_buffer = lalloc(new_size, FALSE);
1317 if (new_buffer == NULL)
1318 {
1319 do_outofmem_msg(new_size);
1320 error = TRUE;
1321 break;
1322 }
1323
1324 mch_memmove(new_buffer, buffer, linerest);
1325 if (newptr != NULL)
1326 mch_memmove(new_buffer + linerest, newptr,
1327 decrypted_size);
1328 }
1329
1330 if (new_buffer != NULL)
1331 {
1332 vim_free(buffer);
1333 buffer = new_buffer;
1334 new_buffer = NULL;
1335 line_start = buffer;
1336 ptr = buffer + linerest;
1337 }
1338 size = decrypted_size;
1339 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001340# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001341 }
1342#endif
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (size <= 0)
1345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001346 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001349 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001350 /*
1351 * Reached end-of-file but some trailing bytes could
1352 * not be converted. Truncated file?
1353 */
1354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001356 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001357#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001358 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001359#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001360 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001361 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001362 if (can_retry)
1363 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001364 if (conv_error == 0)
1365 conv_error = curbuf->b_ml.ml_line_count
1366 - linecnt + 1;
1367 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001369 else if (illegal_byte == 0)
1370 illegal_byte = curbuf->b_ml.ml_line_count
1371 - linecnt + 1;
1372 if (bad_char_behavior == BAD_DROP)
1373 {
1374 *(ptr - conv_restlen) = NUL;
1375 conv_restlen = 0;
1376 }
1377 else
1378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001379 // Replace the trailing bytes with the replacement
1380 // character if we were converting; if we weren't,
1381 // leave the UTF8 checking code to do it, as it
1382 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001383 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001384#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001385 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001386#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001387 ))
1388 {
1389 while (conv_restlen > 0)
1390 {
1391 *(--ptr) = bad_char_behavior;
1392 --conv_restlen;
1393 }
1394 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001395 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001396#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001397 if (iconv_fd != (iconv_t)-1)
1398 {
1399 iconv_close(iconv_fd);
1400 iconv_fd = (iconv_t)-1;
1401 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001402#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001403 }
1404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407 skip_read = FALSE;
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * At start of file (or after crypt magic number): Check for BOM.
1411 * Also check for a BOM for other Unicode encodings, but not after
1412 * converting with 'charconvert' or when a BOM has already been
1413 * found.
1414 */
1415 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001416#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001417 || (cryptkey != NULL
1418 && filesize == crypt_get_header_len(
1419 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
1422 && (fio_flags == FIO_UCSBOM
1423 || (!curbuf->b_p_bomb
1424 && tmpname == NULL
1425 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1426 {
1427 char_u *ccname;
1428 int blen;
1429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (size < 2 || curbuf->b_p_bin)
1432 ccname = NULL;
1433 else
1434 ccname = check_for_bom(ptr, size, &blen,
1435 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1436 if (ccname != NULL)
1437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 filesize += blen;
1440 size -= blen;
1441 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001442 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001445 curbuf->b_start_bomb = TRUE;
1446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 if (fio_flags == FIO_UCSBOM)
1450 {
1451 if (ccname == NULL)
1452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001453 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 advance_fenc = TRUE;
1455 }
1456 else
1457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001458 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (fenc_alloced)
1460 vim_free(fenc);
1461 fenc = ccname;
1462 fenc_alloced = FALSE;
1463 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001464 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 skip_read = TRUE;
1466 goto retry;
1467 }
1468 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001471 ptr -= conv_restlen;
1472 size += conv_restlen;
1473 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /*
1475 * Break here for a read error or end-of-file.
1476 */
1477 if (size <= 0)
1478 break;
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
Bram Moolenaar13505972019-01-24 15:04:48 +01001481#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (iconv_fd != (iconv_t)-1)
1483 {
1484 /*
1485 * Attempt conversion of the read bytes to 'encoding' using
1486 * iconv().
1487 */
1488 const char *fromp;
1489 char *top;
1490 size_t from_size;
1491 size_t to_size;
1492
1493 fromp = (char *)ptr;
1494 from_size = size;
1495 ptr += size;
1496 top = (char *)ptr;
1497 to_size = real_size - size;
1498
1499 /*
1500 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001501 * another conversion. Except for when there is no
1502 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001504 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1505 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1507 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001508 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001509 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001510 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001511 if (conv_error == 0)
1512 conv_error = readfile_linenr(linecnt,
1513 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001516 ++fromp;
1517 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001518 if (bad_char_behavior == BAD_KEEP)
1519 {
1520 *top++ = *(fromp - 1);
1521 --to_size;
1522 }
1523 else if (bad_char_behavior != BAD_DROP)
1524 {
1525 *top++ = bad_char_behavior;
1526 --to_size;
1527 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
1530 if (from_size > 0)
1531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001532 // Some remaining characters, keep them for the next
1533 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1535 conv_restlen = (int)from_size;
1536 }
1537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001538 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 line_start = ptr - linerest;
1540 mch_memmove(line_start, buffer, (size_t)linerest);
1541 size = (long)((char_u *)top - ptr);
1542 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaar4f974752019-02-17 17:44:42 +01001545#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (fio_flags & FIO_CODEPAGE)
1547 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001548 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001549 WCHAR ucs2buf[3];
1550 int ucs2len;
1551 int codepage = FIO_GET_CP(fio_flags);
1552 int bytelen;
1553 int found_bad;
1554 char replstr[2];
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 /*
1557 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001558 * a codepage, using standard MS-Windows functions. This
1559 * requires two steps:
1560 * 1. convert from 'fileencoding' to ucs-2
1561 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001563 * Because there may be illegal bytes AND an incomplete byte
1564 * sequence at the end, we may have to do the conversion one
1565 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001568 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001569 if (bad_char_behavior > 0)
1570 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001572 replstr[0] = '?';
1573 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001576 * Move the bytes to the end of the buffer, so that we have
1577 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001579 src = ptr + real_size - size;
1580 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001582 /*
1583 * Do the conversion.
1584 */
1585 dst = ptr;
1586 size = size;
1587 while (size > 0)
1588 {
1589 found_bad = FALSE;
1590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001591# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001592 if (codepage == CP_UTF8)
1593 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001594 // Handle CP_UTF8 input ourselves to be able to handle
1595 // trailing bytes properly.
1596 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001597 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001598 if (bytelen > size)
1599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001600 // Only got some bytes of a character. Normally
1601 // it's put in "conv_rest", but if it's too long
1602 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001603 if (bytelen <= CONV_RESTLEN)
1604 break;
1605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001607 bytelen = size;
1608 found_bad = TRUE;
1609 }
1610 else
1611 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001612 int u8c = utf_ptr2char(src);
1613
Bram Moolenaar86e01082005-12-29 22:45:34 +00001614 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001615 found_bad = TRUE;
1616 ucs2buf[0] = u8c;
1617 ucs2len = 1;
1618 }
1619 }
1620 else
1621# endif
1622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001623 // We don't know how long the byte sequence is, try
1624 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001625 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1626 ++bytelen)
1627 {
1628 ucs2len = MultiByteToWideChar(codepage,
1629 MB_ERR_INVALID_CHARS,
1630 (LPCSTR)src, bytelen,
1631 ucs2buf, 3);
1632 if (ucs2len > 0)
1633 break;
1634 }
1635 if (ucs2len == 0)
1636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637 // If we have only one byte then it's probably an
1638 // incomplete byte sequence. Otherwise discard
1639 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001640 if (size == 1)
1641 break;
1642 found_bad = TRUE;
1643 bytelen = 1;
1644 }
1645 }
1646
1647 if (!found_bad)
1648 {
1649 int i;
1650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001651 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001652 if (enc_utf8)
1653 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001654 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001655 for (i = 0; i < ucs2len; ++i)
1656 dst += utf_char2bytes(ucs2buf[i], dst);
1657 }
1658 else
1659 {
1660 BOOL bad = FALSE;
1661 int dstlen;
1662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 // From UCS-2 to "enc_codepage". If the
1664 // conversion uses the default character "?",
1665 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001666 dstlen = WideCharToMultiByte(enc_codepage, 0,
1667 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001668 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 replstr, &bad);
1670 if (bad)
1671 found_bad = TRUE;
1672 else
1673 dst += dstlen;
1674 }
1675 }
1676
1677 if (found_bad)
1678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001680 if (can_retry)
1681 goto rewind_retry;
1682 if (conv_error == 0)
1683 conv_error = readfile_linenr(linecnt, ptr, dst);
1684 if (bad_char_behavior != BAD_DROP)
1685 {
1686 if (bad_char_behavior == BAD_KEEP)
1687 {
1688 mch_memmove(dst, src, bytelen);
1689 dst += bytelen;
1690 }
1691 else
1692 *dst++ = bad_char_behavior;
1693 }
1694 }
1695
1696 src += bytelen;
1697 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001699
1700 if (size > 0)
1701 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001703 mch_memmove(conv_rest, src, size);
1704 conv_restlen = size;
1705 }
1706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001707 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001708 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001711#endif
1712#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (fio_flags & FIO_MACROMAN)
1714 {
1715 /*
1716 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001717 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001719 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
1722 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (fio_flags != 0)
1725 {
1726 int u8c;
1727 char_u *dest;
1728 char_u *tail = NULL;
1729
1730 /*
1731 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1732 * "enc_utf8" not set: Convert Unicode to Latin1.
1733 * Go from end to start through the buffer, because the number
1734 * of bytes may increase.
1735 * "dest" points to after where the UTF-8 bytes go, "p" points
1736 * to after the next character to convert.
1737 */
1738 dest = ptr + real_size;
1739 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1740 {
1741 p = ptr + size;
1742 if (fio_flags == FIO_UTF8)
1743 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001744 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 tail = ptr + size - 1;
1746 while (tail > ptr && (*tail & 0xc0) == 0x80)
1747 --tail;
1748 if (tail + utf_byte2len(*tail) <= ptr + size)
1749 tail = NULL;
1750 else
1751 p = tail;
1752 }
1753 }
1754 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 p = ptr + (size & ~1);
1758 if (size & 1)
1759 tail = p;
1760 if ((fio_flags & FIO_UTF16) && p > ptr)
1761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (fio_flags & FIO_ENDIAN_L)
1764 {
1765 u8c = (*--p << 8);
1766 u8c += *--p;
1767 }
1768 else
1769 {
1770 u8c = *--p;
1771 u8c += (*--p << 8);
1772 }
1773 if (u8c >= 0xd800 && u8c <= 0xdbff)
1774 tail = p;
1775 else
1776 p += 2;
1777 }
1778 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001779 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001781 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 p = ptr + (size & ~3);
1783 if (size & 3)
1784 tail = p;
1785 }
1786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001787 // If there is a trailing incomplete sequence move it to
1788 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (tail != NULL)
1790 {
1791 conv_restlen = (int)((ptr + size) - tail);
1792 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1793 size -= conv_restlen;
1794 }
1795
1796
1797 while (p > ptr)
1798 {
1799 if (fio_flags & FIO_LATIN1)
1800 u8c = *--p;
1801 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1802 {
1803 if (fio_flags & FIO_ENDIAN_L)
1804 {
1805 u8c = (*--p << 8);
1806 u8c += *--p;
1807 }
1808 else
1809 {
1810 u8c = *--p;
1811 u8c += (*--p << 8);
1812 }
1813 if ((fio_flags & FIO_UTF16)
1814 && u8c >= 0xdc00 && u8c <= 0xdfff)
1815 {
1816 int u16c;
1817
1818 if (p == ptr)
1819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001820 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (can_retry)
1822 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001823 if (conv_error == 0)
1824 conv_error = readfile_linenr(linecnt,
1825 ptr, p);
1826 if (bad_char_behavior == BAD_DROP)
1827 continue;
1828 if (bad_char_behavior != BAD_KEEP)
1829 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
1831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // found second word of double-word, get the first
1833 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (fio_flags & FIO_ENDIAN_L)
1835 {
1836 u16c = (*--p << 8);
1837 u16c += *--p;
1838 }
1839 else
1840 {
1841 u16c = *--p;
1842 u16c += (*--p << 8);
1843 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001844 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1845 + (u8c & 0x3ff);
1846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001847 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (u16c < 0xd800 || u16c > 0xdbff)
1849 {
1850 if (can_retry)
1851 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001852 if (conv_error == 0)
1853 conv_error = readfile_linenr(linecnt,
1854 ptr, p);
1855 if (bad_char_behavior == BAD_DROP)
1856 continue;
1857 if (bad_char_behavior != BAD_KEEP)
1858 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 }
1862 else if (fio_flags & FIO_UCS4)
1863 {
1864 if (fio_flags & FIO_ENDIAN_L)
1865 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001866 u8c = (unsigned)*--p << 24;
1867 u8c += (unsigned)*--p << 16;
1868 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 u8c += *--p;
1870 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001874 u8c += (unsigned)*--p << 8;
1875 u8c += (unsigned)*--p << 16;
1876 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001879 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 if (*--p < 0x80)
1882 u8c = *p;
1883 else
1884 {
1885 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001886 p -= len;
1887 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (len == 0)
1889 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 // Not a valid UTF-8 character, retry with
1891 // another fenc when possible, otherwise just
1892 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (can_retry)
1894 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001895 if (conv_error == 0)
1896 conv_error = readfile_linenr(linecnt,
1897 ptr, p);
1898 if (bad_char_behavior == BAD_DROP)
1899 continue;
1900 if (bad_char_behavior != BAD_KEEP)
1901 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001905 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 dest -= utf_char2len(u8c);
1908 (void)utf_char2bytes(u8c, dest);
1909 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001910 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 --dest;
1913 if (u8c >= 0x100)
1914 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001915 // character doesn't fit in latin1, retry with
1916 // another fenc when possible, otherwise just
1917 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001918 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001920 if (conv_error == 0)
1921 conv_error = readfile_linenr(linecnt, ptr, p);
1922 if (bad_char_behavior == BAD_DROP)
1923 ++dest;
1924 else if (bad_char_behavior == BAD_KEEP)
1925 *dest = u8c;
1926 else if (eap != NULL && eap->bad_char != 0)
1927 *dest = bad_char_behavior;
1928 else
1929 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 else
1932 *dest = u8c;
1933 }
1934 }
1935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 line_start = dest - linerest;
1938 mch_memmove(line_start, buffer, (size_t)linerest);
1939 size = (long)((ptr + real_size) - dest);
1940 ptr = dest;
1941 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001942 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001944 int incomplete_tail = FALSE;
1945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001947 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001949 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001950 int l;
1951
1952 if (todo <= 0)
1953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (*p >= 0x80)
1955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001956 // A length of 1 means it's an illegal byte. Accept
1957 // an incomplete character at the end though, the next
1958 // read() will get the next bytes, we'll check it
1959 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001960 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001961 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 // Avoid retrying with a different encoding when
1964 // a truncated file is more likely, or attempting
1965 // to read the rest of an incomplete sequence when
1966 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001967 if (p > ptr || filesize > 0)
1968 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 // Incomplete byte sequence, move it to conv_rest[]
1970 // and try to read the rest of it, unless we've
1971 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001972 if (p > ptr)
1973 {
1974 conv_restlen = todo;
1975 mch_memmove(conv_rest, p, conv_restlen);
1976 size -= conv_restlen;
1977 break;
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001980 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001981 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001982 // Illegal byte. If we can try another encoding
1983 // do that, unless at EOF where a truncated
1984 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001985 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001986 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001987#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001988 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001989 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1990 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001991#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001993 if (conv_error == 0 && illegal_byte == 0)
1994 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001997 if (bad_char_behavior == BAD_DROP)
1998 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001999 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002000 --p;
2001 --size;
2002 }
2003 else if (bad_char_behavior != BAD_KEEP)
2004 *p = bad_char_behavior;
2005 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002006 else
2007 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002010 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002015#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002016 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002018 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002020#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002022 advance_fenc = TRUE;
2023 file_rewind = TRUE;
2024 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 filesize += size;
2030
2031 /*
2032 * when reading the first part of a file: guess EOL type
2033 */
2034 if (fileformat == EOL_UNKNOWN)
2035 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002036 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (try_dos || try_unix)
2038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002040 if (try_mac)
2041 try_mac = 1;
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 for (p = ptr; p < ptr + size; ++p)
2044 {
2045 if (*p == NL)
2046 {
2047 if (!try_unix
2048 || (try_dos && p > ptr && p[-1] == CAR))
2049 fileformat = EOL_DOS;
2050 else
2051 fileformat = EOL_UNIX;
2052 break;
2053 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002054 else if (*p == CAR && try_mac)
2055 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (fileformat == EOL_UNIX && try_mac)
2060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 try_mac = 1;
2063 try_unix = 1;
2064 for (; p >= ptr && *p != CAR; p--)
2065 ;
2066 if (p >= ptr)
2067 {
2068 for (p = ptr; p < ptr + size; ++p)
2069 {
2070 if (*p == NL)
2071 try_unix++;
2072 else if (*p == CAR)
2073 try_mac++;
2074 }
2075 if (try_mac > try_unix)
2076 fileformat = EOL_MAC;
2077 }
2078 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002079 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // Looking for CR but found no end-of-line markers at
2081 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002082 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (fileformat == EOL_UNKNOWN && try_mac)
2087 fileformat = EOL_MAC;
2088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (fileformat == EOL_UNKNOWN)
2091 fileformat = default_fileformat();
2092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002094 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 set_fileformat(fileformat, OPT_LOCAL);
2096 }
2097 }
2098
2099 /*
2100 * This loop is executed once for every character read.
2101 * Keep it fast!
2102 */
2103 if (fileformat == EOL_MAC)
2104 {
2105 --ptr;
2106 while (++ptr, --size >= 0)
2107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if ((c = *ptr) != NUL && c != CAR && c != NL)
2110 continue;
2111 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 else
2116 {
2117 if (skip_count == 0)
2118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002119 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 len = (colnr_T) (ptr - line_start + 1);
2121 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2122 {
2123 error = TRUE;
2124 break;
2125 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002126#ifdef FEAT_PERSISTENT_UNDO
2127 if (read_undo_file)
2128 sha256_update(&sha_ctx, line_start, len);
2129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 ++lnum;
2131 if (--read_count == 0)
2132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 error = TRUE; // break loop
2134 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 break;
2136 }
2137 }
2138 else
2139 --skip_count;
2140 line_start = ptr + 1;
2141 }
2142 }
2143 }
2144 else
2145 {
2146 --ptr;
2147 while (++ptr, --size >= 0)
2148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 continue;
2151 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 else
2154 {
2155 if (skip_count == 0)
2156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002157 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 len = (colnr_T)(ptr - line_start + 1);
2159 if (fileformat == EOL_DOS)
2160 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002161 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 ptr[-1] = NUL;
2165 --len;
2166 }
2167 /*
2168 * Reading in Dos format, but no CR-LF found!
2169 * When 'fileformats' includes "unix", delete all
2170 * the lines read so far and start all over again.
2171 * Otherwise give an error message later.
2172 */
2173 else if (ff_error != EOL_DOS)
2174 {
2175 if ( try_unix
2176 && !read_stdin
2177 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002178 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2179 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 set_fileformat(EOL_UNIX, OPT_LOCAL);
2184 file_rewind = TRUE;
2185 keep_fileformat = TRUE;
2186 goto retry;
2187 }
2188 ff_error = EOL_DOS;
2189 }
2190 }
2191 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2192 {
2193 error = TRUE;
2194 break;
2195 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002196#ifdef FEAT_PERSISTENT_UNDO
2197 if (read_undo_file)
2198 sha256_update(&sha_ctx, line_start, len);
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ++lnum;
2201 if (--read_count == 0)
2202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 error = TRUE; // break loop
2204 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 break;
2206 }
2207 }
2208 else
2209 --skip_count;
2210 line_start = ptr + 1;
2211 }
2212 }
2213 }
2214 linerest = (long)(ptr - line_start);
2215 ui_breakcheck();
2216 }
2217
2218failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (error && read_count == 0)
2221 error = FALSE;
2222
2223 /*
2224 * If we get EOF in the middle of a line, note the fact and
2225 * complete the line ourselves.
2226 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2227 */
2228 if (!error
2229 && !got_int
2230 && linerest != 0
2231 && !(!curbuf->b_p_bin
2232 && fileformat == EOL_DOS
2233 && *line_start == Ctrl_Z
2234 && ptr == line_start + 1))
2235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 curbuf->b_p_eol = FALSE;
2239 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002240 len = (colnr_T)(ptr - line_start + 1);
2241 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 error = TRUE;
2243 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002244 {
2245#ifdef FEAT_PERSISTENT_UNDO
2246 if (read_undo_file)
2247 sha256_update(&sha_ctx, line_start, len);
2248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002257 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002258 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002259 crypt_free_state(curbuf->b_cryptstate);
2260 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002261 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002262 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2263 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002264 // Don't set cryptkey to NULL, it's used below as a flag that
2265 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#endif
2267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 // If editing a new file: set 'fenc' for the current buffer.
2269 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002272 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (fenc_alloced)
2274 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002275#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279
2280 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002281 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002282#ifdef HAVE_FD_CLOEXEC
2283 else
2284 {
2285 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002286
Bram Moolenaarf05da212009-11-17 16:13:15 +00002287 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002288 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002289 }
2290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 vim_free(buffer);
2292
2293#ifdef HAVE_DUP
2294 if (read_stdin)
2295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002296 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002298 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300#endif
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (tmpname != NULL)
2303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 vim_free(tmpname);
2306 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 /*
2310 * In recovery mode everything but autocommands is skipped.
2311 */
2312 if (!recoverymode)
2313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2316 {
2317#ifdef FEAT_NETBEANS_INTG
2318 netbeansFireChanges = 0;
2319#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002320 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifdef FEAT_NETBEANS_INTG
2322 netbeansFireChanges = 1;
2323#endif
2324 --linecnt;
2325 }
2326 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2327 if (filesize == 0)
2328 linecnt = 0;
2329 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002332#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // After reading the text into the buffer the diff info needs to
2334 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002335 diff_invalidate(curbuf);
2336#endif
2337#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // All folds in the window are invalid now. Mark them for update
2339 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002340 foldUpdateAll(curwin);
2341#endif
2342 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002343 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 appended_lines_mark(from, linecnt);
2345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#ifndef ALWAYS_USE_GUI
2347 /*
2348 * If we were reading from the same terminal as where messages go,
2349 * the screen will have been messed up.
2350 * Switch on raw mode now and clear the screen.
2351 */
2352 if (read_stdin)
2353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002354 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 starttermcap();
2356 screenclear();
2357 }
2358#endif
2359
2360 if (got_int)
2361 {
2362 if (!(flags & READ_DUMMY))
2363 {
2364 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2365 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 msg_scroll = msg_save;
2369#ifdef FEAT_VIMINFO
2370 check_marks_read();
2371#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002372 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374
2375 if (!filtering && !(flags & READ_DUMMY))
2376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 c = FALSE;
2379
2380#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 STRCAT(IObuff, _("[fifo]"));
2384 c = TRUE;
2385 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002386 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 STRCAT(IObuff, _("[socket]"));
2389 c = TRUE;
2390 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002391# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002392 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002393 {
2394 STRCAT(IObuff, _("[character special]"));
2395 c = TRUE;
2396 }
2397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#endif
2399 if (curbuf->b_p_ro)
2400 {
2401 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2402 c = TRUE;
2403 }
2404 if (read_no_eol_lnum)
2405 {
2406 msg_add_eol();
2407 c = TRUE;
2408 }
2409 if (ff_error == EOL_DOS)
2410 {
2411 STRCAT(IObuff, _("[CR missing]"));
2412 c = TRUE;
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (split)
2415 {
2416 STRCAT(IObuff, _("[long lines split]"));
2417 c = TRUE;
2418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (notconverted)
2420 {
2421 STRCAT(IObuff, _("[NOT converted]"));
2422 c = TRUE;
2423 }
2424 else if (converted)
2425 {
2426 STRCAT(IObuff, _("[converted]"));
2427 c = TRUE;
2428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#ifdef FEAT_CRYPT
2430 if (cryptkey != NULL)
2431 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002432 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 c = TRUE;
2434 }
2435#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002436 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002438 sprintf((char *)IObuff + STRLEN(IObuff),
2439 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 c = TRUE;
2441 }
2442 else if (illegal_byte > 0)
2443 {
2444 sprintf((char *)IObuff + STRLEN(IObuff),
2445 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2446 c = TRUE;
2447 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002448 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 STRCAT(IObuff, _("[READ ERRORS]"));
2451 c = TRUE;
2452 }
2453 if (msg_add_fileformat(fileformat))
2454 c = TRUE;
2455#ifdef FEAT_CRYPT
2456 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002457 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002458 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 else
2460#endif
2461 msg_add_lines(c, (long)linecnt, filesize);
2462
Bram Moolenaard23a8232018-02-10 18:45:26 +01002463 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 msg_scrolled_ign = TRUE;
2465#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002466 // Don't show the message when reading stdin, it would end up in a
2467 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (read_stdin || read_buffer)
2469 p = msg_may_trunc(FALSE, IObuff);
2470 else
2471#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002472 {
2473 if (msg_col > 0)
2474 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002475 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002478 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 // Need to repeat the message after redrawing when:
2480 // - When reading from stdin (the screen will be cleared next).
2481 // - When restart_edit is set (otherwise there will be a delay
2482 // before redrawing).
2483 // - When the screen was scrolled but there is no wait-return
2484 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002485 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 msg_scrolled_ign = FALSE;
2487 }
2488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002489 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002491 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002492 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 curbuf->b_p_ro = TRUE;
2494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002495 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /*
2498 * In Ex mode: cursor at last new line.
2499 * Otherwise: cursor at first new line.
2500 */
2501 if (exmode_active)
2502 curwin->w_cursor.lnum = from + linecnt;
2503 else
2504 curwin->w_cursor.lnum = from + 1;
2505 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002506 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaare1004402020-10-24 20:49:43 +02002508 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002509 {
2510 // Set '[ and '] marks to the newly read lines.
2511 curbuf->b_op_start.lnum = from + 1;
2512 curbuf->b_op_start.col = 0;
2513 curbuf->b_op_end.lnum = from + linecnt;
2514 curbuf->b_op_end.col = 0;
2515 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002516
Bram Moolenaar4f974752019-02-17 17:44:42 +01002517#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002518 /*
2519 * Work around a weird problem: When a file has two links (only
2520 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002521 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002522 * It's correct again after reading the file, thus reset the timestamp
2523 * here.
2524 */
2525 if (newfile && !read_stdin && !read_buffer
2526 && mch_stat((char *)fname, &st) >= 0)
2527 {
2528 buf_store_time(curbuf, &st, fname);
2529 curbuf->b_mtime_read = curbuf->b_mtime;
2530 }
2531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533 msg_scroll = msg_save;
2534
2535#ifdef FEAT_VIMINFO
2536 /*
2537 * Get the marks before executing autocommands, so they can be used there.
2538 */
2539 check_marks_read();
2540#endif
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002543 * We remember if the last line of the read didn't have
2544 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2545 * or writing the read again with 'binary' on. The latter is required
2546 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002548 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 // When reloading a buffer put the cursor at the first line that is
2551 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002552 if (flags & READ_KEEP_UNDO)
2553 u_find_first_changed();
2554
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002555#ifdef FEAT_PERSISTENT_UNDO
2556 /*
2557 * When opening a new file locate undo info and read it.
2558 */
2559 if (read_undo_file)
2560 {
2561 char_u hash[UNDO_HASH_SIZE];
2562
2563 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002564 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002565 }
2566#endif
2567
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002568 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 int m = msg_scroll;
2571 int n = msg_scrolled;
2572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002573 // Save the fileformat now, otherwise the buffer will be considered
2574 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002575 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 save_file_ff(curbuf);
2577
2578 /*
2579 * The output from the autocommands should not overwrite anything and
2580 * should not be overwritten: Set msg_scroll, restore its value if no
2581 * output was done.
2582 */
2583 msg_scroll = TRUE;
2584 if (filtering)
2585 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2586 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002587 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2590 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002591 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2592 /*
2593 * EVENT_FILETYPE was not triggered but the buffer already has a
2594 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2595 */
2596 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2597 TRUE, curbuf);
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 else
2600 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2601 FALSE, NULL, eap);
2602 if (msg_scrolled == n)
2603 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002604# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 if (recoverymode && error)
2611 return FAIL;
2612 return OK;
2613}
2614
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002615#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002616/*
2617 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2618 * which is the name of files used for process substitution output by
2619 * some shells on some operating systems, e.g., bash on SunOS.
2620 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2621 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002624{
2625 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2626 && VIM_ISDIGIT(fname[8])
2627 && *skipdigits(fname + 9) == NUL
2628 && (fname[9] != NUL
2629 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2630}
2631#endif
2632
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002633/*
2634 * From the current line count and characters read after that, estimate the
2635 * line number where we are now.
2636 * Used for error messages that include a line number.
2637 */
2638 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002639readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 linenr_T linecnt, // line count before reading more bytes
2641 char_u *p, // start of more bytes read
2642 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002643{
2644 char_u *s;
2645 linenr_T lnum;
2646
2647 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2648 for (s = p; s < endp; ++s)
2649 if (*s == '\n')
2650 ++lnum;
2651 return lnum;
2652}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002655 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2656 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 * Returns OK or FAIL.
2658 */
2659 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002660prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar13505972019-01-24 15:04:48 +01002662 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (eap->cmd == NULL)
2664 return FAIL;
2665
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002666 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2667 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002668 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002669 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002670
2671 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002672 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002673 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return OK;
2675}
2676
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002677/*
2678 * Set default or forced 'fileformat' and 'binary'.
2679 */
2680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002681set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002682{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002683 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002684 if (set_options)
2685 {
2686 if (eap != NULL && eap->force_ff != 0)
2687 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2688 else if (*p_ffs != NUL)
2689 set_fileformat(default_fileformat(), OPT_LOCAL);
2690 }
2691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002692 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002693 if (eap != NULL && eap->force_bin != 0)
2694 {
2695 int oldval = curbuf->b_p_bin;
2696
2697 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2698 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2699 }
2700}
2701
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002702/*
2703 * Set forced 'fileencoding'.
2704 */
2705 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002706set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002707{
2708 if (eap->force_enc != 0)
2709 {
2710 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2711
2712 if (fenc != NULL)
2713 set_string_option_direct((char_u *)"fenc", -1,
2714 fenc, OPT_FREE|OPT_LOCAL, 0);
2715 vim_free(fenc);
2716 }
2717}
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719/*
2720 * Find next fileencoding to use from 'fileencodings'.
2721 * "pp" points to fenc_next. It's advanced to the next item.
2722 * When there are no more items, an empty string is returned and *pp is set to
2723 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002724 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2725 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002728next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 char_u *p;
2731 char_u *r;
2732
Bram Moolenaarf077db22019-08-13 00:18:24 +02002733 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (**pp == NUL)
2735 {
2736 *pp = NULL;
2737 return (char_u *)"";
2738 }
2739 p = vim_strchr(*pp, ',');
2740 if (p == NULL)
2741 {
2742 r = enc_canonize(*pp);
2743 *pp += STRLEN(*pp);
2744 }
2745 else
2746 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002747 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 *pp = p + 1;
2749 if (r != NULL)
2750 {
2751 p = enc_canonize(r);
2752 vim_free(r);
2753 r = p;
2754 }
2755 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002756 if (r != NULL)
2757 *alloced = TRUE;
2758 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002760 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 r = (char_u *)"";
2762 *pp = NULL;
2763 }
2764 return r;
2765}
2766
Bram Moolenaar13505972019-01-24 15:04:48 +01002767#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768/*
2769 * Convert a file with the 'charconvert' expression.
2770 * This closes the file which is to be read, converts it and opens the
2771 * resulting file for reading.
2772 * Returns name of the resulting converted file (the caller should delete it
2773 * after reading it).
2774 * Returns NULL if the conversion failed ("*fdp" is not set) .
2775 */
2776 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002777readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002778 char_u *fname, // name of input file
2779 char_u *fenc, // converted from
2780 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002783 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002785 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002787 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002790 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 *fdp = -1;
2792 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2793 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002794 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2796 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002797 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799
2800 if (errmsg != NULL)
2801 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002802 // Don't use emsg(), it breaks mappings, the retry with
2803 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002804 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (tmpname != NULL)
2806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002807 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002808 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810 }
2811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (*fdp < 0)
2814 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2815
2816 return tmpname;
2817}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#endif
2819
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002820#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002822 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2824 * *filesizep are updated.
2825 * Return the (new) encryption key, NULL for no encryption.
2826 */
2827 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002828check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 char_u *cryptkey, // previous encryption key or NULL
2830 char_u *ptr, // pointer to read bytes
2831 long *sizep, // length of read bytes
2832 off_T *filesizep, // nr of bytes used from file
2833 int newfile, // editing a new buffer
2834 char_u *fname, // file name to display
2835 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002837 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002838 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002839
2840 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002842 // Mark the buffer as read-only until the decryption has taken place.
2843 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002844 curbuf->b_p_ro = TRUE;
2845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002846 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002847 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002848 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
2850 if (*curbuf->b_p_key)
2851 cryptkey = curbuf->b_p_key;
2852 else
2853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002854 // When newfile is TRUE, store the typed key in the 'key'
2855 // option and don't free it. bf needs hash of the key saved.
2856 // Don't ask for the key again when first time Enter was hit.
2857 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002858 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002859 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002860 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002861 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002862 *did_ask = TRUE;
2863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002864 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 if (cryptkey != NULL && *cryptkey == NUL)
2866 {
2867 if (cryptkey != curbuf->b_p_key)
2868 vim_free(cryptkey);
2869 cryptkey = NULL;
2870 }
2871 }
2872 }
2873
2874 if (cryptkey != NULL)
2875 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002876 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002877
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002878 curbuf->b_cryptstate = crypt_create_from_header(
2879 method, cryptkey, ptr);
2880 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002883 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002884 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002885 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002886 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002887 *filesizep += header_len;
2888 *sizep -= header_len;
2889 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002892 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002895 // When starting to edit a new file which does not have encryption, clear
2896 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002897 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2899
2900 return cryptkey;
2901}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002902#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002905 * Return TRUE if a file appears to be read-only from the file permissions.
2906 */
2907 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002908check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002909 char_u *fname, // full path to file
2910 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002911{
2912#ifndef USE_MCH_ACCESS
2913 int fd = 0;
2914#endif
2915
2916 return (
2917#ifdef USE_MCH_ACCESS
2918# ifdef UNIX
2919 (perm & 0222) == 0 ||
2920# endif
2921 mch_access((char *)fname, W_OK)
2922#else
2923 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2924 ? TRUE : (close(fd), FALSE)
2925#endif
2926 );
2927}
2928
Bram Moolenaara7870192019-02-14 12:56:36 +01002929#if defined(HAVE_FSYNC) || defined(PROTO)
2930/*
2931 * Call fsync() with Mac-specific exception.
2932 * Return fsync() result: zero for success.
2933 */
2934 int
2935vim_fsync(int fd)
2936{
2937 int r;
2938
2939# ifdef MACOS_X
2940 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002941 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002942# endif
2943 r = fsync(fd);
2944 return r;
2945}
2946#endif
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002949 * Set the name of the current buffer. Use when the buffer doesn't have a
2950 * name and a ":r" or ":w" command with a file name is used.
2951 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002952 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002953set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002954{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002955 buf_T *buf = curbuf;
2956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002958 if (curbuf->b_p_bl)
2959 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2960 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002961#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002962 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002963 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002964#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002965 if (curbuf != buf)
2966 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002967 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002969 return FAIL;
2970 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002971
2972 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2973 curbuf->b_flags |= BF_NOTEDITED;
2974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002975 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002976 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2977 if (curbuf->b_p_bl)
2978 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002979#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002980 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002981 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002982#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002984 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002985 if (*curbuf->b_p_ft == NUL)
2986 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002987 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002988 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002989 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002990 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002991
2992 return OK;
2993}
2994
2995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 * Put file name into IObuff with quotes.
2997 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 if (fname == NULL)
3002 fname = (char_u *)"-stdin-";
3003 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3004 IObuff[0] = '"';
3005 STRCAT(IObuff, "\" ");
3006}
3007
3008/*
3009 * Append message for text mode to IObuff.
3010 * Return TRUE if something appended.
3011 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015#ifndef USE_CRNL
3016 if (eol_type == EOL_DOS)
3017 {
3018 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3019 return TRUE;
3020 }
3021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (eol_type == EOL_MAC)
3023 {
3024 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3025 return TRUE;
3026 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003027#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (eol_type == EOL_UNIX)
3029 {
3030 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3031 return TRUE;
3032 }
3033#endif
3034 return FALSE;
3035}
3036
3037/*
3038 * Append line and character count to IObuff.
3039 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041msg_add_lines(
3042 int insert_space,
3043 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003044 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 char_u *p;
3047
3048 p = IObuff + STRLEN(IObuff);
3049
3050 if (insert_space)
3051 *p++ = ' ';
3052 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003053 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003054 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else
3056 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003057 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003059 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003060 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003061 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
3063}
3064
3065/*
3066 * Append message for missing line separator to IObuff.
3067 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003069msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3072}
3073
Bram Moolenaar473952e2019-09-28 16:30:04 +02003074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003075time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003077#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003078 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3079 // the seconds. Since the roundoff is done when flushing the inode, the
3080 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return (t1 - t2 > 1 || t2 - t1 > 1);
3082#else
3083 return (t1 != t2);
3084#endif
3085}
3086
3087/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003088 * Return TRUE if file encoding "fenc" requires conversion from or to
3089 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003091 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003094 int same_encoding;
3095 int enc_flags;
3096 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003098 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003099 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003100 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003101 fenc_flags = 0;
3102 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003103 else
3104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003105 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3106 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003107 enc_flags = get_fio_flags(p_enc);
3108 fenc_flags = get_fio_flags(fenc);
3109 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3110 }
3111 if (same_encoding)
3112 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003113 // Specified encoding matches with 'encoding'. This requires
3114 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003115 return enc_unicode != 0;
3116 }
3117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003118 // Encodings differ. However, conversion is not needed when 'enc' is any
3119 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003120 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121}
3122
3123/*
3124 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3125 * internal conversion.
3126 * if "ptr" is an empty string, use 'encoding'.
3127 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003129get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 int prop;
3132
3133 if (*ptr == NUL)
3134 ptr = p_enc;
3135
3136 prop = enc_canon_props(ptr);
3137 if (prop & ENC_UNICODE)
3138 {
3139 if (prop & ENC_2BYTE)
3140 {
3141 if (prop & ENC_ENDIAN_L)
3142 return FIO_UCS2 | FIO_ENDIAN_L;
3143 return FIO_UCS2;
3144 }
3145 if (prop & ENC_4BYTE)
3146 {
3147 if (prop & ENC_ENDIAN_L)
3148 return FIO_UCS4 | FIO_ENDIAN_L;
3149 return FIO_UCS4;
3150 }
3151 if (prop & ENC_2WORD)
3152 {
3153 if (prop & ENC_ENDIAN_L)
3154 return FIO_UTF16 | FIO_ENDIAN_L;
3155 return FIO_UTF16;
3156 }
3157 return FIO_UTF8;
3158 }
3159 if (prop & ENC_LATIN1)
3160 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003161 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return 0;
3163}
3164
Bram Moolenaar473952e2019-09-28 16:30:04 +02003165#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166/*
3167 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3168 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3169 * Used for conversion between 'encoding' and 'fileencoding'.
3170 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003171 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003172get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 int cp;
3175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003176 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (!enc_utf8 && enc_codepage <= 0)
3178 return 0;
3179
3180 cp = encname2codepage(ptr);
3181 if (cp == 0)
3182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003183# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (STRCMP(ptr, "utf-8") == 0)
3185 cp = CP_UTF8;
3186 else
3187# endif
3188 return 0;
3189 }
3190 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3191}
3192#endif
3193
Bram Moolenaar473952e2019-09-28 16:30:04 +02003194#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195/*
3196 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3197 * needed for the internal conversion to/from utf-8 or latin1.
3198 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003199 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003200get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3203 && (enc_canon_props(ptr) & ENC_MACROMAN))
3204 return FIO_MACROMAN;
3205 return 0;
3206}
3207#endif
3208
3209/*
3210 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3211 * "size" must be at least 2.
3212 * Return the name of the encoding and set "*lenp" to the length.
3213 * Returns NULL when no BOM found.
3214 */
3215 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003216check_for_bom(
3217 char_u *p,
3218 long size,
3219 int *lenp,
3220 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 char *name = NULL;
3223 int len = 2;
3224
3225 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003226 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 len = 3;
3230 }
3231 else if (p[0] == 0xff && p[1] == 0xfe)
3232 {
3233 if (size >= 4 && p[2] == 0 && p[3] == 0
3234 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003236 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 len = 4;
3238 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003239 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003241 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242 // utf-16le is preferred, it also works for ucs-2le text
3243 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245 else if (p[0] == 0xfe && p[1] == 0xff
3246 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003248 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003249 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003250 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003251 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003252 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3255 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003257 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 len = 4;
3259 }
3260
3261 *lenp = len;
3262 return (char_u *)name;
3263}
3264
3265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * Try to find a shortname by comparing the fullname with the current
3267 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003268 * Returns "full_path" or pointer into "full_path" if shortened.
3269 */
3270 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003272{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003273 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003274 char_u *p = full_path;
3275
Bram Moolenaard9462e32011-04-11 21:35:11 +02003276 dirname = alloc(MAXPATHL);
3277 if (dirname == NULL)
3278 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003279 if (mch_dirname(dirname, MAXPATHL) == OK)
3280 {
3281 p = shorten_fname(full_path, dirname);
3282 if (p == NULL || *p == NUL)
3283 p = full_path;
3284 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003285 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003286 return p;
3287}
3288
3289/*
3290 * Try to find a shortname by comparing the fullname with the current
3291 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * Returns NULL if not shorter name possible, pointer into "full_path"
3293 * otherwise.
3294 */
3295 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003296shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 int len;
3299 char_u *p;
3300
3301 if (full_path == NULL)
3302 return NULL;
3303 len = (int)STRLEN(dir_name);
3304 if (fnamencmp(dir_name, full_path, len) == 0)
3305 {
3306 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003307#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003309 * MS-Windows: when a file is in the root directory, dir_name will end
3310 * in a slash, since C: by itself does not define a specific dir. In
3311 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 */
3313 if (!((len > 2) && (*(p - 2) == ':')))
3314#endif
3315 {
3316 if (vim_ispathsep(*p))
3317 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003318#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
3320 p = NULL;
3321#endif
3322 }
3323 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003324#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 /*
3326 * When using a file in the current drive, remove the drive name:
3327 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3328 * a floppy from "A:\dir" to "B:\dir".
3329 */
3330 else if (len > 3
3331 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3332 && full_path[1] == ':'
3333 && vim_ispathsep(full_path[2]))
3334 p = full_path + 2;
3335#endif
3336 else
3337 p = NULL;
3338 return p;
3339}
3340
3341/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003342 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * When "force" is TRUE: Use full path from now on for files currently being
3344 * edited, both for file name and swap file name. Try to shorten the file
3345 * names a bit, if safe to do so.
3346 * When "force" is FALSE: Only try to shorten absolute file names.
3347 * For buffers that have buftype "nofile" or "scratch": never change the file
3348 * name.
3349 */
3350 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003351shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3352{
3353 char_u *p;
3354
3355 if (buf->b_fname != NULL
3356#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003357 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003358#endif
3359 && !path_with_url(buf->b_fname)
3360 && (force
3361 || buf->b_sfname == NULL
3362 || mch_isFullName(buf->b_sfname)))
3363 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003364 if (buf->b_sfname != buf->b_ffname)
3365 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003366 p = shorten_fname(buf->b_ffname, dirname);
3367 if (p != NULL)
3368 {
3369 buf->b_sfname = vim_strsave(p);
3370 buf->b_fname = buf->b_sfname;
3371 }
3372 if (p == NULL || buf->b_fname == NULL)
3373 buf->b_fname = buf->b_ffname;
3374 }
3375}
3376
3377/*
3378 * Shorten filenames for all buffers.
3379 */
3380 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003381shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 char_u dirname[MAXPATHL];
3384 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
3386 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003387 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003389 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003391 // Always make the swap file name a full path, a "nofile" buffer may
3392 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003393 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003396 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003397#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003398 popup_update_preview_title();
3399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400}
3401
3402#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3403 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003404 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 || defined(PROTO)
3406/*
3407 * Shorten all filenames in "fnames[count]" by current directory.
3408 */
3409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003410shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 int i;
3413 char_u dirname[MAXPATHL];
3414 char_u *p;
3415
3416 if (fnames == NULL || count < 1)
3417 return;
3418 mch_dirname(dirname, sizeof(dirname));
3419 for (i = 0; i < count; ++i)
3420 {
3421 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003423 // shorten_fname() returns pointer in given "fnames[i]". If free
3424 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3425 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 p = vim_strsave(p);
3427 vim_free(fnames[i]);
3428 fnames[i] = p;
3429 }
3430 }
3431}
3432#endif
3433
3434/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003435 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * fo_o_h.ext for MSDOS or when shortname option set.
3437 *
3438 * Assumed that fname is a valid name found in the filesystem we assure that
3439 * the return value is a different name and ends in 'ext'.
3440 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3441 * characters otherwise.
3442 * Space for the returned name is allocated, must be freed later.
3443 * Returns NULL when out of memory.
3444 */
3445 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003446modname(
3447 char_u *fname,
3448 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003449 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003451 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 fname, ext, prepend_dot);
3453}
3454
3455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003457 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003458 char_u *fname,
3459 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003460 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 char_u *retval;
3463 char_u *s;
3464 char_u *e;
3465 char_u *ptr;
3466 int fnamelen, extlen;
3467
3468 extlen = (int)STRLEN(ext);
3469
3470 /*
3471 * If there is no file name we must get the name of the current directory
3472 * (we need the full path in case :cd is used).
3473 */
3474 if (fname == NULL || *fname == NUL)
3475 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003476 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (retval == NULL)
3478 return NULL;
3479 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3480 (fnamelen = (int)STRLEN(retval)) == 0)
3481 {
3482 vim_free(retval);
3483 return NULL;
3484 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003485 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 retval[fnamelen++] = PATHSEP;
3488 retval[fnamelen] = NUL;
3489 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003490 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492 else
3493 {
3494 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003495 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (retval == NULL)
3497 return NULL;
3498 STRCPY(retval, fname);
3499#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003500 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#endif
3502 }
3503
3504 /*
3505 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3506 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3507 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3508 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3509 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003510 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003512 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003513 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003516 {
3517 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003522 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3524 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 s = ptr + STRLEN(ptr);
3527
3528 /*
3529 * For 8.3 file names we may have to reduce the length.
3530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 /*
3534 * If there is no file name, or the file name ends in '/', and the
3535 * extension starts with '.', put a '_' before the dot, because just
3536 * ".ext" is invalid.
3537 */
3538 if (fname == NULL || *fname == NUL
3539 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 *s++ = '_';
3543 }
3544 /*
3545 * If the extension starts with '.', truncate the base name at 8
3546 * characters
3547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003550 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
3552 s = ptr + 8;
3553 *s = '\0';
3554 }
3555 }
3556 /*
3557 * If the extension doesn't start with '.', and the file name
3558 * doesn't have an extension yet, append a '.'
3559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else if ((e = vim_strchr(ptr, '.')) == NULL)
3561 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 /*
3563 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003564 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
3566 else if ((int)STRLEN(e) + extlen > 4)
3567 s = e + 4 - extlen;
3568 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003569#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /*
3571 * If there is no file name, and the extension starts with '.', put a
3572 * '_' before the dot, because just ".ext" may be invalid if it's on a
3573 * FAT partition, and on HPFS it doesn't matter.
3574 */
3575 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3576 *s++ = '_';
3577#endif
3578
3579 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003580 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * ext can start with '.' and cannot exceed 3 more characters.
3582 */
3583 STRCPY(s, ext);
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 /*
3586 * Prepend the dot.
3587 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003588 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003590 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594 /*
3595 * Check that, after appending the extension, the file name is really
3596 * different.
3597 */
3598 if (fname != NULL && STRCMP(fname, retval) == 0)
3599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003600 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 while (--s >= ptr)
3602 {
3603 if (*s != '_')
3604 {
3605 *s = '_';
3606 break;
3607 }
3608 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003609 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 *ptr = 'v';
3611 }
3612 return retval;
3613}
3614
3615/*
3616 * Like fgets(), but if the file line is too long, it is truncated and the
3617 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003618 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
3620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003621vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 char *eof;
3624#define FGETS_SIZE 200
3625 char tbuf[FGETS_SIZE];
3626
3627 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3630 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003631 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003633 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 do
3635 {
3636 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003637 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3639 }
3640 return (eof == NULL);
3641}
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643/*
3644 * rename() only works if both files are on the same file system, this
3645 * function will (attempts to?) copy the file across if rename fails -- webb
3646 * Return -1 for failure, 0 for success.
3647 */
3648 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003649vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 int fd_in;
3652 int fd_out;
3653 int n;
3654 char *errmsg = NULL;
3655 char *buffer;
3656#ifdef AMIGA
3657 BPTR flock;
3658#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003659 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003660 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003661#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003662 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003663#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003664 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003667 * When the names are identical, there is nothing to do. When they refer
3668 * to the same file (ignoring case and slash/backslash differences) but
3669 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003672 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003673 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003674 use_tmp_file = TRUE;
3675 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003676 return 0;
3677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /*
3680 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3681 */
3682 if (mch_stat((char *)from, &st) < 0)
3683 return -1;
3684
Bram Moolenaar3576da72008-12-30 15:15:57 +00003685#ifdef UNIX
3686 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003687 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003689 // It's possible for the source and destination to be the same file.
3690 // This happens when "from" and "to" differ in case and are on a FAT32
3691 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003692 if (mch_stat((char *)to, &st_to) >= 0
3693 && st.st_dev == st_to.st_dev
3694 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003695 use_tmp_file = TRUE;
3696 }
3697#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003698#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003699 {
3700 BY_HANDLE_FILE_INFORMATION info1, info2;
3701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003702 // It's possible for the source and destination to be the same file.
3703 // In that case go through a temp file name. This makes rename("foo",
3704 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003705 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3706 && win32_fileinfo(to, &info2) == FILEINFO_OK
3707 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3708 && info1.nFileIndexHigh == info2.nFileIndexHigh
3709 && info1.nFileIndexLow == info2.nFileIndexLow)
3710 use_tmp_file = TRUE;
3711 }
3712#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003713
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003714 if (use_tmp_file)
3715 {
3716 char tempname[MAXPATHL + 1];
3717
3718 /*
3719 * Find a name that doesn't exist and is in the same directory.
3720 * Rename "from" to "tempname" and then rename "tempname" to "to".
3721 */
3722 if (STRLEN(from) >= MAXPATHL - 5)
3723 return -1;
3724 STRCPY(tempname, from);
3725 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003727 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3728 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003729 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003730 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003731 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003732 if (mch_rename(tempname, (char *)to) == 0)
3733 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003734 // Strange, the second step failed. Try moving the
3735 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003736 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003737 return -1;
3738 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003739 // If it fails for one temp name it will most likely fail
3740 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003741 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003742 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003743 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003744 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003745 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * Delete the "to" file, this is required on some systems to make the
3749 * mch_rename() work, on other systems it makes sure that we don't have
3750 * two files when the mch_rename() fails.
3751 */
3752
3753#ifdef AMIGA
3754 /*
3755 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3756 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003757 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 * deleting the "from" file (horror!) we lock it during the remove.
3759 *
3760 * When used for making a backup before writing the file: This should not
3761 * happen with ":w", because startscript() should detect this problem and
3762 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3763 * name. This problem does exist with ":w filename", but then the
3764 * original file will be somewhere else so the backup isn't really
3765 * important. If autoscripting is off the rename may fail.
3766 */
3767 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3768#endif
3769 mch_remove(to);
3770#ifdef AMIGA
3771 if (flock)
3772 UnLock(flock);
3773#endif
3774
3775 /*
3776 * First try a normal rename, return if it works.
3777 */
3778 if (mch_rename((char *)from, (char *)to) == 0)
3779 return 0;
3780
3781 /*
3782 * Rename() failed, try copying the file.
3783 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003784 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003785#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003786 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003787 acl = mch_get_acl(from);
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3790 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003791 {
3792#ifdef HAVE_ACL
3793 mch_free_acl(acl);
3794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003796 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003798 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003799 fd_out = mch_open((char *)to,
3800 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (fd_out == -1)
3802 {
3803 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003804#ifdef HAVE_ACL
3805 mch_free_acl(acl);
3806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return -1;
3808 }
3809
Bram Moolenaar473952e2019-09-28 16:30:04 +02003810 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (buffer == NULL)
3812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003814 close(fd_in);
3815#ifdef HAVE_ACL
3816 mch_free_acl(acl);
3817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return -1;
3819 }
3820
Bram Moolenaar473952e2019-09-28 16:30:04 +02003821 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003822 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 errmsg = _("E208: Error writing to \"%s\"");
3825 break;
3826 }
3827
3828 vim_free(buffer);
3829 close(fd_in);
3830 if (close(fd_out) < 0)
3831 errmsg = _("E209: Error closing \"%s\"");
3832 if (n < 0)
3833 {
3834 errmsg = _("E210: Error reading \"%s\"");
3835 to = from;
3836 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003838 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003839#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003840#ifdef HAVE_ACL
3841 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003842 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003843#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003844#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003845 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (errmsg != NULL)
3848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003849 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return -1;
3851 }
3852 mch_remove(from);
3853 return 0;
3854}
3855
3856static int already_warned = FALSE;
3857
3858/*
3859 * Check if any not hidden buffer has been changed.
3860 * Postpone the check if there are characters in the stuff buffer, a global
3861 * command is being executed, a mapping is being executed or an autocommand is
3862 * busy.
3863 * Returns TRUE if some message was written (screen should be redrawn and
3864 * cursor positioned).
3865 */
3866 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003867check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
3870 buf_T *buf;
3871 int didit = 0;
3872 int n;
3873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003874 // Don't check timestamps while system() or another low-level function may
3875 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (no_check_timestamps > 0)
3877 return FALSE;
3878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3880 // event and we would keep on checking if the file is steadily growing.
3881 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (focus && did_check_timestamps)
3883 {
3884 need_check_timestamps = TRUE;
3885 return FALSE;
3886 }
3887
3888 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003889 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 {
3893 ++no_wait_return;
3894 did_check_timestamps = TRUE;
3895 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003896 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003898 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (buf->b_nwindows > 0)
3900 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003901 bufref_T bufref;
3902
3903 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 n = buf_check_timestamp(buf, focus);
3905 if (didit < n)
3906 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003907 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003909 // Autocommands have removed the buffer, start at the
3910 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 buf = firstbuf;
3912 continue;
3913 }
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 --no_wait_return;
3917 need_check_timestamps = FALSE;
3918 if (need_wait_return && didit == 2)
3919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003921 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 out_flush();
3923 }
3924 }
3925 return didit;
3926}
3927
3928/*
3929 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3930 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3931 * empty.
3932 */
3933 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003934move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 buf_T *tbuf = curbuf;
3937 int retval = OK;
3938 linenr_T lnum;
3939 char_u *p;
3940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 curbuf = tobuf;
3943 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3944 {
3945 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3946 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3947 {
3948 vim_free(p);
3949 retval = FAIL;
3950 break;
3951 }
3952 vim_free(p);
3953 }
3954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003955 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (retval != FAIL)
3957 {
3958 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003959 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003960 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003962 // Oops! We could try putting back the saved lines, but that
3963 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 retval = FAIL;
3965 break;
3966 }
3967 }
3968
3969 curbuf = tbuf;
3970 return retval;
3971}
3972
3973/*
3974 * Check if buffer "buf" has been changed.
3975 * Also check if the file for a new buffer unexpectedly appeared.
3976 * return 1 if a changed buffer was found.
3977 * return 2 if a message has been displayed.
3978 * return 0 otherwise.
3979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981buf_check_timestamp(
3982 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003983 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003985 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 int stat_res;
3987 int retval = 0;
3988 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003989 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003991 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int helpmesg = FALSE;
3993 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003994 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3996 int can_reload = FALSE;
3997#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003998 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int orig_mode = buf->b_orig_mode;
4000#ifdef FEAT_GUI
4001 int save_mouse_correct = need_mouse_correct;
4002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004004 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004005#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004006 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004007#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004008 bufref_T bufref;
4009
4010 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 // If there is no file name, the buffer is not loaded, 'buftype' is
4013 // set, we are in the middle of a save or being called recursively: ignore
4014 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (buf->b_ffname == NULL
4016 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004017 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004020#ifdef FEAT_NETBEANS_INTG
4021 || isNetbeansBuffer(buf)
4022#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004023#ifdef FEAT_TERMINAL
4024 || buf->b_term != NULL
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 )
4027 return 0;
4028
4029 if ( !(buf->b_flags & BF_NOTEDITED)
4030 && buf->b_mtime != 0
4031 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4032 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004033 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#ifdef HAVE_ST_MODE
4035 || (int)st.st_mode != buf->b_orig_mode
4036#else
4037 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4038#endif
4039 ))
4040 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004041 long prev_b_mtime = buf->b_mtime;
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 retval = 1;
4044
Bram Moolenaar386bc822018-07-07 18:34:12 +02004045 // set b_mtime to stop further warnings (e.g., when executing
4046 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (stat_res < 0)
4048 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004049 // Check the file again later to see if it re-appears.
4050 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 buf->b_orig_size = 0;
4052 buf->b_orig_mode = 0;
4053 }
4054 else
4055 buf_store_time(buf, &st, buf->b_ffname);
4056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004057 // Don't do anything for a directory. Might contain the file
4058 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (mch_isdir(buf->b_fname))
4060 ;
4061
4062 /*
4063 * If 'autoread' is set, the buffer has no changes and the file still
4064 * exists, reload the buffer. Use the buffer-local option value if it
4065 * was set, the global option value otherwise.
4066 */
4067 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4068 && !bufIsChanged(buf) && stat_res >= 0)
4069 reload = TRUE;
4070 else
4071 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004072 if (stat_res < 0)
4073 reason = "deleted";
4074 else if (bufIsChanged(buf))
4075 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004076 /*
4077 * Check if the file contents really changed to avoid giving a
4078 * warning when only the timestamp was set (e.g., checked out of
4079 * CVS). Always warn when the buffer was changed.
4080 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004081 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4082 reason = "changed";
4083 else if (orig_mode != buf->b_orig_mode)
4084 reason = "mode";
4085 else
4086 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Only give the warning if there are no FileChangedShell
4090 * autocommands.
4091 * Avoid being called recursively by setting "busy".
4092 */
4093 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004094#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004095 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4096 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004097#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004098 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4100 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004101 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 busy = FALSE;
4103 if (n)
4104 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004105 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004107#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004108 s = get_vim_var_str(VV_FCS_CHOICE);
4109 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4110 reload = TRUE;
4111 else if (STRCMP(s, "ask") == 0)
4112 n = FALSE;
4113 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004114#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004117 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004119 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004120 {
4121 // Only give the message once.
4122 if (prev_b_mtime != -1)
4123 mesg = _("E211: File \"%s\" no longer available");
4124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
4126 {
4127 helpmesg = TRUE;
4128#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4129 can_reload = TRUE;
4130#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004131 if (reason[2] == 'n')
4132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004134 mesg2 = _("See \":help W12\" for more info.");
4135 }
4136 else if (reason[1] == 'h')
4137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 mesg2 = _("See \":help W11\" for more info.");
4140 }
4141 else if (*reason == 'm')
4142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 mesg2 = _("See \":help W16\" for more info.");
4145 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004146 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004147 // Only timestamp changed, store it to avoid a warning
4148 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004149 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151 }
4152 }
4153
4154 }
4155 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4156 && vim_fexists(buf->b_ffname))
4157 {
4158 retval = 1;
4159 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4160 buf->b_flags |= BF_NEW_W;
4161#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4162 can_reload = TRUE;
4163#endif
4164 }
4165
4166 if (mesg != NULL)
4167 {
4168 path = home_replace_save(buf, buf->b_fname);
4169 if (path != NULL)
4170 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004171 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004173 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004174 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004175#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004176 // Set warningmsg here, before the unimportant and output-specific
4177 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004178 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4181 if (can_reload)
4182 {
4183 if (*mesg2 != NUL)
4184 {
4185 STRCAT(tbuf, "\n");
4186 STRCAT(tbuf, mesg2);
4187 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4189 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004190 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 reload = TRUE;
4192 }
4193 else
4194#endif
4195 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4196 {
4197 if (*mesg2 != NUL)
4198 {
4199 STRCAT(tbuf, "; ");
4200 STRCAT(tbuf, mesg2);
4201 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004202 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 retval = 2;
4204 }
4205 else
4206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004210 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004212 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 msg_clr_eos();
4214 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004215 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004218#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004220#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004222 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004224 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 redraw_cmdline = FALSE;
4226 }
4227 }
4228 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230
4231 vim_free(path);
4232 vim_free(tbuf);
4233 }
4234 }
4235
4236 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004238 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004239 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004240#ifdef FEAT_PERSISTENT_UNDO
4241 if (buf->b_p_udf && buf->b_ffname != NULL)
4242 {
4243 char_u hash[UNDO_HASH_SIZE];
4244 buf_T *save_curbuf = curbuf;
4245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004246 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004247 curbuf = buf;
4248 u_compute_hash(hash);
4249 u_write_undo(NULL, FALSE, buf, hash);
4250 curbuf = save_curbuf;
4251 }
4252#endif
4253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004255 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004256 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004257 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4258 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004260 // restore this in case an autocommand has set it; it would break
4261 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 need_mouse_correct = save_mouse_correct;
4263#endif
4264
4265 return retval;
4266}
4267
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004268/*
4269 * Reload a buffer that is already loaded.
4270 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004271 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4272 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004273 */
4274 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004275buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004276{
4277 exarg_T ea;
4278 pos_T old_cursor;
4279 linenr_T old_topline;
4280 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004281 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004282 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004283 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004284 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004285 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004286
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004287 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004288 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004290 // We only want to read the text from the file, not reset the syntax
4291 // highlighting, clear marks, diff status, etc. Force the fileformat
4292 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004293 if (prep_exarg(&ea, buf) == OK)
4294 {
4295 old_cursor = curwin->w_cursor;
4296 old_topline = curwin->w_topline;
4297
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004298 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 // Save all the text, so that the reload can be undone.
4301 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004302 u_sync(FALSE);
4303 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4304 flags |= READ_KEEP_UNDO;
4305 }
4306
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004307 /*
4308 * To behave like when a new file is edited (matters for
4309 * BufReadPost autocommands) we first need to delete the current
4310 * buffer contents. But if reading the file fails we should keep
4311 * the old contents. Can't use memory only, the file might be
4312 * too big. Use a hidden buffer to move the buffer contents to.
4313 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004314 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004315 savebuf = NULL;
4316 else
4317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004320 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004321 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004323 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004324 curbuf = savebuf;
4325 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004326 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 curbuf = buf;
4328 curwin->w_buffer = buf;
4329 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004330 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331 || move_lines(buf, savebuf) == FAIL)
4332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004334 buf->b_fname);
4335 saved = FAIL;
4336 }
4337 }
4338
4339 if (saved == OK)
4340 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004341 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4342 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004343 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4344 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004345 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004346 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004347#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004348 if (!aborting())
4349#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004350 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004351 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 // Put the text back from the save buffer. First
4354 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004355 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004356 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004357 break;
4358 (void)move_lines(savebuf, buf);
4359 }
4360 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004364 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004365 if ((flags & READ_KEEP_UNDO) == 0)
4366 {
4367 u_blockfree(buf);
4368 u_clearall(buf);
4369 }
4370 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004373 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004374 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004375 }
4376 }
4377 vim_free(ea.cmd);
4378
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004379 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004380 wipe_buffer(savebuf, FALSE);
4381
4382#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004384 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385#endif
4386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 // Restore the topline and cursor position and check it (lines may
4388 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004389 if (old_topline > curbuf->b_ml.ml_line_count)
4390 curwin->w_topline = curbuf->b_ml.ml_line_count;
4391 else
4392 curwin->w_topline = old_topline;
4393 curwin->w_cursor = old_cursor;
4394 check_cursor();
4395 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004396 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397#ifdef FEAT_FOLDING
4398 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004399 win_T *wp;
4400 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004403 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004404 if (wp->w_buffer == curwin->w_buffer
4405 && !foldmethodIsManual(wp))
4406 foldUpdateAll(wp);
4407 }
4408#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 // If the mode didn't change and 'readonly' was set, keep the old
4410 // value; the user probably used the ":view" command. But don't
4411 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 if (orig_mode == curbuf->b_orig_mode)
4413 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004416 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004417 }
4418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004420 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004422}
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004425buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004428 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429#ifdef HAVE_ST_MODE
4430 buf->b_orig_mode = (int)st->st_mode;
4431#else
4432 buf->b_orig_mode = mch_getperm(fname);
4433#endif
4434}
4435
4436/*
4437 * Adjust the line with missing eol, used for the next write.
4438 * Used for do_filter(), when the input lines for the filter are deleted.
4439 */
4440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004441write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004444 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445}
4446
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004447// Subfuncions for readdirex()
4448#ifdef FEAT_EVAL
4449# ifdef MSWIN
4450 static char_u *
4451getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4452{
4453 stat_T st;
4454 unsigned short st_mode;
4455 DWORD flag = wfd->dwFileAttributes;
4456 WCHAR *wp;
4457
4458 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4459 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4460 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4461 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4462
4463 wp = wcsrchr(wfd->cFileName, L'.');
4464 if (wp != NULL)
4465 {
4466 if (_wcsicmp(wp, L".exe") == 0 ||
4467 _wcsicmp(wp, L".com") == 0 ||
4468 _wcsicmp(wp, L".cmd") == 0 ||
4469 _wcsicmp(wp, L".bat") == 0)
4470 st_mode |= _S_IEXEC;
4471 }
4472
4473 // Copy user bits to group/other.
4474 st_mode |= (st_mode & 0700) >> 3;
4475 st_mode |= (st_mode & 0700) >> 6;
4476
4477 st.st_mode = st_mode;
4478 return getfpermst(&st, perm);
4479}
4480
4481 static char_u *
4482getftypewfd(WIN32_FIND_DATAW *wfd)
4483{
4484 DWORD flag = wfd->dwFileAttributes;
4485 DWORD tag = wfd->dwReserved0;
4486
4487 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4488 {
4489 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4490 return (char_u*)"junction";
4491 else if (tag == IO_REPARSE_TAG_SYMLINK)
4492 {
4493 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4494 return (char_u*)"linkd";
4495 else
4496 return (char_u*)"link";
4497 }
4498 return (char_u*)"reparse"; // unknown reparse point type
4499 }
4500 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4501 return (char_u*)"dir";
4502 else
4503 return (char_u*)"file";
4504}
4505
4506 static dict_T *
4507create_readdirex_item(WIN32_FIND_DATAW *wfd)
4508{
4509 dict_T *item;
4510 char_u *p;
4511 varnumber_T size, time;
4512 char_u permbuf[] = "---------";
4513
4514 item = dict_alloc();
4515 if (item == NULL)
4516 return NULL;
4517 item->dv_refcount++;
4518
4519 p = utf16_to_enc(wfd->cFileName, NULL);
4520 if (p == NULL)
4521 goto theend;
4522 if (dict_add_string(item, "name", p) == FAIL)
4523 {
4524 vim_free(p);
4525 goto theend;
4526 }
4527 vim_free(p);
4528
4529 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4530 if (dict_add_number(item, "size", size) == FAIL)
4531 goto theend;
4532
4533 // Convert FILETIME to unix time.
4534 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4535 wfd->ftLastWriteTime.dwLowDateTime)
4536 - 116444736000000000) / 10000000;
4537 if (dict_add_number(item, "time", time) == FAIL)
4538 goto theend;
4539
4540 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4541 goto theend;
4542 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4543 goto theend;
4544
4545 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4546 goto theend;
4547 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4548 goto theend;
4549
4550 return item;
4551
4552theend:
4553 dict_unref(item);
4554 return NULL;
4555}
4556# else
4557 static dict_T *
4558create_readdirex_item(char_u *path, char_u *name)
4559{
4560 dict_T *item;
4561 char *p;
4562 size_t len;
4563 stat_T st;
4564 int ret, link = FALSE;
4565 varnumber_T size;
4566 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004567 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004568 struct passwd *pw;
4569 struct group *gr;
4570
4571 item = dict_alloc();
4572 if (item == NULL)
4573 return NULL;
4574 item->dv_refcount++;
4575
4576 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4577 p = alloc(len);
4578 if (p == NULL)
4579 goto theend;
4580 vim_snprintf(p, len, "%s/%s", path, name);
4581 ret = mch_lstat(p, &st);
4582 if (ret >= 0 && S_ISLNK(st.st_mode))
4583 {
4584 link = TRUE;
4585 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004586 if (ret < 0)
4587 q = (char_u*)"link";
4588
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004589 }
4590 vim_free(p);
4591
4592 if (dict_add_string(item, "name", name) == FAIL)
4593 goto theend;
4594
4595 if (ret >= 0)
4596 {
4597 size = (varnumber_T)st.st_size;
4598 if (S_ISDIR(st.st_mode))
4599 size = 0;
4600 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004601 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004602 size = -2;
4603 if (dict_add_number(item, "size", size) == FAIL)
4604 goto theend;
4605 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4606 goto theend;
4607
4608 if (link)
4609 {
4610 if (S_ISDIR(st.st_mode))
4611 q = (char_u*)"linkd";
4612 else
4613 q = (char_u*)"link";
4614 }
4615 else
4616 q = getftypest(&st);
4617 if (dict_add_string(item, "type", q) == FAIL)
4618 goto theend;
4619 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4620 goto theend;
4621
4622 pw = getpwuid(st.st_uid);
4623 if (pw == NULL)
4624 q = (char_u*)"";
4625 else
4626 q = (char_u*)pw->pw_name;
4627 if (dict_add_string(item, "user", q) == FAIL)
4628 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004629# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004630 gr = getgrgid(st.st_gid);
4631 if (gr == NULL)
4632 q = (char_u*)"";
4633 else
4634 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004635# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004636 if (dict_add_string(item, "group", q) == FAIL)
4637 goto theend;
4638 }
4639 else
4640 {
4641 if (dict_add_number(item, "size", -1) == FAIL)
4642 goto theend;
4643 if (dict_add_number(item, "time", -1) == FAIL)
4644 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004645 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004646 goto theend;
4647 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4648 goto theend;
4649 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4650 goto theend;
4651 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4652 goto theend;
4653 }
4654 return item;
4655
4656theend:
4657 dict_unref(item);
4658 return NULL;
4659}
4660# endif
4661
4662 static int
4663compare_readdirex_item(const void *p1, const void *p2)
4664{
4665 char_u *name1, *name2;
4666
4667 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4668 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004669 if (readdirex_sort == READDIR_SORT_BYTE)
4670 return STRCMP(name1, name2);
4671 else if (readdirex_sort == READDIR_SORT_IC)
4672 return STRICMP(name1, name2);
4673 else
4674 return STRCOLL(name1, name2);
4675}
4676
4677 static int
4678compare_readdir_item(const void *s1, const void *s2)
4679{
4680 if (readdirex_sort == READDIR_SORT_BYTE)
4681 return STRCMP(*(char **)s1, *(char **)s2);
4682 else if (readdirex_sort == READDIR_SORT_IC)
4683 return STRICMP(*(char **)s1, *(char **)s2);
4684 else
4685 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004686}
4687#endif
4688
Bram Moolenaarda440d22016-01-16 21:27:23 +01004689#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4690/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004691 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004692 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004693 * If "withattr" is TRUE, retrieve the names and their attributes.
4694 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004695 * Return OK for success, FAIL for failure.
4696 */
4697 int
4698readdir_core(
4699 garray_T *gap,
4700 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004701 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004702 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004703 int (*checkitem)(void *context, void *item),
4704 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004705{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004706 int failed = FALSE;
4707 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004708# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004709 char_u *buf;
4710 int ok;
4711 HANDLE hFind = INVALID_HANDLE_VALUE;
4712 WIN32_FIND_DATAW wfd;
4713 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004714# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004715 DIR *dirp;
4716 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004717# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004718
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004719 ga_init2(gap, (int)sizeof(void *), 20);
4720
4721# ifdef FEAT_EVAL
4722# define FREE_ITEM(item) do { \
4723 if (withattr) \
4724 dict_unref((dict_T*)item); \
4725 else \
4726 vim_free(item); \
4727 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004728
4729 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004730# else
4731# define FREE_ITEM(item) vim_free(item)
4732# endif
4733
4734# ifdef MSWIN
4735 buf = alloc(MAXPATHL);
4736 if (buf == NULL)
4737 return FAIL;
4738 STRNCPY(buf, path, MAXPATHL-5);
4739 p = buf + STRLEN(buf);
4740 MB_PTR_BACK(buf, p);
4741 if (*p == '\\' || *p == '/')
4742 *p = NUL;
4743 STRCAT(p, "\\*");
4744
4745 wn = enc_to_utf16(buf, NULL);
4746 if (wn != NULL)
4747 hFind = FindFirstFileW(wn, &wfd);
4748 ok = (hFind != INVALID_HANDLE_VALUE);
4749 if (!ok)
4750 {
4751 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004752 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004753 }
4754 else
4755 {
4756 while (ok)
4757 {
4758 int ignore;
4759 void *item;
4760 WCHAR *wp;
4761
4762 wp = wfd.cFileName;
4763 ignore = wp[0] == L'.' &&
4764 (wp[1] == NUL ||
4765 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004766 if (ignore)
4767 {
4768 ok = FindNextFileW(hFind, &wfd);
4769 continue;
4770 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004771# ifdef FEAT_EVAL
4772 if (withattr)
4773 item = (void*)create_readdirex_item(&wfd);
4774 else
4775# endif
4776 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4777 if (item == NULL)
4778 {
4779 failed = TRUE;
4780 break;
4781 }
4782
4783 if (!ignore && checkitem != NULL)
4784 {
4785 int r = checkitem(context, item);
4786
4787 if (r < 0)
4788 {
4789 FREE_ITEM(item);
4790 break;
4791 }
4792 if (r == 0)
4793 ignore = TRUE;
4794 }
4795
4796 if (!ignore)
4797 {
4798 if (ga_grow(gap, 1) == OK)
4799 ((void**)gap->ga_data)[gap->ga_len++] = item;
4800 else
4801 {
4802 failed = TRUE;
4803 FREE_ITEM(item);
4804 break;
4805 }
4806 }
4807 else
4808 FREE_ITEM(item);
4809
4810 ok = FindNextFileW(hFind, &wfd);
4811 }
4812 FindClose(hFind);
4813 }
4814
4815 vim_free(buf);
4816 vim_free(wn);
4817# else // MSWIN
4818 dirp = opendir((char *)path);
4819 if (dirp == NULL)
4820 {
4821 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004822 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004823 }
4824 else
4825 {
4826 for (;;)
4827 {
4828 int ignore;
4829 void *item;
4830
4831 dp = readdir(dirp);
4832 if (dp == NULL)
4833 break;
4834 p = (char_u *)dp->d_name;
4835
4836 ignore = p[0] == '.' &&
4837 (p[1] == NUL ||
4838 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004839 if (ignore)
4840 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004841# ifdef FEAT_EVAL
4842 if (withattr)
4843 item = (void*)create_readdirex_item(path, p);
4844 else
4845# endif
4846 item = (void*)vim_strsave(p);
4847 if (item == NULL)
4848 {
4849 failed = TRUE;
4850 break;
4851 }
4852
4853 if (!ignore && checkitem != NULL)
4854 {
4855 int r = checkitem(context, item);
4856
4857 if (r < 0)
4858 {
4859 FREE_ITEM(item);
4860 break;
4861 }
4862 if (r == 0)
4863 ignore = TRUE;
4864 }
4865
4866 if (!ignore)
4867 {
4868 if (ga_grow(gap, 1) == OK)
4869 ((void**)gap->ga_data)[gap->ga_len++] = item;
4870 else
4871 {
4872 failed = TRUE;
4873 FREE_ITEM(item);
4874 break;
4875 }
4876 }
4877 else
4878 FREE_ITEM(item);
4879 }
4880
4881 closedir(dirp);
4882 }
4883# endif // MSWIN
4884
4885# undef FREE_ITEM
4886
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004887 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004888 {
4889# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004890 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004891 if (withattr)
4892 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4893 compare_readdirex_item);
4894 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004895 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4896 compare_readdir_item);
4897# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004898 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004899# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004900 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004901
4902 return failed ? FAIL : OK;
4903}
4904
4905/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004906 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004907 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004908 */
4909 int
4910delete_recursive(char_u *name)
4911{
4912 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004913 int i;
4914 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004915 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004916
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004917 // A symbolic link to a directory itself is deleted, not the directory it
4918 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004919 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004920# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004921 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004922# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004923 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004924# endif
4925 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004926 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004927 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004928 if (exp == NULL)
4929 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004930 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004931 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004932 for (i = 0; i < ga.ga_len; ++i)
4933 {
4934 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4935 ((char_u **)ga.ga_data)[i]);
4936 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004937 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004938 }
4939 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004940 }
4941 else
4942 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004943 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004944 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004945 }
4946 else
4947 result = mch_remove(name) == 0 ? 0 : -1;
4948
4949 return result;
4950}
4951#endif
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004954static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004956# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4957/*
4958 * Open temporary directory and take file lock to prevent
4959 * to be auto-cleaned.
4960 */
4961 static void
4962vim_opentempdir(void)
4963{
4964 DIR *dp = NULL;
4965
4966 if (vim_tempdir_dp != NULL)
4967 return;
4968
4969 dp = opendir((const char*)vim_tempdir);
4970
4971 if (dp != NULL)
4972 {
4973 vim_tempdir_dp = dp;
4974 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4975 }
4976}
4977
4978/*
4979 * Close temporary directory - it automatically release file lock.
4980 */
4981 static void
4982vim_closetempdir(void)
4983{
4984 if (vim_tempdir_dp != NULL)
4985 {
4986 closedir(vim_tempdir_dp);
4987 vim_tempdir_dp = NULL;
4988 }
4989}
4990# endif
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992/*
4993 * Delete the temp directory and all files it contains.
4994 */
4995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (vim_tempdir != NULL)
4999 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005000# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5001 vim_closetempdir();
5002# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005004 gettail(vim_tempdir)[-1] = NUL;
5005 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005006 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
5010/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005011 * Directory "tempdir" was created. Expand this name to a full path and put
5012 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5013 * "tempdir" must be no longer than MAXPATHL.
5014 */
5015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005016vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005017{
5018 char_u *buf;
5019
Bram Moolenaar964b3742019-05-24 18:54:09 +02005020 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005021 if (buf != NULL)
5022 {
5023 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5024 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005025 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005026 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005027# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5028 vim_opentempdir();
5029# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005030 vim_free(buf);
5031 }
5032}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005033#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005034
5035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 * vim_tempname(): Return a unique name that can be used for a temp file.
5037 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005038 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5039 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * The returned pointer is to allocated memory.
5042 * The returned pointer is NULL if no valid name was found.
5043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005047 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
5049#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005050 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005051#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005052 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#else
5054 char_u itmp[TEMPNAMELEN];
5055#endif
5056
5057#ifdef TEMPDIRNAMES
5058 static char *(tempdirs[]) = {TEMPDIRNAMES};
5059 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005061 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062# endif
5063
5064 /*
5065 * This will create a directory for private use by this instance of Vim.
5066 * This is done once, and the same directory is used for all temp files.
5067 * This method avoids security problems because of symlink attacks et al.
5068 * It's also a bit faster, because we only need to check for an existing
5069 * file when creating the directory and not for each temp file.
5070 */
5071 if (vim_tempdir == NULL)
5072 {
5073 /*
5074 * Try the entries in TEMPDIRNAMES to create the temp directory.
5075 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00005076 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005078# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005079 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005080 long nr;
5081 long off;
5082# endif
5083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // Expand $TMP, leave room for "/v1100000/999999999".
5085 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005087 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005090 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaareaf03392009-11-17 11:08:52 +00005092# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005093 {
5094# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005095 // Make sure the umask doesn't remove the executable bit.
5096 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005097 mode_t umask_save = umask(077);
5098# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005100 STRCAT(itmp, "vXXXXXX");
5101 if (mkdtemp((char *)itmp) != NULL)
5102 vim_settempdir(itmp);
5103# if defined(UNIX) || defined(VMS)
5104 (void)umask(umask_save);
5105# endif
5106 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005107# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 // Get an arbitrary number of up to 6 digits. When it's
5109 // unlikely that it already exists it will be faster,
5110 // otherwise it doesn't matter. The use of mkdir() avoids any
5111 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005113 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 // Try up to 10000 different values until we find a name that
5116 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 for (off = 0; off < 10000L; ++off)
5118 {
5119 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005120# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaareaf03392009-11-17 11:08:52 +00005124 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5125# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 // If mkdir() does not set errno to EEXIST, check for
5127 // existing file here. There is a race condition then,
5128 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (mch_stat((char *)itmp, &st) >= 0)
5130 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005131# endif
5132# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 // Make sure the umask doesn't remove the executable bit.
5134 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005138# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (r == 0)
5142 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005143 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 break;
5145 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005146# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 // If the mkdir() didn't fail because the file/dir exists,
5148 // we probably can't create any dir here, try another
5149 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (vim_tempdir != NULL)
5156 break;
5157 }
5158 }
5159 }
5160
5161 if (vim_tempdir != NULL)
5162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 // There is no need to check if the file exists, because we own the
5164 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5166 return vim_strsave(itmp);
5167 }
5168
5169 return NULL;
5170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
Bram Moolenaar4f974752019-02-17 17:44:42 +01005173# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005174 WCHAR wszTempFile[_MAX_PATH + 1];
5175 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005176 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 char_u *retval;
5178 char_u *p;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005179 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005181 wcscpy(itmp, L"");
5182 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005183 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005184 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005185 wszTempFile[1] = L'\\';
5186 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005187 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005188 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005189
5190 // randomize the name to avoid collisions
5191 i = mch_get_pid() + extra_char;
5192 buf4[1] = chartab[i % 36];
5193 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005194 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005196 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005197 // GetTempFileName() will create the file, we don't want that
5198 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005200 // Backslashes in a temp file name cause problems when filtering with
5201 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5202 // didn't set 'shellslash'.
5203 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (*p_shcf == '-' || p_ssl)
5205 for (p = retval; *p; ++p)
5206 if (*p == '\\')
5207 *p = '/';
5208 return retval;
5209
Bram Moolenaar4f974752019-02-17 17:44:42 +01005210# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005213 char_u *p;
5214
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005215 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005216 p = tmpnam((char *)itmp);
5217 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 return NULL;
5219# else
5220 char_u *p;
5221
5222# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 // mktemp() is not working on VMS. It seems to be
5224 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 sprintf((char *)itmp, "VIM%c", extra_char);
5226 p = (char_u *)tempnam("tmp:", (char *)itmp);
5227 if (p != NULL)
5228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 // VMS will use '.LIS' if we don't explicitly specify an extension,
5230 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 STRCPY(itmp, p);
5232 STRCAT(itmp, ".txt");
5233 free(p);
5234 }
5235 else
5236 return NULL;
5237# else
5238 STRCPY(itmp, TEMPNAME);
5239 if ((p = vim_strchr(itmp, '?')) != NULL)
5240 *p = extra_char;
5241 if (mktemp((char *)itmp) == NULL)
5242 return NULL;
5243# endif
5244# endif
5245
5246 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005247# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249}
5250
5251#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5252/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005253 * Convert all backslashes in fname to forward slashes in-place, unless when
5254 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 */
5256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 char_u *p;
5260
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005261 if (path_with_url(fname))
5262 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005264 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005265 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005267 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 *p = '/';
5269}
5270#endif
5271
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005272/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005273 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5274 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5275 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 * Used for autocommands and 'wildignore'.
5277 * Returns TRUE if there is a match, FALSE otherwise.
5278 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005279 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005280match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005281 char_u *pattern, // pattern to match with
5282 regprog_T **prog, // pre-compiled regprog or NULL
5283 char_u *fname, // full path of file name
5284 char_u *sfname, // short file name or NULL
5285 char_u *tail, // tail of path
5286 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 regmatch_T regmatch;
5289 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005291 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005292 if (prog != NULL)
5293 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005295 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 /*
5298 * Try for a match with the pattern with:
5299 * 1. the full file name, when the pattern has a '/'.
5300 * 2. the short file name, when the pattern has a '/'.
5301 * 3. the tail of the file name, when the pattern has no '/'.
5302 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005303 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 && ((allow_dirs
5305 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5306 || (sfname != NULL
5307 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005308 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 result = TRUE;
5310
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005311 if (prog != NULL)
5312 *prog = regmatch.regprog;
5313 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005314 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return result;
5316}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318#if defined(FEAT_WILDIGN) || defined(PROTO)
5319/*
5320 * Return TRUE if a file matches with a pattern in "list".
5321 * "list" is a comma-separated list of patterns, like 'wildignore'.
5322 * "sfname" is the short file name or NULL, "ffname" the long file name.
5323 */
5324 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005325match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
5327 char_u buf[100];
5328 char_u *tail;
5329 char_u *regpat;
5330 char allow_dirs;
5331 int match;
5332 char_u *p;
5333
5334 tail = gettail(sfname);
5335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 p = list;
5338 while (*p)
5339 {
5340 copy_option_part(&p, buf, 100, ",");
5341 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5342 if (regpat == NULL)
5343 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005344 match = match_file_pat(regpat, NULL, ffname, sfname,
5345 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 vim_free(regpat);
5347 if (match)
5348 return TRUE;
5349 }
5350 return FALSE;
5351}
5352#endif
5353
5354/*
5355 * Convert the given pattern "pat" which has shell style wildcards in it, into
5356 * a regular expression, and return the result in allocated memory. If there
5357 * is a directory path separator to be matched, then TRUE is put in
5358 * allow_dirs, otherwise FALSE is put there -- webb.
5359 * Handle backslashes before special characters, like "\*" and "\ ".
5360 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 * Returns NULL when out of memory.
5362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364file_pat_to_reg_pat(
5365 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005366 char_u *pat_end, // first char after pattern or NULL
5367 char *allow_dirs, // Result passed back out in here
5368 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 char_u *endp;
5372 char_u *reg_pat;
5373 char_u *p;
5374 int i;
5375 int nested = 0;
5376 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
5378 if (allow_dirs != NULL)
5379 *allow_dirs = FALSE;
5380 if (pat_end == NULL)
5381 pat_end = pat + STRLEN(pat);
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 for (p = pat; p < pat_end; p++)
5384 {
5385 switch (*p)
5386 {
5387 case '*':
5388 case '.':
5389 case ',':
5390 case '{':
5391 case '}':
5392 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005393 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 break;
5395#ifdef BACKSLASH_IN_FILENAME
5396 case '\\':
5397 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005398 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 break;
5400#endif
5401 default:
5402 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005403 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 ++p;
5406 ++size;
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 break;
5409 }
5410 }
5411 reg_pat = alloc(size + 1);
5412 if (reg_pat == NULL)
5413 return NULL;
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 if (pat[0] == '*')
5418 while (pat[0] == '*' && pat < pat_end - 1)
5419 pat++;
5420 else
5421 reg_pat[i++] = '^';
5422 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005423 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 while (endp - pat > 0 && *endp == '*')
5426 endp--;
5427 add_dollar = FALSE;
5428 }
5429 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5430 {
5431 switch (*p)
5432 {
5433 case '*':
5434 reg_pat[i++] = '.';
5435 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005437 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 break;
5439 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 case '~':
5441 reg_pat[i++] = '\\';
5442 reg_pat[i++] = *p;
5443 break;
5444 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 reg_pat[i++] = '.';
5446 break;
5447 case '\\':
5448 if (p[1] == NUL)
5449 break;
5450#ifdef BACKSLASH_IN_FILENAME
5451 if (!no_bslash)
5452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 // translate:
5454 // "\x" to "\\x" e.g., "dir\file"
5455 // "\*" to "\\.*" e.g., "dir\*.c"
5456 // "\?" to "\\." e.g., "dir\??.c"
5457 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5459 && p[1] != '+')
5460 {
5461 reg_pat[i++] = '[';
5462 reg_pat[i++] = '\\';
5463 reg_pat[i++] = '/';
5464 reg_pat[i++] = ']';
5465 if (allow_dirs != NULL)
5466 *allow_dirs = TRUE;
5467 break;
5468 }
5469 }
5470#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 // Undo escaping from ExpandEscape():
5472 // foo\?bar -> foo?bar
5473 // foo\%bar -> foo%bar
5474 // foo\,bar -> foo,bar
5475 // foo\ bar -> foo bar
5476 // Don't unescape \, * and others that are also special in a
5477 // regexp.
5478 // An escaped { must be unescaped since we use magic not
5479 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (*++p == '?'
5481#ifdef BACKSLASH_IN_FILENAME
5482 && no_bslash
5483#endif
5484 )
5485 reg_pat[i++] = '?';
5486 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005487 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005488 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005489 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005490 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5491 {
5492 reg_pat[i++] = '\\';
5493 reg_pat[i++] = '{';
5494 p += 2;
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 else
5497 {
5498 if (allow_dirs != NULL && vim_ispathsep(*p)
5499#ifdef BACKSLASH_IN_FILENAME
5500 && (!no_bslash || *p != '\\')
5501#endif
5502 )
5503 *allow_dirs = TRUE;
5504 reg_pat[i++] = '\\';
5505 reg_pat[i++] = *p;
5506 }
5507 break;
5508#ifdef BACKSLASH_IN_FILENAME
5509 case '/':
5510 reg_pat[i++] = '[';
5511 reg_pat[i++] = '\\';
5512 reg_pat[i++] = '/';
5513 reg_pat[i++] = ']';
5514 if (allow_dirs != NULL)
5515 *allow_dirs = TRUE;
5516 break;
5517#endif
5518 case '{':
5519 reg_pat[i++] = '\\';
5520 reg_pat[i++] = '(';
5521 nested++;
5522 break;
5523 case '}':
5524 reg_pat[i++] = '\\';
5525 reg_pat[i++] = ')';
5526 --nested;
5527 break;
5528 case ',':
5529 if (nested)
5530 {
5531 reg_pat[i++] = '\\';
5532 reg_pat[i++] = '|';
5533 }
5534 else
5535 reg_pat[i++] = ',';
5536 break;
5537 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005538 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005540 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 *allow_dirs = TRUE;
5542 reg_pat[i++] = *p;
5543 break;
5544 }
5545 }
5546 if (add_dollar)
5547 reg_pat[i++] = '$';
5548 reg_pat[i] = NUL;
5549 if (nested != 0)
5550 {
5551 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005554 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005555 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 return reg_pat;
5558}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005559
5560#if defined(EINTR) || defined(PROTO)
5561/*
5562 * Version of read() that retries when interrupted by EINTR (possibly
5563 * by a SIGWINCH).
5564 */
5565 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005567{
5568 long ret;
5569
5570 for (;;)
5571 {
5572 ret = vim_read(fd, buf, bufsize);
5573 if (ret >= 0 || errno != EINTR)
5574 break;
5575 }
5576 return ret;
5577}
5578
5579/*
5580 * Version of write() that retries when interrupted by EINTR (possibly
5581 * by a SIGWINCH).
5582 */
5583 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005585{
5586 long ret = 0;
5587 long wlen;
5588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005589 // Repeat the write() so long it didn't fail, other than being interrupted
5590 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005591 while (ret < (long)bufsize)
5592 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005593 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005594 if (wlen < 0)
5595 {
5596 if (errno != EINTR)
5597 break;
5598 }
5599 else
5600 ret += wlen;
5601 }
5602 return ret;
5603}
5604#endif