blob: e80009fbe1ef7dba19501edb9503745486ce1d9f [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 Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
19
Bram Moolenaar217e1b82019-12-01 21:41:28 +010020// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000021#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaarf077db22019-08-13 00:18:24 +020023static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010024#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020028static 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 +000029#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010030static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010031static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000032static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000033
Bram Moolenaar473952e2019-09-28 16:30:04 +020034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010035filemess(
36 buf_T *buf,
37 char_u *name,
38 char_u *s,
39 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000040{
41 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020042 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44 if (msg_silent != 0)
45 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010046 msg_add_fname(buf, name); // put file name in IObuff with quotes
47 // If it's extremely long, truncate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 if (STRLEN(IObuff) > IOSIZE - 80)
49 IObuff[IOSIZE - 80] = NUL;
50 STRCAT(IObuff, s);
51 /*
52 * For the first message may have to start a new line.
53 * For further ones overwrite the previous one, reset msg_scroll before
54 * calling filemess().
55 */
56 msg_scroll_save = msg_scroll;
57 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
58 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010059 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 check_for_delay(FALSE);
61 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020062 if (prev_msg_col != 0 && msg_col == 0)
63 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 msg_scroll = msg_scroll_save;
65 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010066 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
68 msg_clr_eos();
69 out_flush();
70 msg_scrolled_ign = FALSE;
71}
72
73/*
74 * Read lines from file "fname" into the buffer after line "from".
75 *
76 * 1. We allocate blocks with lalloc, as big as possible.
77 * 2. Each block is filled with characters from the file with a single read().
78 * 3. The lines are inserted in the buffer with ml_append().
79 *
80 * (caller must check that fname != NULL, unless READ_STDIN is used)
81 *
82 * "lines_to_skip" is the number of lines that must be skipped
83 * "lines_to_read" is the number of lines that are appended
84 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
85 *
86 * flags:
87 * READ_NEW starting to edit a new buffer
88 * READ_FILTER reading filter output
89 * READ_STDIN read from stdin instead of a file
90 * READ_BUFFER read from curbuf instead of a file (converting after reading
91 * stdin)
92 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +020093 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020094 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +010096 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
98 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010099readfile(
100 char_u *fname,
101 char_u *sfname,
102 linenr_T from,
103 linenr_T lines_to_skip,
104 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100105 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100106 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107{
108 int fd = 0;
109 int newfile = (flags & READ_NEW);
110 int check_readonly;
111 int filtering = (flags & READ_FILTER);
112 int read_stdin = (flags & READ_STDIN);
113 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000115 int set_options = newfile || read_buffer
116 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100117 linenr_T read_buf_lnum = 1; // next line to read from curbuf
118 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 char_u c;
120 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100121 char_u *ptr = NULL; // pointer into read buffer
122 char_u *buffer = NULL; // read buffer
123 char_u *new_buffer = NULL; // init to shut up gcc
124 char_u *line_start = NULL; // init to shut up gcc
125 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 colnr_T len;
127 long size = 0;
128 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200129 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 int skip_read = FALSE;
131#ifdef FEAT_CRYPT
132 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200133 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200135#ifdef FEAT_PERSISTENT_UNDO
136 context_sha256_T sha_ctx;
137 int read_undo_file = FALSE;
138#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100139 int split = 0; // number of split lines
140#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100142 int error = FALSE; // errors encountered
143 int ff_error = EOL_UNKNOWN; // file format with errors
144 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#ifdef UNIX
146 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100147 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#else
149 int perm;
150#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100151 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200153 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 int file_readonly;
155 linenr_T skip_count = 0;
156 linenr_T read_count = 0;
157 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100158 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
159 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100160 int try_mac;
161 int try_dos;
162 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100165 linenr_T conv_error = 0; // line nr with conversion error
166 linenr_T illegal_byte = 0; // line nr with illegal byte
167 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
168 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000169 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100170 // BAD_KEEP, BAD_DROP or character to
171 // replace with
172 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100174 char_u *fenc; // fileencoding to use
175 int fenc_alloced; // fenc_next is in allocated memory
176 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 int advance_fenc = FALSE;
178 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100179#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100180 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100181# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100182 int did_iconv = FALSE; // TRUE when iconv() failed and trying
183 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100185#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 int converted = FALSE; // TRUE if conversion done
187 int notconverted = FALSE; // TRUE if conversion wanted but it
188 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100190 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100191 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200192 buf_T *old_curbuf;
193 char_u *old_b_ffname;
194 char_u *old_b_fname;
195 int using_b_ffname;
196 int using_b_fname;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200197
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100198 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200199
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100200 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202 /*
203 * If there is no file name yet, use the one for the read file.
204 * BF_NOTEDITED is set to reflect this.
205 * Don't do this for a read from a filter.
206 * Only do this when 'cpoptions' contains the 'f' flag.
207 */
208 if (curbuf->b_ffname == NULL
209 && !filtering
210 && fname != NULL
211 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
212 && !(flags & READ_DUMMY))
213 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000214 if (set_rw_fname(fname, sfname) == FAIL)
215 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 }
217
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100218 // Remember the initial values of curbuf, curbuf->b_ffname and
219 // curbuf->b_fname to detect whether they are altered as a result of
220 // executing nasty autocommands. Also check if "fname" and "sfname"
221 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200222 old_curbuf = curbuf;
223 old_b_ffname = curbuf->b_ffname;
224 old_b_fname = curbuf->b_fname;
225 using_b_ffname = (fname == curbuf->b_ffname)
226 || (sfname == curbuf->b_ffname);
227 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200228
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100229 // After reading a file the cursor line changes but we don't want to
230 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000231 ex_no_reprint = TRUE;
232
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100233 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000234 need_fileinfo = FALSE;
235
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 /*
237 * For Unix: Use the short file name whenever possible.
238 * Avoids problems with networks and when directory names are changed.
239 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
240 * another directory, which we don't detect.
241 */
242 if (sfname == NULL)
243 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200244#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 fname = sfname;
246#endif
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 /*
249 * The BufReadCmd and FileReadCmd events intercept the reading process by
250 * executing the associated commands instead.
251 */
252 if (!filtering && !read_stdin && !read_buffer)
253 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100254 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100256 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
258 curbuf->b_op_start.col = 0;
259
260 if (newfile)
261 {
262 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
263 FALSE, curbuf, eap))
264#ifdef FEAT_EVAL
265 return aborting() ? FAIL : OK;
266#else
267 return OK;
268#endif
269 }
270 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
271 FALSE, NULL, eap))
272#ifdef FEAT_EVAL
273 return aborting() ? FAIL : OK;
274#else
275 return OK;
276#endif
277
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100278 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100282 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100284 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
286 /*
287 * If the name ends in a path separator, we can't open it. Check here,
288 * because reading the file may actually work, but then creating the swap
289 * file may destroy it! Reported on MS-DOS and Win 95.
290 * If the name is too long we might crash further on, quit here.
291 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000292 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000294 p = fname + STRLEN(fname);
295 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000296 {
297 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
298 msg_end();
299 msg_scroll = msg_save;
300 return FAIL;
301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200304 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200306#ifdef UNIX
307 /*
308 * On Unix it is possible to read a directory, so we have to
309 * check for it before the mch_open().
310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100312 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
313 && !S_ISFIFO(perm) // ... or fifo
314 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000315# ifdef OPEN_CHR_FILES
316 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100317 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 )
320 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100321 int retval = FAIL;
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100326 retval = NOTDONE;
327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 else
329 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
330 msg_end();
331 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100332 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200334#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100335#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000336 /*
337 * MS-Windows allows opening a device, but we will probably get stuck
338 * trying to read it.
339 */
340 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
341 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000342 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000343 msg_end();
344 msg_scroll = msg_save;
345 return FAIL;
346 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000347#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200348 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000349
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100350 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200351 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 /*
354 * When opening a new file we take the readonly flag from the file.
355 * Default is r/w, can be set to r/o below.
356 * Don't reset it when in readonly mode
357 * Only set/reset b_p_ro when BF_CHECK_RO is set.
358 */
359 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000360 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 curbuf->b_p_ro = FALSE;
362
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200363 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100365 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (mch_stat((char *)fname, &st) >= 0)
367 {
368 buf_store_time(curbuf, &st, fname);
369 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef UNIX
371 /*
372 * Use the protection bits of the original file for the swap file.
373 * This makes it possible for others to read the name of the
374 * edited file from the swapfile, but only if they can read the
375 * edited file.
376 * Remove the "write" and "execute" bits for group and others
377 * (they must not write the swapfile).
378 * Add the "read" and "write" bits for the user, otherwise we may
379 * not be able to write to the file ourselves.
380 * Setting the bits is done below, after creating the swap file.
381 */
382 swap_mode = (st.st_mode & 0644) | 0600;
383#endif
384#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385 // Get the FSSpec on MacOS
386 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
388#endif
389#ifdef VMS
390 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000391 curbuf->b_fab_rat = st.st_fab_rat;
392 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394 }
395 else
396 {
397 curbuf->b_mtime = 0;
398 curbuf->b_mtime_read = 0;
399 curbuf->b_orig_size = 0;
400 curbuf->b_orig_mode = 0;
401 }
402
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 // Reset the "new file" flag. It will be set again below when the
404 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
406 }
407
408/*
409 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100410 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 */
412 file_readonly = FALSE;
413 if (read_stdin)
414 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100415#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 setmode(0, O_BINARY);
418#endif
419 }
420 else if (!read_buffer)
421 {
422#ifdef USE_MCH_ACCESS
423 if (
424# ifdef UNIX
425 !(perm & 0222) ||
426# endif
427 mch_access((char *)fname, W_OK))
428 file_readonly = TRUE;
429 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
430#else
431 if (!newfile
432 || readonlymode
433 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
434 {
435 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
438 }
439#endif
440 }
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
444#ifndef UNIX
445 int isdir_f;
446#endif
447 msg_scroll = msg_save;
448#ifndef UNIX
449 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100450 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 */
452 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100453 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (isdir_f)
455 {
456 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100457 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
459 else
460#endif
461 if (newfile)
462 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200463 if (perm < 0
464#ifdef ENOENT
465 && errno == ENOENT
466#endif
467 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {
469 /*
470 * Set the 'new-file' flag, so that when the file has
471 * been created by someone else, a ":w" will complain.
472 */
473 curbuf->b_flags |= BF_NEW;
474
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 // Create a swap file now, so that other Vims are warned
476 // that we are editing this file. Don't do this for a
477 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478#ifdef FEAT_QUICKFIX
479 if (!bt_dontwrite(curbuf))
480#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000484 if (curbuf != old_curbuf
485 || (using_b_ffname
486 && (old_b_ffname != curbuf->b_ffname))
487 || (using_b_fname
488 && (old_b_fname != curbuf->b_fname)))
489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100490 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000491 return FAIL;
492 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000493 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000494 if (dir_of_file_exists(fname))
495 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
496 else
497 filemess(curbuf, sfname,
498 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100500 // Even though this is a new file, it might have been
501 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 check_marks_read();
503#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200505 if (eap != NULL)
506 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
508 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100509 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 save_file_ff(curbuf);
511
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100512#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100513 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 return FAIL;
515#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100516 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000520 filemess(curbuf, sfname, (char_u *)(
521# ifdef EFBIG
522 (errno == EFBIG) ? _("[File too big]") :
523# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200524# ifdef EOVERFLOW
525 (errno == EOVERFLOW) ? _("[File too big]") :
526# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000527 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100528 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530 }
531
532 return FAIL;
533 }
534
535 /*
536 * Only set the 'ro' flag for readonly files the first time they are
537 * loaded. Help files always get readonly mode
538 */
539 if ((check_readonly && file_readonly) || curbuf->b_help)
540 curbuf->b_p_ro = TRUE;
541
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000542 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 // Don't change 'eol' if reading from buffer as it will already be
545 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000546 if (!read_buffer)
547 {
548 curbuf->b_p_eol = TRUE;
549 curbuf->b_start_eol = TRUE;
550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000552 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100555 // Create a swap file now, so that other Vims are warned that we are
556 // editing this file.
557 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#ifdef FEAT_QUICKFIX
559 if (!bt_dontwrite(curbuf))
560#endif
561 {
562 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000563 if (!read_stdin && (curbuf != old_curbuf
564 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
565 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000568 if (!read_buffer)
569 close(fd);
570 return FAIL;
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100573 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000574 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
575 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100576 {
577 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
578
579 /*
580 * If the group-read bit is set but not the world-read bit, then
581 * the group must be equal to the group of the original file. If
582 * we can't make that happen then reset the group-read bit. This
583 * avoids making the swap file readable to more users when the
584 * primary group of the user is too permissive.
585 */
586 if ((swap_mode & 044) == 040)
587 {
588 stat_T swap_st;
589
590 if (mch_stat((char *)swap_fname, &swap_st) >= 0
591 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200592# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100593 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200594 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200595# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200596 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100597 swap_mode &= 0600;
598 }
599
600 (void)mch_setperm(swap_fname, (long)swap_mode);
601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603 }
604
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200605 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (swap_exists_action == SEA_QUIT)
607 {
608 if (!read_buffer && !read_stdin)
609 close(fd);
610 return FAIL;
611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100613 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 /*
616 * Set '[ mark to the line above where the lines go (line 1 if zero).
617 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100618 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
620 curbuf->b_op_start.col = 0;
621
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100622 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
623 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
624 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (!read_buffer)
627 {
628 int m = msg_scroll;
629 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 /*
632 * The file must be closed again, the autocommands may want to change
633 * the file before reading it.
634 */
635 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100636 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 /*
639 * The output from the autocommands should not overwrite anything and
640 * should not be overwritten: Set msg_scroll, restore its value if no
641 * output was done.
642 */
643 msg_scroll = TRUE;
644 if (filtering)
645 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
646 FALSE, curbuf, eap);
647 else if (read_stdin)
648 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
649 FALSE, curbuf, eap);
650 else if (newfile)
651 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
652 FALSE, curbuf, eap);
653 else
654 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
655 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100657 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
658 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
659 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100660 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (msg_scrolled == n)
663 msg_scroll = m;
664
665#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
668 --no_wait_return;
669 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 return FAIL;
672 }
673#endif
674 /*
675 * Don't allow the autocommands to change the current buffer.
676 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000677 *
678 * Don't allow the autocommands to change the buffer name either
679 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
681 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000682 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
683 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
685 {
686 --no_wait_return;
687 msg_scroll = msg_save;
688 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100689 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100691 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100692 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return FAIL;
694 }
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
699
700 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
701 {
702 /*
703 * Show the user that we are busy reading the input. Sometimes this
704 * may take a while. When reading from stdin another program may
705 * still be running, don't move the cursor to the last line, unless
706 * always using the GUI.
707 */
708 if (read_stdin)
709 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100710 if (!is_not_a_term())
711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200713# ifdef VIMDLL
714 if (!gui.in_use)
715# endif
716 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#endif
718#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100719 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100720 if (gui.in_use && !gui.dying && !gui.starting)
721 {
722 p = (char_u *)_("Reading from stdin...");
723 gui_write(p, (int)STRLEN(p));
724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728 else if (!read_buffer)
729 filemess(curbuf, sfname, (char_u *)"", 0);
730 }
731
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100732 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 /*
735 * Set linecnt now, before the "retry" caused by a wrong guess for
736 * fileformat, and after the autocommands, which may change them.
737 */
738 linecnt = curbuf->b_ml.ml_line_count;
739
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100740 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000741 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000742 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000743 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000744 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000745 curbuf->b_bad_char = eap->bad_char;
746 }
747 else
748 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000751 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 */
753 if (eap != NULL && eap->force_enc != 0)
754 {
755 fenc = enc_canonize(eap->cmd + eap->force_enc);
756 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000757 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 else if (curbuf->b_p_bin)
760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100761 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 fenc_alloced = FALSE;
763 }
764 else if (curbuf->b_help)
765 {
766 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000767 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // Help files are either utf-8 or latin1. Try utf-8 first, if this
770 // fails it must be latin1.
771 // Always do this when 'encoding' is "utf-8". Otherwise only do
772 // this when needed to avoid [converted] remarks all the time.
773 // It is needed when the first line contains non-ASCII characters.
774 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 fenc = (char_u *)"latin1";
776 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000777 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000779 fc = fname[STRLEN(fname) - 1];
780 if (TOLOWER_ASC(fc) == 'x')
781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // Read the first line (and a bit more). Immediately rewind to
783 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100784 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200785 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000786 for (p = firstline; p < firstline + len; ++p)
787 if (*p >= 0x80)
788 {
789 c = TRUE;
790 break;
791 }
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794
795 if (c)
796 {
797 fenc_next = fenc;
798 fenc = (char_u *)"utf-8";
799
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // When the file is utf-8 but a character doesn't fit in
801 // 'encoding' don't retry. In help text editing utf-8 bytes
802 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000803 if (!enc_utf8)
804 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 fenc_alloced = FALSE;
807 }
808 else if (*p_fencs == NUL)
809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 fenc_alloced = FALSE;
812 }
813 else
814 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200816 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 /*
820 * Jump back here to retry reading the file in different ways.
821 * Reasons to retry:
822 * - encoding conversion failed: try another one from "fenc_next"
823 * - BOM detected and fenc was set, need to setup conversion
824 * - "fileformat" check failed: try another
825 *
826 * Variables set for special retry actions:
827 * "file_rewind" Rewind the file to start reading it again.
828 * "advance_fenc" Advance "fenc" using "fenc_next".
829 * "skip_read" Re-use already read bytes (BOM detected).
830 * "did_iconv" iconv() conversion failed, try 'charconvert'.
831 * "keep_fileformat" Don't reset "fileformat".
832 *
833 * Other status indicators:
834 * "tmpname" When != NULL did conversion with 'charconvert'.
835 * Output file has to be deleted afterwards.
836 * "iconv_fd" When != -1 did conversion with iconv().
837 */
838retry:
839
840 if (file_rewind)
841 {
842 if (read_buffer)
843 {
844 read_buf_lnum = 1;
845 read_buf_col = 0;
846 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200847 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 error = TRUE;
851 goto failed;
852 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 while (lnum > from)
855 ml_delete(lnum--, FALSE);
856 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000857 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000860 curbuf->b_start_bomb = FALSE;
861 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000862 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864
865 /*
866 * When retrying with another "fenc" and the first time "fileformat"
867 * will be reset.
868 */
869 if (keep_fileformat)
870 keep_fileformat = FALSE;
871 else
872 {
873 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000876 try_unix = try_dos = try_mac = FALSE;
877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100879 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100881 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885
Bram Moolenaar13505972019-01-24 15:04:48 +0100886#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (iconv_fd != (iconv_t)-1)
888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100889 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 iconv_close(iconv_fd);
891 iconv_fd = (iconv_t)-1;
892 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895 if (advance_fenc)
896 {
897 /*
898 * Try the next entry in 'fileencodings'.
899 */
900 advance_fenc = FALSE;
901
902 if (eap != NULL && eap->force_enc != 0)
903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100904 // Conversion given with "++cc=" wasn't possible, read
905 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000907 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (fenc_alloced)
909 vim_free(fenc);
910 fenc = (char_u *)"";
911 fenc_alloced = FALSE;
912 }
913 else
914 {
915 if (fenc_alloced)
916 vim_free(fenc);
917 if (fenc_next != NULL)
918 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200919 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 else
922 {
923 fenc = (char_u *)"";
924 fenc_alloced = FALSE;
925 }
926 }
927 if (tmpname != NULL)
928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100930 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
932 }
933
934 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000935 * Conversion may be required when the encoding of the file is different
936 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 */
938 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000939 converted = need_conversion(fenc);
940 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // "ucs-bom" means we need to check the first bytes of the file
944 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (STRCMP(fenc, ENC_UCSBOM) == 0)
946 fio_flags = FIO_UCSBOM;
947
948 /*
949 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
950 * done. This is handled below after read(). Prepare the
951 * fio_flags to avoid having to parse the string each time.
952 * Also check for Unicode to Latin1 conversion, because iconv()
953 * appears not to handle this correctly. This works just like
954 * conversion to UTF-8 except how the resulting character is put in
955 * the buffer.
956 */
957 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
958 fio_flags = get_fio_flags(fenc);
959
Bram Moolenaar4f974752019-02-17 17:44:42 +0100960#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 /*
962 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
963 * is handled with MultiByteToWideChar().
964 */
965 if (fio_flags == 0)
966 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaar13505972019-01-24 15:04:48 +0100969#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100970 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (fio_flags == 0)
972 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar13505972019-01-24 15:04:48 +0100975#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * Try using iconv() if we can't convert internally.
978 */
979 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100980# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +0100982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 )
984 iconv_fd = (iconv_t)my_iconv_open(
985 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar13505972019-01-24 15:04:48 +0100988#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 /*
990 * Use the 'charconvert' expression when conversion is required
991 * and we can't do it internally or with iconv().
992 */
993 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200994 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +0100995# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 )
999 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001000# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001002# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 // Skip conversion when it's already done (retry for wrong
1004 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (tmpname == NULL)
1006 {
1007 tmpname = readfile_charconvert(fname, fenc, &fd);
1008 if (tmpname == NULL)
1009 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 advance_fenc = TRUE;
1012 if (fd < 0)
1013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001014 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001015 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 error = TRUE;
1017 goto failed;
1018 }
1019 goto retry;
1020 }
1021 }
1022 }
1023 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
1026 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001027#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 )
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // Conversion wanted but we can't.
1033 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 advance_fenc = TRUE;
1035 goto retry;
1036 }
1037 }
1038 }
1039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // Set "can_retry" when it's possible to rewind the file and try with
1041 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1042 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001043 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045 if (!skip_read)
1046 {
1047 linerest = 0;
1048 filesize = 0;
1049 skip_count = lines_to_skip;
1050 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001052#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001053 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1054 && curbuf->b_ffname != NULL
1055 && curbuf->b_p_udf
1056 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001057 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001058 && !read_stdin
1059 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001060 if (read_undo_file)
1061 sha256_start(&sha_ctx);
1062#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001063#ifdef FEAT_CRYPT
1064 if (curbuf->b_cryptstate != NULL)
1065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // Need to free the state, but keep the key, don't want to ask for
1067 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001068 crypt_free_state(curbuf->b_cryptstate);
1069 curbuf->b_cryptstate = NULL;
1070 }
1071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073
1074 while (!error && !got_int)
1075 {
1076 /*
1077 * We allocate as much space for the file as we can get, plus
1078 * space for the old line plus room for one terminating NUL.
1079 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001080 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001082 if (!skip_read)
1083 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001084#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001086#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Use buffer >= 64K. Add linerest to double the size if the
1088 // line gets very long, to avoid a lot of copying. But don't
1089 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001090 size = 0x10000L + linerest;
1091 if (size > 0x100000L)
1092 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001093#endif
1094 }
1095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001096 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001097 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001100 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 size = 1;
1102 }
1103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
1105 if (!skip_read)
1106 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001107 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001109 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 FALSE)) != NULL)
1111 break;
1112 }
1113 if (new_buffer == NULL)
1114 {
1115 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1116 error = TRUE;
1117 break;
1118 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1121 vim_free(buffer);
1122 buffer = new_buffer;
1123 ptr = buffer + linerest;
1124 line_start = buffer;
1125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // May need room to translate into.
1127 // For iconv() we don't really know the required space, use a
1128 // factor ICONV_MULT.
1129 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1130 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1131 // become up to 4 bytes, size must be multiple of 2
1132 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1133 // multiple of 2
1134 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1135 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001136 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001137#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (iconv_fd != (iconv_t)-1)
1139 size = size / ICONV_MULT;
1140 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (fio_flags & FIO_LATIN1)
1143 size = size / 2;
1144 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1145 size = (size * 2 / 3) & ~1;
1146 else if (fio_flags & FIO_UCS4)
1147 size = (size * 2 / 3) & ~3;
1148 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001150#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001152 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001153#endif
1154#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001156 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#endif
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (conv_restlen > 0)
1160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 mch_memmove(ptr, conv_rest, conv_restlen);
1163 ptr += conv_restlen;
1164 size -= conv_restlen;
1165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 if (read_buffer)
1168 {
1169 /*
1170 * Read bytes from curbuf. Used for converting text read
1171 * from stdin.
1172 */
1173 if (read_buf_lnum > from)
1174 size = 0;
1175 else
1176 {
1177 int n, ni;
1178 long tlen;
1179
1180 tlen = 0;
1181 for (;;)
1182 {
1183 p = ml_get(read_buf_lnum) + read_buf_col;
1184 n = (int)STRLEN(p);
1185 if ((int)tlen + n + 1 > size)
1186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001187 // Filled up to "size", append partial line.
1188 // Change NL to NUL to reverse the effect done
1189 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001190 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 for (ni = 0; ni < n; ++ni)
1192 {
1193 if (p[ni] == NL)
1194 ptr[tlen++] = NUL;
1195 else
1196 ptr[tlen++] = p[ni];
1197 }
1198 read_buf_col += n;
1199 break;
1200 }
1201 else
1202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001203 // Append whole line and new-line. Change NL
1204 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 for (ni = 0; ni < n; ++ni)
1206 {
1207 if (p[ni] == NL)
1208 ptr[tlen++] = NUL;
1209 else
1210 ptr[tlen++] = p[ni];
1211 }
1212 ptr[tlen++] = NL;
1213 read_buf_col = 0;
1214 if (++read_buf_lnum > from)
1215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001216 // When the last line didn't have an
1217 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (!curbuf->b_p_eol)
1219 --tlen;
1220 size = tlen;
1221 break;
1222 }
1223 }
1224 }
1225 }
1226 }
1227 else
1228 {
1229 /*
1230 * Read bytes from the file.
1231 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001232 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001235#ifdef FEAT_CRYPT
1236 /*
1237 * At start of file: Check for magic number of encryption.
1238 */
1239 if (filesize == 0 && size > 0)
1240 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1241 &filesize, newfile, sfname,
1242 &did_ask_for_key);
1243 /*
1244 * Decrypt the read bytes. This is done before checking for
1245 * EOF because the crypt layer may be buffering.
1246 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001247 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1248 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001249 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001250# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001251 if (crypt_works_inplace(curbuf->b_cryptstate))
1252 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001253# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001254 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001255# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 }
1257 else
1258 {
1259 char_u *newptr = NULL;
1260 int decrypted_size;
1261
1262 decrypted_size = crypt_decode_alloc(
1263 curbuf->b_cryptstate, ptr, size, &newptr);
1264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001265 // If the crypt layer is buffering, not producing
1266 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001267 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001268 continue;
1269
1270 if (linerest == 0)
1271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 // Simple case: reuse returned buffer (may be
1273 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001274 new_buffer = newptr;
1275 }
1276 else
1277 {
1278 long_u new_size;
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001281 new_size = (long_u)(decrypted_size + linerest + 1);
1282 new_buffer = lalloc(new_size, FALSE);
1283 if (new_buffer == NULL)
1284 {
1285 do_outofmem_msg(new_size);
1286 error = TRUE;
1287 break;
1288 }
1289
1290 mch_memmove(new_buffer, buffer, linerest);
1291 if (newptr != NULL)
1292 mch_memmove(new_buffer + linerest, newptr,
1293 decrypted_size);
1294 }
1295
1296 if (new_buffer != NULL)
1297 {
1298 vim_free(buffer);
1299 buffer = new_buffer;
1300 new_buffer = NULL;
1301 line_start = buffer;
1302 ptr = buffer + linerest;
1303 }
1304 size = decrypted_size;
1305 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001306# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 }
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (size <= 0)
1311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001312 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001315 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001316 /*
1317 * Reached end-of-file but some trailing bytes could
1318 * not be converted. Truncated file?
1319 */
1320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001321 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001322 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001323#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001324 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001325#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001326 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001327 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001328 if (can_retry)
1329 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001330 if (conv_error == 0)
1331 conv_error = curbuf->b_ml.ml_line_count
1332 - linecnt + 1;
1333 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001334 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001335 else if (illegal_byte == 0)
1336 illegal_byte = curbuf->b_ml.ml_line_count
1337 - linecnt + 1;
1338 if (bad_char_behavior == BAD_DROP)
1339 {
1340 *(ptr - conv_restlen) = NUL;
1341 conv_restlen = 0;
1342 }
1343 else
1344 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001345 // Replace the trailing bytes with the replacement
1346 // character if we were converting; if we weren't,
1347 // leave the UTF8 checking code to do it, as it
1348 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001349 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001350#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001351 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001352#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001353 ))
1354 {
1355 while (conv_restlen > 0)
1356 {
1357 *(--ptr) = bad_char_behavior;
1358 --conv_restlen;
1359 }
1360 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001362#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001363 if (iconv_fd != (iconv_t)-1)
1364 {
1365 iconv_close(iconv_fd);
1366 iconv_fd = (iconv_t)-1;
1367 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001368#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001369 }
1370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 skip_read = FALSE;
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /*
1376 * At start of file (or after crypt magic number): Check for BOM.
1377 * Also check for a BOM for other Unicode encodings, but not after
1378 * converting with 'charconvert' or when a BOM has already been
1379 * found.
1380 */
1381 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001382#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 || (cryptkey != NULL
1384 && filesize == crypt_get_header_len(
1385 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 )
1388 && (fio_flags == FIO_UCSBOM
1389 || (!curbuf->b_p_bomb
1390 && tmpname == NULL
1391 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1392 {
1393 char_u *ccname;
1394 int blen;
1395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (size < 2 || curbuf->b_p_bin)
1398 ccname = NULL;
1399 else
1400 ccname = check_for_bom(ptr, size, &blen,
1401 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1402 if (ccname != NULL)
1403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 filesize += blen;
1406 size -= blen;
1407 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001408 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001411 curbuf->b_start_bomb = TRUE;
1412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414
1415 if (fio_flags == FIO_UCSBOM)
1416 {
1417 if (ccname == NULL)
1418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 advance_fenc = TRUE;
1421 }
1422 else
1423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001424 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (fenc_alloced)
1426 vim_free(fenc);
1427 fenc = ccname;
1428 fenc_alloced = FALSE;
1429 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 skip_read = TRUE;
1432 goto retry;
1433 }
1434 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001436 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001437 ptr -= conv_restlen;
1438 size += conv_restlen;
1439 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * Break here for a read error or end-of-file.
1442 */
1443 if (size <= 0)
1444 break;
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar13505972019-01-24 15:04:48 +01001447#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (iconv_fd != (iconv_t)-1)
1449 {
1450 /*
1451 * Attempt conversion of the read bytes to 'encoding' using
1452 * iconv().
1453 */
1454 const char *fromp;
1455 char *top;
1456 size_t from_size;
1457 size_t to_size;
1458
1459 fromp = (char *)ptr;
1460 from_size = size;
1461 ptr += size;
1462 top = (char *)ptr;
1463 to_size = real_size - size;
1464
1465 /*
1466 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001467 * another conversion. Except for when there is no
1468 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001470 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1471 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1473 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001474 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001475 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001476 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001477 if (conv_error == 0)
1478 conv_error = readfile_linenr(linecnt,
1479 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001481 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001482 ++fromp;
1483 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001484 if (bad_char_behavior == BAD_KEEP)
1485 {
1486 *top++ = *(fromp - 1);
1487 --to_size;
1488 }
1489 else if (bad_char_behavior != BAD_DROP)
1490 {
1491 *top++ = bad_char_behavior;
1492 --to_size;
1493 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 if (from_size > 0)
1497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001498 // Some remaining characters, keep them for the next
1499 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1501 conv_restlen = (int)from_size;
1502 }
1503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 line_start = ptr - linerest;
1506 mch_memmove(line_start, buffer, (size_t)linerest);
1507 size = (long)((char_u *)top - ptr);
1508 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaar4f974752019-02-17 17:44:42 +01001511#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (fio_flags & FIO_CODEPAGE)
1513 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001514 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001515 WCHAR ucs2buf[3];
1516 int ucs2len;
1517 int codepage = FIO_GET_CP(fio_flags);
1518 int bytelen;
1519 int found_bad;
1520 char replstr[2];
1521
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /*
1523 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001524 * a codepage, using standard MS-Windows functions. This
1525 * requires two steps:
1526 * 1. convert from 'fileencoding' to ucs-2
1527 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001529 * Because there may be illegal bytes AND an incomplete byte
1530 * sequence at the end, we may have to do the conversion one
1531 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001534 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001535 if (bad_char_behavior > 0)
1536 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001538 replstr[0] = '?';
1539 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001542 * Move the bytes to the end of the buffer, so that we have
1543 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001545 src = ptr + real_size - size;
1546 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001548 /*
1549 * Do the conversion.
1550 */
1551 dst = ptr;
1552 size = size;
1553 while (size > 0)
1554 {
1555 found_bad = FALSE;
1556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001558 if (codepage == CP_UTF8)
1559 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001560 // Handle CP_UTF8 input ourselves to be able to handle
1561 // trailing bytes properly.
1562 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001563 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001564 if (bytelen > size)
1565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001566 // Only got some bytes of a character. Normally
1567 // it's put in "conv_rest", but if it's too long
1568 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001569 if (bytelen <= CONV_RESTLEN)
1570 break;
1571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001572 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001573 bytelen = size;
1574 found_bad = TRUE;
1575 }
1576 else
1577 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001578 int u8c = utf_ptr2char(src);
1579
Bram Moolenaar86e01082005-12-29 22:45:34 +00001580 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001581 found_bad = TRUE;
1582 ucs2buf[0] = u8c;
1583 ucs2len = 1;
1584 }
1585 }
1586 else
1587# endif
1588 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001589 // We don't know how long the byte sequence is, try
1590 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001591 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1592 ++bytelen)
1593 {
1594 ucs2len = MultiByteToWideChar(codepage,
1595 MB_ERR_INVALID_CHARS,
1596 (LPCSTR)src, bytelen,
1597 ucs2buf, 3);
1598 if (ucs2len > 0)
1599 break;
1600 }
1601 if (ucs2len == 0)
1602 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 // If we have only one byte then it's probably an
1604 // incomplete byte sequence. Otherwise discard
1605 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001606 if (size == 1)
1607 break;
1608 found_bad = TRUE;
1609 bytelen = 1;
1610 }
1611 }
1612
1613 if (!found_bad)
1614 {
1615 int i;
1616
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001617 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001618 if (enc_utf8)
1619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001620 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001621 for (i = 0; i < ucs2len; ++i)
1622 dst += utf_char2bytes(ucs2buf[i], dst);
1623 }
1624 else
1625 {
1626 BOOL bad = FALSE;
1627 int dstlen;
1628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629 // From UCS-2 to "enc_codepage". If the
1630 // conversion uses the default character "?",
1631 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001632 dstlen = WideCharToMultiByte(enc_codepage, 0,
1633 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001634 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001635 replstr, &bad);
1636 if (bad)
1637 found_bad = TRUE;
1638 else
1639 dst += dstlen;
1640 }
1641 }
1642
1643 if (found_bad)
1644 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001645 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 if (can_retry)
1647 goto rewind_retry;
1648 if (conv_error == 0)
1649 conv_error = readfile_linenr(linecnt, ptr, dst);
1650 if (bad_char_behavior != BAD_DROP)
1651 {
1652 if (bad_char_behavior == BAD_KEEP)
1653 {
1654 mch_memmove(dst, src, bytelen);
1655 dst += bytelen;
1656 }
1657 else
1658 *dst++ = bad_char_behavior;
1659 }
1660 }
1661
1662 src += bytelen;
1663 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665
1666 if (size > 0)
1667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001668 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 mch_memmove(conv_rest, src, size);
1670 conv_restlen = size;
1671 }
1672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001674 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001677#endif
1678#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (fio_flags & FIO_MACROMAN)
1680 {
1681 /*
1682 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001683 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001685 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (fio_flags != 0)
1691 {
1692 int u8c;
1693 char_u *dest;
1694 char_u *tail = NULL;
1695
1696 /*
1697 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1698 * "enc_utf8" not set: Convert Unicode to Latin1.
1699 * Go from end to start through the buffer, because the number
1700 * of bytes may increase.
1701 * "dest" points to after where the UTF-8 bytes go, "p" points
1702 * to after the next character to convert.
1703 */
1704 dest = ptr + real_size;
1705 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1706 {
1707 p = ptr + size;
1708 if (fio_flags == FIO_UTF8)
1709 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001710 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 tail = ptr + size - 1;
1712 while (tail > ptr && (*tail & 0xc0) == 0x80)
1713 --tail;
1714 if (tail + utf_byte2len(*tail) <= ptr + size)
1715 tail = NULL;
1716 else
1717 p = tail;
1718 }
1719 }
1720 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001722 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 p = ptr + (size & ~1);
1724 if (size & 1)
1725 tail = p;
1726 if ((fio_flags & FIO_UTF16) && p > ptr)
1727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if (fio_flags & FIO_ENDIAN_L)
1730 {
1731 u8c = (*--p << 8);
1732 u8c += *--p;
1733 }
1734 else
1735 {
1736 u8c = *--p;
1737 u8c += (*--p << 8);
1738 }
1739 if (u8c >= 0xd800 && u8c <= 0xdbff)
1740 tail = p;
1741 else
1742 p += 2;
1743 }
1744 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 p = ptr + (size & ~3);
1749 if (size & 3)
1750 tail = p;
1751 }
1752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 // If there is a trailing incomplete sequence move it to
1754 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (tail != NULL)
1756 {
1757 conv_restlen = (int)((ptr + size) - tail);
1758 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1759 size -= conv_restlen;
1760 }
1761
1762
1763 while (p > ptr)
1764 {
1765 if (fio_flags & FIO_LATIN1)
1766 u8c = *--p;
1767 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1768 {
1769 if (fio_flags & FIO_ENDIAN_L)
1770 {
1771 u8c = (*--p << 8);
1772 u8c += *--p;
1773 }
1774 else
1775 {
1776 u8c = *--p;
1777 u8c += (*--p << 8);
1778 }
1779 if ((fio_flags & FIO_UTF16)
1780 && u8c >= 0xdc00 && u8c <= 0xdfff)
1781 {
1782 int u16c;
1783
1784 if (p == ptr)
1785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001786 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (can_retry)
1788 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001789 if (conv_error == 0)
1790 conv_error = readfile_linenr(linecnt,
1791 ptr, p);
1792 if (bad_char_behavior == BAD_DROP)
1793 continue;
1794 if (bad_char_behavior != BAD_KEEP)
1795 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // found second word of double-word, get the first
1799 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (fio_flags & FIO_ENDIAN_L)
1801 {
1802 u16c = (*--p << 8);
1803 u16c += *--p;
1804 }
1805 else
1806 {
1807 u16c = *--p;
1808 u16c += (*--p << 8);
1809 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001810 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1811 + (u8c & 0x3ff);
1812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001813 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (u16c < 0xd800 || u16c > 0xdbff)
1815 {
1816 if (can_retry)
1817 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001818 if (conv_error == 0)
1819 conv_error = readfile_linenr(linecnt,
1820 ptr, p);
1821 if (bad_char_behavior == BAD_DROP)
1822 continue;
1823 if (bad_char_behavior != BAD_KEEP)
1824 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 }
1828 else if (fio_flags & FIO_UCS4)
1829 {
1830 if (fio_flags & FIO_ENDIAN_L)
1831 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001832 u8c = (unsigned)*--p << 24;
1833 u8c += (unsigned)*--p << 16;
1834 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 u8c += *--p;
1836 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001840 u8c += (unsigned)*--p << 8;
1841 u8c += (unsigned)*--p << 16;
1842 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001845 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
1847 if (*--p < 0x80)
1848 u8c = *p;
1849 else
1850 {
1851 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001852 p -= len;
1853 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (len == 0)
1855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001856 // Not a valid UTF-8 character, retry with
1857 // another fenc when possible, otherwise just
1858 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (can_retry)
1860 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001861 if (conv_error == 0)
1862 conv_error = readfile_linenr(linecnt,
1863 ptr, p);
1864 if (bad_char_behavior == BAD_DROP)
1865 continue;
1866 if (bad_char_behavior != BAD_KEEP)
1867 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 dest -= utf_char2len(u8c);
1874 (void)utf_char2bytes(u8c, dest);
1875 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001876 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 --dest;
1879 if (u8c >= 0x100)
1880 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001881 // character doesn't fit in latin1, retry with
1882 // another fenc when possible, otherwise just
1883 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001884 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001886 if (conv_error == 0)
1887 conv_error = readfile_linenr(linecnt, ptr, p);
1888 if (bad_char_behavior == BAD_DROP)
1889 ++dest;
1890 else if (bad_char_behavior == BAD_KEEP)
1891 *dest = u8c;
1892 else if (eap != NULL && eap->bad_char != 0)
1893 *dest = bad_char_behavior;
1894 else
1895 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 else
1898 *dest = u8c;
1899 }
1900 }
1901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 line_start = dest - linerest;
1904 mch_memmove(line_start, buffer, (size_t)linerest);
1905 size = (long)((ptr + real_size) - dest);
1906 ptr = dest;
1907 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001908 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001910 int incomplete_tail = FALSE;
1911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001913 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001915 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001916 int l;
1917
1918 if (todo <= 0)
1919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (*p >= 0x80)
1921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 // A length of 1 means it's an illegal byte. Accept
1923 // an incomplete character at the end though, the next
1924 // read() will get the next bytes, we'll check it
1925 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001926 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001927 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001929 // Avoid retrying with a different encoding when
1930 // a truncated file is more likely, or attempting
1931 // to read the rest of an incomplete sequence when
1932 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001933 if (p > ptr || filesize > 0)
1934 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001935 // Incomplete byte sequence, move it to conv_rest[]
1936 // and try to read the rest of it, unless we've
1937 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001938 if (p > ptr)
1939 {
1940 conv_restlen = todo;
1941 mch_memmove(conv_rest, p, conv_restlen);
1942 size -= conv_restlen;
1943 break;
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001946 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001947 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // Illegal byte. If we can try another encoding
1949 // do that, unless at EOF where a truncated
1950 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001951 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001952 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001953#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001955 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1956 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001957#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001959 if (conv_error == 0 && illegal_byte == 0)
1960 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001962 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001963 if (bad_char_behavior == BAD_DROP)
1964 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001965 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001966 --p;
1967 --size;
1968 }
1969 else if (bad_char_behavior != BAD_KEEP)
1970 *p = bad_char_behavior;
1971 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001972 else
1973 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001976 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01001981#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001982 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001983 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001984 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001986#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001988 advance_fenc = TRUE;
1989 file_rewind = TRUE;
1990 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 filesize += size;
1996
1997 /*
1998 * when reading the first part of a file: guess EOL type
1999 */
2000 if (fileformat == EOL_UNKNOWN)
2001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (try_dos || try_unix)
2004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002006 if (try_mac)
2007 try_mac = 1;
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 for (p = ptr; p < ptr + size; ++p)
2010 {
2011 if (*p == NL)
2012 {
2013 if (!try_unix
2014 || (try_dos && p > ptr && p[-1] == CAR))
2015 fileformat = EOL_DOS;
2016 else
2017 fileformat = EOL_UNIX;
2018 break;
2019 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002020 else if (*p == CAR && try_mac)
2021 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (fileformat == EOL_UNIX && try_mac)
2026 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002027 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 try_mac = 1;
2029 try_unix = 1;
2030 for (; p >= ptr && *p != CAR; p--)
2031 ;
2032 if (p >= ptr)
2033 {
2034 for (p = ptr; p < ptr + size; ++p)
2035 {
2036 if (*p == NL)
2037 try_unix++;
2038 else if (*p == CAR)
2039 try_mac++;
2040 }
2041 if (try_mac > try_unix)
2042 fileformat = EOL_MAC;
2043 }
2044 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002045 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // Looking for CR but found no end-of-line markers at
2047 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002048 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (fileformat == EOL_UNKNOWN && try_mac)
2053 fileformat = EOL_MAC;
2054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (fileformat == EOL_UNKNOWN)
2057 fileformat = default_fileformat();
2058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002060 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 set_fileformat(fileformat, OPT_LOCAL);
2062 }
2063 }
2064
2065 /*
2066 * This loop is executed once for every character read.
2067 * Keep it fast!
2068 */
2069 if (fileformat == EOL_MAC)
2070 {
2071 --ptr;
2072 while (++ptr, --size >= 0)
2073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if ((c = *ptr) != NUL && c != CAR && c != NL)
2076 continue;
2077 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 else
2082 {
2083 if (skip_count == 0)
2084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 len = (colnr_T) (ptr - line_start + 1);
2087 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2088 {
2089 error = TRUE;
2090 break;
2091 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002092#ifdef FEAT_PERSISTENT_UNDO
2093 if (read_undo_file)
2094 sha256_update(&sha_ctx, line_start, len);
2095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 ++lnum;
2097 if (--read_count == 0)
2098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 error = TRUE; // break loop
2100 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 break;
2102 }
2103 }
2104 else
2105 --skip_count;
2106 line_start = ptr + 1;
2107 }
2108 }
2109 }
2110 else
2111 {
2112 --ptr;
2113 while (++ptr, --size >= 0)
2114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 continue;
2117 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120 {
2121 if (skip_count == 0)
2122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002123 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 len = (colnr_T)(ptr - line_start + 1);
2125 if (fileformat == EOL_DOS)
2126 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002127 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 ptr[-1] = NUL;
2131 --len;
2132 }
2133 /*
2134 * Reading in Dos format, but no CR-LF found!
2135 * When 'fileformats' includes "unix", delete all
2136 * the lines read so far and start all over again.
2137 * Otherwise give an error message later.
2138 */
2139 else if (ff_error != EOL_DOS)
2140 {
2141 if ( try_unix
2142 && !read_stdin
2143 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002144 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2145 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 set_fileformat(EOL_UNIX, OPT_LOCAL);
2150 file_rewind = TRUE;
2151 keep_fileformat = TRUE;
2152 goto retry;
2153 }
2154 ff_error = EOL_DOS;
2155 }
2156 }
2157 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2158 {
2159 error = TRUE;
2160 break;
2161 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002162#ifdef FEAT_PERSISTENT_UNDO
2163 if (read_undo_file)
2164 sha256_update(&sha_ctx, line_start, len);
2165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ++lnum;
2167 if (--read_count == 0)
2168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 error = TRUE; // break loop
2170 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 break;
2172 }
2173 }
2174 else
2175 --skip_count;
2176 line_start = ptr + 1;
2177 }
2178 }
2179 }
2180 linerest = (long)(ptr - line_start);
2181 ui_breakcheck();
2182 }
2183
2184failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002185 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (error && read_count == 0)
2187 error = FALSE;
2188
2189 /*
2190 * If we get EOF in the middle of a line, note the fact and
2191 * complete the line ourselves.
2192 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2193 */
2194 if (!error
2195 && !got_int
2196 && linerest != 0
2197 && !(!curbuf->b_p_bin
2198 && fileformat == EOL_DOS
2199 && *line_start == Ctrl_Z
2200 && ptr == line_start + 1))
2201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 curbuf->b_p_eol = FALSE;
2205 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002206 len = (colnr_T)(ptr - line_start + 1);
2207 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 error = TRUE;
2209 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002210 {
2211#ifdef FEAT_PERSISTENT_UNDO
2212 if (read_undo_file)
2213 sha256_update(&sha_ctx, line_start, len);
2214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002223 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002224 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002225 crypt_free_state(curbuf->b_cryptstate);
2226 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002227 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002228 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2229 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 // Don't set cryptkey to NULL, it's used below as a flag that
2231 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 // If editing a new file: set 'fenc' for the current buffer.
2235 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002238 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (fenc_alloced)
2240 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002241#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244#endif
2245
2246 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002248#ifdef HAVE_FD_CLOEXEC
2249 else
2250 {
2251 int fdflags = fcntl(fd, F_GETFD);
2252 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002253 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002254 }
2255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 vim_free(buffer);
2257
2258#ifdef HAVE_DUP
2259 if (read_stdin)
2260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002263 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265#endif
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (tmpname != NULL)
2268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 vim_free(tmpname);
2271 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 /*
2275 * In recovery mode everything but autocommands is skipped.
2276 */
2277 if (!recoverymode)
2278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002279 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2281 {
2282#ifdef FEAT_NETBEANS_INTG
2283 netbeansFireChanges = 0;
2284#endif
2285 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
2286#ifdef FEAT_NETBEANS_INTG
2287 netbeansFireChanges = 1;
2288#endif
2289 --linecnt;
2290 }
2291 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2292 if (filesize == 0)
2293 linecnt = 0;
2294 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002297#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 // After reading the text into the buffer the diff info needs to
2299 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002300 diff_invalidate(curbuf);
2301#endif
2302#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002303 // All folds in the window are invalid now. Mark them for update
2304 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002305 foldUpdateAll(curwin);
2306#endif
2307 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002308 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 appended_lines_mark(from, linecnt);
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#ifndef ALWAYS_USE_GUI
2312 /*
2313 * If we were reading from the same terminal as where messages go,
2314 * the screen will have been messed up.
2315 * Switch on raw mode now and clear the screen.
2316 */
2317 if (read_stdin)
2318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 starttermcap();
2321 screenclear();
2322 }
2323#endif
2324
2325 if (got_int)
2326 {
2327 if (!(flags & READ_DUMMY))
2328 {
2329 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2330 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 msg_scroll = msg_save;
2334#ifdef FEAT_VIMINFO
2335 check_marks_read();
2336#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002337 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339
2340 if (!filtering && !(flags & READ_DUMMY))
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 c = FALSE;
2344
2345#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 STRCAT(IObuff, _("[fifo]"));
2349 c = TRUE;
2350 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002351 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 STRCAT(IObuff, _("[socket]"));
2354 c = TRUE;
2355 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002356# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002357 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002358 {
2359 STRCAT(IObuff, _("[character special]"));
2360 c = TRUE;
2361 }
2362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#endif
2364 if (curbuf->b_p_ro)
2365 {
2366 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2367 c = TRUE;
2368 }
2369 if (read_no_eol_lnum)
2370 {
2371 msg_add_eol();
2372 c = TRUE;
2373 }
2374 if (ff_error == EOL_DOS)
2375 {
2376 STRCAT(IObuff, _("[CR missing]"));
2377 c = TRUE;
2378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (split)
2380 {
2381 STRCAT(IObuff, _("[long lines split]"));
2382 c = TRUE;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (notconverted)
2385 {
2386 STRCAT(IObuff, _("[NOT converted]"));
2387 c = TRUE;
2388 }
2389 else if (converted)
2390 {
2391 STRCAT(IObuff, _("[converted]"));
2392 c = TRUE;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#ifdef FEAT_CRYPT
2395 if (cryptkey != NULL)
2396 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002397 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 c = TRUE;
2399 }
2400#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002401 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002403 sprintf((char *)IObuff + STRLEN(IObuff),
2404 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 c = TRUE;
2406 }
2407 else if (illegal_byte > 0)
2408 {
2409 sprintf((char *)IObuff + STRLEN(IObuff),
2410 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2411 c = TRUE;
2412 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002413 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 STRCAT(IObuff, _("[READ ERRORS]"));
2416 c = TRUE;
2417 }
2418 if (msg_add_fileformat(fileformat))
2419 c = TRUE;
2420#ifdef FEAT_CRYPT
2421 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002422 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002423 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 else
2425#endif
2426 msg_add_lines(c, (long)linecnt, filesize);
2427
Bram Moolenaard23a8232018-02-10 18:45:26 +01002428 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 msg_scrolled_ign = TRUE;
2430#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // Don't show the message when reading stdin, it would end up in a
2432 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (read_stdin || read_buffer)
2434 p = msg_may_trunc(FALSE, IObuff);
2435 else
2436#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002437 {
2438 if (msg_col > 0)
2439 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002440 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002443 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002444 // Need to repeat the message after redrawing when:
2445 // - When reading from stdin (the screen will be cleared next).
2446 // - When restart_edit is set (otherwise there will be a delay
2447 // before redrawing).
2448 // - When the screen was scrolled but there is no wait-return
2449 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002450 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 msg_scrolled_ign = FALSE;
2452 }
2453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002454 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002456 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002457 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 curbuf->b_p_ro = TRUE;
2459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002460 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /*
2463 * In Ex mode: cursor at last new line.
2464 * Otherwise: cursor at first new line.
2465 */
2466 if (exmode_active)
2467 curwin->w_cursor.lnum = from + linecnt;
2468 else
2469 curwin->w_cursor.lnum = from + 1;
2470 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002471 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002473 if (!cmdmod.lockmarks)
2474 {
2475 // Set '[ and '] marks to the newly read lines.
2476 curbuf->b_op_start.lnum = from + 1;
2477 curbuf->b_op_start.col = 0;
2478 curbuf->b_op_end.lnum = from + linecnt;
2479 curbuf->b_op_end.col = 0;
2480 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002481
Bram Moolenaar4f974752019-02-17 17:44:42 +01002482#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002483 /*
2484 * Work around a weird problem: When a file has two links (only
2485 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002486 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002487 * It's correct again after reading the file, thus reset the timestamp
2488 * here.
2489 */
2490 if (newfile && !read_stdin && !read_buffer
2491 && mch_stat((char *)fname, &st) >= 0)
2492 {
2493 buf_store_time(curbuf, &st, fname);
2494 curbuf->b_mtime_read = curbuf->b_mtime;
2495 }
2496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498 msg_scroll = msg_save;
2499
2500#ifdef FEAT_VIMINFO
2501 /*
2502 * Get the marks before executing autocommands, so they can be used there.
2503 */
2504 check_marks_read();
2505#endif
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002508 * We remember if the last line of the read didn't have
2509 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2510 * or writing the read again with 'binary' on. The latter is required
2511 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002513 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002515 // When reloading a buffer put the cursor at the first line that is
2516 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002517 if (flags & READ_KEEP_UNDO)
2518 u_find_first_changed();
2519
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002520#ifdef FEAT_PERSISTENT_UNDO
2521 /*
2522 * When opening a new file locate undo info and read it.
2523 */
2524 if (read_undo_file)
2525 {
2526 char_u hash[UNDO_HASH_SIZE];
2527
2528 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002529 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002530 }
2531#endif
2532
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002533 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 int m = msg_scroll;
2536 int n = msg_scrolled;
2537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 // Save the fileformat now, otherwise the buffer will be considered
2539 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002540 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 save_file_ff(curbuf);
2542
2543 /*
2544 * The output from the autocommands should not overwrite anything and
2545 * should not be overwritten: Set msg_scroll, restore its value if no
2546 * output was done.
2547 */
2548 msg_scroll = TRUE;
2549 if (filtering)
2550 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2551 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002552 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2555 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002556 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2557 /*
2558 * EVENT_FILETYPE was not triggered but the buffer already has a
2559 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2560 */
2561 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2562 TRUE, curbuf);
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 else
2565 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2566 FALSE, NULL, eap);
2567 if (msg_scrolled == n)
2568 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002569# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002570 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 if (recoverymode && error)
2576 return FAIL;
2577 return OK;
2578}
2579
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002580#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002581/*
2582 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2583 * which is the name of files used for process substitution output by
2584 * some shells on some operating systems, e.g., bash on SunOS.
2585 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2586 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002587 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002588is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002589{
2590 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2591 && VIM_ISDIGIT(fname[8])
2592 && *skipdigits(fname + 9) == NUL
2593 && (fname[9] != NUL
2594 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2595}
2596#endif
2597
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002598/*
2599 * From the current line count and characters read after that, estimate the
2600 * line number where we are now.
2601 * Used for error messages that include a line number.
2602 */
2603 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 linenr_T linecnt, // line count before reading more bytes
2606 char_u *p, // start of more bytes read
2607 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002608{
2609 char_u *s;
2610 linenr_T lnum;
2611
2612 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2613 for (s = p; s < endp; ++s)
2614 if (*s == '\n')
2615 ++lnum;
2616 return lnum;
2617}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002620 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2621 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * Returns OK or FAIL.
2623 */
2624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002625prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
Bram Moolenaar13505972019-01-24 15:04:48 +01002627 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (eap->cmd == NULL)
2629 return FAIL;
2630
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002631 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2632 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002633 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002634 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002635
2636 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002637 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002638 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return OK;
2640}
2641
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002642/*
2643 * Set default or forced 'fileformat' and 'binary'.
2644 */
2645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002646set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002647{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002648 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002649 if (set_options)
2650 {
2651 if (eap != NULL && eap->force_ff != 0)
2652 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2653 else if (*p_ffs != NUL)
2654 set_fileformat(default_fileformat(), OPT_LOCAL);
2655 }
2656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002657 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002658 if (eap != NULL && eap->force_bin != 0)
2659 {
2660 int oldval = curbuf->b_p_bin;
2661
2662 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2663 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2664 }
2665}
2666
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002667/*
2668 * Set forced 'fileencoding'.
2669 */
2670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002671set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002672{
2673 if (eap->force_enc != 0)
2674 {
2675 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2676
2677 if (fenc != NULL)
2678 set_string_option_direct((char_u *)"fenc", -1,
2679 fenc, OPT_FREE|OPT_LOCAL, 0);
2680 vim_free(fenc);
2681 }
2682}
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
2685 * Find next fileencoding to use from 'fileencodings'.
2686 * "pp" points to fenc_next. It's advanced to the next item.
2687 * When there are no more items, an empty string is returned and *pp is set to
2688 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002689 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2690 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
2692 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002693next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 char_u *p;
2696 char_u *r;
2697
Bram Moolenaarf077db22019-08-13 00:18:24 +02002698 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (**pp == NUL)
2700 {
2701 *pp = NULL;
2702 return (char_u *)"";
2703 }
2704 p = vim_strchr(*pp, ',');
2705 if (p == NULL)
2706 {
2707 r = enc_canonize(*pp);
2708 *pp += STRLEN(*pp);
2709 }
2710 else
2711 {
2712 r = vim_strnsave(*pp, (int)(p - *pp));
2713 *pp = p + 1;
2714 if (r != NULL)
2715 {
2716 p = enc_canonize(r);
2717 vim_free(r);
2718 r = p;
2719 }
2720 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002721 if (r != NULL)
2722 *alloced = TRUE;
2723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002725 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 r = (char_u *)"";
2727 *pp = NULL;
2728 }
2729 return r;
2730}
2731
Bram Moolenaar13505972019-01-24 15:04:48 +01002732#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733/*
2734 * Convert a file with the 'charconvert' expression.
2735 * This closes the file which is to be read, converts it and opens the
2736 * resulting file for reading.
2737 * Returns name of the resulting converted file (the caller should delete it
2738 * after reading it).
2739 * Returns NULL if the conversion failed ("*fdp" is not set) .
2740 */
2741 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002742readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002743 char_u *fname, // name of input file
2744 char_u *fenc, // converted from
2745 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002748 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002750 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002752 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 else
2754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002755 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 *fdp = -1;
2757 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2758 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002759 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2761 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002762 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764
2765 if (errmsg != NULL)
2766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002767 // Don't use emsg(), it breaks mappings, the retry with
2768 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002769 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (tmpname != NULL)
2771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002772 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002773 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 }
2776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002777 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (*fdp < 0)
2779 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2780
2781 return tmpname;
2782}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#endif
2784
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002785#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002787 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2789 * *filesizep are updated.
2790 * Return the (new) encryption key, NULL for no encryption.
2791 */
2792 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002793check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 char_u *cryptkey, // previous encryption key or NULL
2795 char_u *ptr, // pointer to read bytes
2796 long *sizep, // length of read bytes
2797 off_T *filesizep, // nr of bytes used from file
2798 int newfile, // editing a new buffer
2799 char_u *fname, // file name to display
2800 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002802 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002803 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002804
2805 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002807 // Mark the buffer as read-only until the decryption has taken place.
2808 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002809 curbuf->b_p_ro = TRUE;
2810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002811 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002812 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002813 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
2815 if (*curbuf->b_p_key)
2816 cryptkey = curbuf->b_p_key;
2817 else
2818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002819 // When newfile is TRUE, store the typed key in the 'key'
2820 // option and don't free it. bf needs hash of the key saved.
2821 // Don't ask for the key again when first time Enter was hit.
2822 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002823 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002824 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002825 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002826 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002827 *did_ask = TRUE;
2828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (cryptkey != NULL && *cryptkey == NUL)
2831 {
2832 if (cryptkey != curbuf->b_p_key)
2833 vim_free(cryptkey);
2834 cryptkey = NULL;
2835 }
2836 }
2837 }
2838
2839 if (cryptkey != NULL)
2840 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002841 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002842
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002843 curbuf->b_cryptstate = crypt_create_from_header(
2844 method, cryptkey, ptr);
2845 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002847 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002848 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002849 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002850 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002851 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002852 *filesizep += header_len;
2853 *sizep -= header_len;
2854 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2855
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002857 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002860 // When starting to edit a new file which does not have encryption, clear
2861 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002862 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2864
2865 return cryptkey;
2866}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002867#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002870 * Return TRUE if a file appears to be read-only from the file permissions.
2871 */
2872 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002873check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 char_u *fname, // full path to file
2875 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002876{
2877#ifndef USE_MCH_ACCESS
2878 int fd = 0;
2879#endif
2880
2881 return (
2882#ifdef USE_MCH_ACCESS
2883# ifdef UNIX
2884 (perm & 0222) == 0 ||
2885# endif
2886 mch_access((char *)fname, W_OK)
2887#else
2888 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2889 ? TRUE : (close(fd), FALSE)
2890#endif
2891 );
2892}
2893
Bram Moolenaara7870192019-02-14 12:56:36 +01002894#if defined(HAVE_FSYNC) || defined(PROTO)
2895/*
2896 * Call fsync() with Mac-specific exception.
2897 * Return fsync() result: zero for success.
2898 */
2899 int
2900vim_fsync(int fd)
2901{
2902 int r;
2903
2904# ifdef MACOS_X
2905 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002906 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002907# endif
2908 r = fsync(fd);
2909 return r;
2910}
2911#endif
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002914 * Set the name of the current buffer. Use when the buffer doesn't have a
2915 * name and a ":r" or ":w" command with a file name is used.
2916 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002918set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002919{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002920 buf_T *buf = curbuf;
2921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002923 if (curbuf->b_p_bl)
2924 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2925 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002926#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002927 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002928 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002929#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002930 if (curbuf != buf)
2931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002932 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002933 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002934 return FAIL;
2935 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002936
2937 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2938 curbuf->b_flags |= BF_NOTEDITED;
2939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002941 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2942 if (curbuf->b_p_bl)
2943 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002944#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002946 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002947#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002949 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002950 if (*curbuf->b_p_ft == NUL)
2951 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002952 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002953 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002954 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002955 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002956
2957 return OK;
2958}
2959
2960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 * Put file name into IObuff with quotes.
2962 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002963 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002964msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 if (fname == NULL)
2967 fname = (char_u *)"-stdin-";
2968 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2969 IObuff[0] = '"';
2970 STRCAT(IObuff, "\" ");
2971}
2972
2973/*
2974 * Append message for text mode to IObuff.
2975 * Return TRUE if something appended.
2976 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002977 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002978msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980#ifndef USE_CRNL
2981 if (eol_type == EOL_DOS)
2982 {
2983 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
2984 return TRUE;
2985 }
2986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (eol_type == EOL_MAC)
2988 {
2989 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
2990 return TRUE;
2991 }
Bram Moolenaar00590742019-02-15 21:06:09 +01002992#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (eol_type == EOL_UNIX)
2994 {
2995 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
2996 return TRUE;
2997 }
2998#endif
2999 return FALSE;
3000}
3001
3002/*
3003 * Append line and character count to IObuff.
3004 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003006msg_add_lines(
3007 int insert_space,
3008 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003009 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 char_u *p;
3012
3013 p = IObuff + STRLEN(IObuff);
3014
3015 if (insert_space)
3016 *p++ = ' ';
3017 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003018 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003019 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 else
3021 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003022 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003024 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3025 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003026 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 }
3028}
3029
3030/*
3031 * Append message for missing line separator to IObuff.
3032 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003034msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3037}
3038
Bram Moolenaar473952e2019-09-28 16:30:04 +02003039 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003040time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003042#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003043 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3044 // the seconds. Since the roundoff is done when flushing the inode, the
3045 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 return (t1 - t2 > 1 || t2 - t1 > 1);
3047#else
3048 return (t1 != t2);
3049#endif
3050}
3051
3052/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003053 * Return TRUE if file encoding "fenc" requires conversion from or to
3054 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003059 int same_encoding;
3060 int enc_flags;
3061 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003063 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003064 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003065 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003066 fenc_flags = 0;
3067 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003068 else
3069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003070 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3071 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003072 enc_flags = get_fio_flags(p_enc);
3073 fenc_flags = get_fio_flags(fenc);
3074 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3075 }
3076 if (same_encoding)
3077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003078 // Specified encoding matches with 'encoding'. This requires
3079 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003080 return enc_unicode != 0;
3081 }
3082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003083 // Encodings differ. However, conversion is not needed when 'enc' is any
3084 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003085 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087
3088/*
3089 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3090 * internal conversion.
3091 * if "ptr" is an empty string, use 'encoding'.
3092 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003093 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 int prop;
3097
3098 if (*ptr == NUL)
3099 ptr = p_enc;
3100
3101 prop = enc_canon_props(ptr);
3102 if (prop & ENC_UNICODE)
3103 {
3104 if (prop & ENC_2BYTE)
3105 {
3106 if (prop & ENC_ENDIAN_L)
3107 return FIO_UCS2 | FIO_ENDIAN_L;
3108 return FIO_UCS2;
3109 }
3110 if (prop & ENC_4BYTE)
3111 {
3112 if (prop & ENC_ENDIAN_L)
3113 return FIO_UCS4 | FIO_ENDIAN_L;
3114 return FIO_UCS4;
3115 }
3116 if (prop & ENC_2WORD)
3117 {
3118 if (prop & ENC_ENDIAN_L)
3119 return FIO_UTF16 | FIO_ENDIAN_L;
3120 return FIO_UTF16;
3121 }
3122 return FIO_UTF8;
3123 }
3124 if (prop & ENC_LATIN1)
3125 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003126 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return 0;
3128}
3129
Bram Moolenaar473952e2019-09-28 16:30:04 +02003130#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131/*
3132 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3133 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3134 * Used for conversion between 'encoding' and 'fileencoding'.
3135 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003136 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003137get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 int cp;
3140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003141 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 if (!enc_utf8 && enc_codepage <= 0)
3143 return 0;
3144
3145 cp = encname2codepage(ptr);
3146 if (cp == 0)
3147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003148# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (STRCMP(ptr, "utf-8") == 0)
3150 cp = CP_UTF8;
3151 else
3152# endif
3153 return 0;
3154 }
3155 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3156}
3157#endif
3158
Bram Moolenaar473952e2019-09-28 16:30:04 +02003159#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160/*
3161 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3162 * needed for the internal conversion to/from utf-8 or latin1.
3163 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003164 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003165get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3168 && (enc_canon_props(ptr) & ENC_MACROMAN))
3169 return FIO_MACROMAN;
3170 return 0;
3171}
3172#endif
3173
3174/*
3175 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3176 * "size" must be at least 2.
3177 * Return the name of the encoding and set "*lenp" to the length.
3178 * Returns NULL when no BOM found.
3179 */
3180 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003181check_for_bom(
3182 char_u *p,
3183 long size,
3184 int *lenp,
3185 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 char *name = NULL;
3188 int len = 2;
3189
3190 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003191 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003193 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 len = 3;
3195 }
3196 else if (p[0] == 0xff && p[1] == 0xfe)
3197 {
3198 if (size >= 4 && p[2] == 0 && p[3] == 0
3199 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003201 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 len = 4;
3203 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003204 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003205 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003206 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003207 // utf-16le is preferred, it also works for ucs-2le text
3208 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 else if (p[0] == 0xfe && p[1] == 0xff
3211 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003213 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003214 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003215 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003216 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003217 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3220 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003222 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 len = 4;
3224 }
3225
3226 *lenp = len;
3227 return (char_u *)name;
3228}
3229
3230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 * Try to find a shortname by comparing the fullname with the current
3232 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003233 * Returns "full_path" or pointer into "full_path" if shortened.
3234 */
3235 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003236shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003237{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003238 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003239 char_u *p = full_path;
3240
Bram Moolenaard9462e32011-04-11 21:35:11 +02003241 dirname = alloc(MAXPATHL);
3242 if (dirname == NULL)
3243 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003244 if (mch_dirname(dirname, MAXPATHL) == OK)
3245 {
3246 p = shorten_fname(full_path, dirname);
3247 if (p == NULL || *p == NUL)
3248 p = full_path;
3249 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003250 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003251 return p;
3252}
3253
3254/*
3255 * Try to find a shortname by comparing the fullname with the current
3256 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 * Returns NULL if not shorter name possible, pointer into "full_path"
3258 * otherwise.
3259 */
3260 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003261shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 int len;
3264 char_u *p;
3265
3266 if (full_path == NULL)
3267 return NULL;
3268 len = (int)STRLEN(dir_name);
3269 if (fnamencmp(dir_name, full_path, len) == 0)
3270 {
3271 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003272#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003274 * MS-Windows: when a file is in the root directory, dir_name will end
3275 * in a slash, since C: by itself does not define a specific dir. In
3276 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 if (!((len > 2) && (*(p - 2) == ':')))
3279#endif
3280 {
3281 if (vim_ispathsep(*p))
3282 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003283#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 else
3285 p = NULL;
3286#endif
3287 }
3288 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003289#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 /*
3291 * When using a file in the current drive, remove the drive name:
3292 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3293 * a floppy from "A:\dir" to "B:\dir".
3294 */
3295 else if (len > 3
3296 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3297 && full_path[1] == ':'
3298 && vim_ispathsep(full_path[2]))
3299 p = full_path + 2;
3300#endif
3301 else
3302 p = NULL;
3303 return p;
3304}
3305
3306/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003307 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * When "force" is TRUE: Use full path from now on for files currently being
3309 * edited, both for file name and swap file name. Try to shorten the file
3310 * names a bit, if safe to do so.
3311 * When "force" is FALSE: Only try to shorten absolute file names.
3312 * For buffers that have buftype "nofile" or "scratch": never change the file
3313 * name.
3314 */
3315 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003316shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3317{
3318 char_u *p;
3319
3320 if (buf->b_fname != NULL
3321#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003322 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003323#endif
3324 && !path_with_url(buf->b_fname)
3325 && (force
3326 || buf->b_sfname == NULL
3327 || mch_isFullName(buf->b_sfname)))
3328 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003329 if (buf->b_sfname != buf->b_ffname)
3330 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003331 p = shorten_fname(buf->b_ffname, dirname);
3332 if (p != NULL)
3333 {
3334 buf->b_sfname = vim_strsave(p);
3335 buf->b_fname = buf->b_sfname;
3336 }
3337 if (p == NULL || buf->b_fname == NULL)
3338 buf->b_fname = buf->b_ffname;
3339 }
3340}
3341
3342/*
3343 * Shorten filenames for all buffers.
3344 */
3345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003346shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 char_u dirname[MAXPATHL];
3349 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003352 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003354 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356 // Always make the swap file name a full path, a "nofile" buffer may
3357 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003358 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003361 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003362#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003363 popup_update_preview_title();
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365}
3366
3367#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3368 || defined(FEAT_GUI_MSWIN) \
3369 || defined(FEAT_GUI_MAC) \
3370 || defined(PROTO)
3371/*
3372 * Shorten all filenames in "fnames[count]" by current directory.
3373 */
3374 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003375shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 int i;
3378 char_u dirname[MAXPATHL];
3379 char_u *p;
3380
3381 if (fnames == NULL || count < 1)
3382 return;
3383 mch_dirname(dirname, sizeof(dirname));
3384 for (i = 0; i < count; ++i)
3385 {
3386 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003388 // shorten_fname() returns pointer in given "fnames[i]". If free
3389 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3390 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 p = vim_strsave(p);
3392 vim_free(fnames[i]);
3393 fnames[i] = p;
3394 }
3395 }
3396}
3397#endif
3398
3399/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003400 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * fo_o_h.ext for MSDOS or when shortname option set.
3402 *
3403 * Assumed that fname is a valid name found in the filesystem we assure that
3404 * the return value is a different name and ends in 'ext'.
3405 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3406 * characters otherwise.
3407 * Space for the returned name is allocated, must be freed later.
3408 * Returns NULL when out of memory.
3409 */
3410 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003411modname(
3412 char_u *fname,
3413 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003416 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 fname, ext, prepend_dot);
3418}
3419
3420 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003421buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003422 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003423 char_u *fname,
3424 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003425 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 char_u *retval;
3428 char_u *s;
3429 char_u *e;
3430 char_u *ptr;
3431 int fnamelen, extlen;
3432
3433 extlen = (int)STRLEN(ext);
3434
3435 /*
3436 * If there is no file name we must get the name of the current directory
3437 * (we need the full path in case :cd is used).
3438 */
3439 if (fname == NULL || *fname == NUL)
3440 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003441 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (retval == NULL)
3443 return NULL;
3444 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3445 (fnamelen = (int)STRLEN(retval)) == 0)
3446 {
3447 vim_free(retval);
3448 return NULL;
3449 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003450 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 retval[fnamelen++] = PATHSEP;
3453 retval[fnamelen] = NUL;
3454 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 else
3458 {
3459 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003460 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (retval == NULL)
3462 return NULL;
3463 STRCPY(retval, fname);
3464#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#endif
3467 }
3468
3469 /*
3470 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3471 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3472 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3473 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3474 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003475 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003477 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003478 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003481 {
3482 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003487 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3489 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 s = ptr + STRLEN(ptr);
3492
3493 /*
3494 * For 8.3 file names we may have to reduce the length.
3495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 /*
3499 * If there is no file name, or the file name ends in '/', and the
3500 * extension starts with '.', put a '_' before the dot, because just
3501 * ".ext" is invalid.
3502 */
3503 if (fname == NULL || *fname == NUL
3504 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 *s++ = '_';
3508 }
3509 /*
3510 * If the extension starts with '.', truncate the base name at 8
3511 * characters
3512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003515 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 s = ptr + 8;
3518 *s = '\0';
3519 }
3520 }
3521 /*
3522 * If the extension doesn't start with '.', and the file name
3523 * doesn't have an extension yet, append a '.'
3524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 else if ((e = vim_strchr(ptr, '.')) == NULL)
3526 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /*
3528 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003529 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
3531 else if ((int)STRLEN(e) + extlen > 4)
3532 s = e + 4 - extlen;
3533 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003534#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /*
3536 * If there is no file name, and the extension starts with '.', put a
3537 * '_' before the dot, because just ".ext" may be invalid if it's on a
3538 * FAT partition, and on HPFS it doesn't matter.
3539 */
3540 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3541 *s++ = '_';
3542#endif
3543
3544 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003545 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * ext can start with '.' and cannot exceed 3 more characters.
3547 */
3548 STRCPY(s, ext);
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
3551 * Prepend the dot.
3552 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003553 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003555 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /*
3560 * Check that, after appending the extension, the file name is really
3561 * different.
3562 */
3563 if (fname != NULL && STRCMP(fname, retval) == 0)
3564 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003565 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 while (--s >= ptr)
3567 {
3568 if (*s != '_')
3569 {
3570 *s = '_';
3571 break;
3572 }
3573 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003574 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 *ptr = 'v';
3576 }
3577 return retval;
3578}
3579
3580/*
3581 * Like fgets(), but if the file line is too long, it is truncated and the
3582 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003583 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 */
3585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003586vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
3588 char *eof;
3589#define FGETS_SIZE 200
3590 char tbuf[FGETS_SIZE];
3591
3592 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003596 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003598 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 do
3600 {
3601 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003602 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3604 }
3605 return (eof == NULL);
3606}
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
3609 * rename() only works if both files are on the same file system, this
3610 * function will (attempts to?) copy the file across if rename fails -- webb
3611 * Return -1 for failure, 0 for success.
3612 */
3613 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003614vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 int fd_in;
3617 int fd_out;
3618 int n;
3619 char *errmsg = NULL;
3620 char *buffer;
3621#ifdef AMIGA
3622 BPTR flock;
3623#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003624 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003625 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003626#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003627 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003628#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003629 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003632 * When the names are identical, there is nothing to do. When they refer
3633 * to the same file (ignoring case and slash/backslash differences) but
3634 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 */
3636 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003637 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003638 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003639 use_tmp_file = TRUE;
3640 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003641 return 0;
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 /*
3645 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3646 */
3647 if (mch_stat((char *)from, &st) < 0)
3648 return -1;
3649
Bram Moolenaar3576da72008-12-30 15:15:57 +00003650#ifdef UNIX
3651 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003652 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 // It's possible for the source and destination to be the same file.
3655 // This happens when "from" and "to" differ in case and are on a FAT32
3656 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003657 if (mch_stat((char *)to, &st_to) >= 0
3658 && st.st_dev == st_to.st_dev
3659 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003660 use_tmp_file = TRUE;
3661 }
3662#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003663#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003664 {
3665 BY_HANDLE_FILE_INFORMATION info1, info2;
3666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003667 // It's possible for the source and destination to be the same file.
3668 // In that case go through a temp file name. This makes rename("foo",
3669 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003670 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3671 && win32_fileinfo(to, &info2) == FILEINFO_OK
3672 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3673 && info1.nFileIndexHigh == info2.nFileIndexHigh
3674 && info1.nFileIndexLow == info2.nFileIndexLow)
3675 use_tmp_file = TRUE;
3676 }
3677#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003678
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003679 if (use_tmp_file)
3680 {
3681 char tempname[MAXPATHL + 1];
3682
3683 /*
3684 * Find a name that doesn't exist and is in the same directory.
3685 * Rename "from" to "tempname" and then rename "tempname" to "to".
3686 */
3687 if (STRLEN(from) >= MAXPATHL - 5)
3688 return -1;
3689 STRCPY(tempname, from);
3690 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003691 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003692 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3693 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003694 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003695 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003696 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003697 if (mch_rename(tempname, (char *)to) == 0)
3698 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003699 // Strange, the second step failed. Try moving the
3700 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003701 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003702 return -1;
3703 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003704 // If it fails for one temp name it will most likely fail
3705 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003706 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003707 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003708 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003709 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003710 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 /*
3713 * Delete the "to" file, this is required on some systems to make the
3714 * mch_rename() work, on other systems it makes sure that we don't have
3715 * two files when the mch_rename() fails.
3716 */
3717
3718#ifdef AMIGA
3719 /*
3720 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3721 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003722 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * deleting the "from" file (horror!) we lock it during the remove.
3724 *
3725 * When used for making a backup before writing the file: This should not
3726 * happen with ":w", because startscript() should detect this problem and
3727 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3728 * name. This problem does exist with ":w filename", but then the
3729 * original file will be somewhere else so the backup isn't really
3730 * important. If autoscripting is off the rename may fail.
3731 */
3732 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3733#endif
3734 mch_remove(to);
3735#ifdef AMIGA
3736 if (flock)
3737 UnLock(flock);
3738#endif
3739
3740 /*
3741 * First try a normal rename, return if it works.
3742 */
3743 if (mch_rename((char *)from, (char *)to) == 0)
3744 return 0;
3745
3746 /*
3747 * Rename() failed, try copying the file.
3748 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003749 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003750#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003751 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003752 acl = mch_get_acl(from);
3753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3755 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003756 {
3757#ifdef HAVE_ACL
3758 mch_free_acl(acl);
3759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003761 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003763 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003764 fd_out = mch_open((char *)to,
3765 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (fd_out == -1)
3767 {
3768 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003769#ifdef HAVE_ACL
3770 mch_free_acl(acl);
3771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return -1;
3773 }
3774
Bram Moolenaar473952e2019-09-28 16:30:04 +02003775 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (buffer == NULL)
3777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003779 close(fd_in);
3780#ifdef HAVE_ACL
3781 mch_free_acl(acl);
3782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return -1;
3784 }
3785
Bram Moolenaar473952e2019-09-28 16:30:04 +02003786 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003787 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
3789 errmsg = _("E208: Error writing to \"%s\"");
3790 break;
3791 }
3792
3793 vim_free(buffer);
3794 close(fd_in);
3795 if (close(fd_out) < 0)
3796 errmsg = _("E209: Error closing \"%s\"");
3797 if (n < 0)
3798 {
3799 errmsg = _("E210: Error reading \"%s\"");
3800 to = from;
3801 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003802#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003803 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003804#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003805#ifdef HAVE_ACL
3806 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003807 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003808#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003809#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003810 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (errmsg != NULL)
3813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003814 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return -1;
3816 }
3817 mch_remove(from);
3818 return 0;
3819}
3820
3821static int already_warned = FALSE;
3822
3823/*
3824 * Check if any not hidden buffer has been changed.
3825 * Postpone the check if there are characters in the stuff buffer, a global
3826 * command is being executed, a mapping is being executed or an autocommand is
3827 * busy.
3828 * Returns TRUE if some message was written (screen should be redrawn and
3829 * cursor positioned).
3830 */
3831 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003832check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003833 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 buf_T *buf;
3836 int didit = 0;
3837 int n;
3838
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003839 // Don't check timestamps while system() or another low-level function may
3840 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (no_check_timestamps > 0)
3842 return FALSE;
3843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003844 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3845 // event and we would keep on checking if the file is steadily growing.
3846 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (focus && did_check_timestamps)
3848 {
3849 need_check_timestamps = TRUE;
3850 return FALSE;
3851 }
3852
3853 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003854 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003855 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 else
3857 {
3858 ++no_wait_return;
3859 did_check_timestamps = TRUE;
3860 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003861 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003863 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (buf->b_nwindows > 0)
3865 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003866 bufref_T bufref;
3867
3868 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 n = buf_check_timestamp(buf, focus);
3870 if (didit < n)
3871 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003872 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003874 // Autocommands have removed the buffer, start at the
3875 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 buf = firstbuf;
3877 continue;
3878 }
3879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 --no_wait_return;
3882 need_check_timestamps = FALSE;
3883 if (need_wait_return && didit == 2)
3884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003885 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003886 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 out_flush();
3888 }
3889 }
3890 return didit;
3891}
3892
3893/*
3894 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3895 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3896 * empty.
3897 */
3898 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003899move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 buf_T *tbuf = curbuf;
3902 int retval = OK;
3903 linenr_T lnum;
3904 char_u *p;
3905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 curbuf = tobuf;
3908 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3909 {
3910 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3911 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3912 {
3913 vim_free(p);
3914 retval = FAIL;
3915 break;
3916 }
3917 vim_free(p);
3918 }
3919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (retval != FAIL)
3922 {
3923 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003924 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
3925 if (ml_delete(lnum, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003927 // Oops! We could try putting back the saved lines, but that
3928 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 retval = FAIL;
3930 break;
3931 }
3932 }
3933
3934 curbuf = tbuf;
3935 return retval;
3936}
3937
3938/*
3939 * Check if buffer "buf" has been changed.
3940 * Also check if the file for a new buffer unexpectedly appeared.
3941 * return 1 if a changed buffer was found.
3942 * return 2 if a message has been displayed.
3943 * return 0 otherwise.
3944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003946buf_check_timestamp(
3947 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003948 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003950 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 int stat_res;
3952 int retval = 0;
3953 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003954 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003956 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 int helpmesg = FALSE;
3958 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003959 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3961 int can_reload = FALSE;
3962#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003963 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 int orig_mode = buf->b_orig_mode;
3965#ifdef FEAT_GUI
3966 int save_mouse_correct = need_mouse_correct;
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003969 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003970#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003971 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003972#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003973 bufref_T bufref;
3974
3975 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 // If there is no file name, the buffer is not loaded, 'buftype' is
3978 // set, we are in the middle of a save or being called recursively: ignore
3979 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (buf->b_ffname == NULL
3981 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02003982 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00003985#ifdef FEAT_NETBEANS_INTG
3986 || isNetbeansBuffer(buf)
3987#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02003988#ifdef FEAT_TERMINAL
3989 || buf->b_term != NULL
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 )
3992 return 0;
3993
3994 if ( !(buf->b_flags & BF_NOTEDITED)
3995 && buf->b_mtime != 0
3996 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
3997 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02003998 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#ifdef HAVE_ST_MODE
4000 || (int)st.st_mode != buf->b_orig_mode
4001#else
4002 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4003#endif
4004 ))
4005 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004006 long prev_b_mtime = buf->b_mtime;
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 retval = 1;
4009
Bram Moolenaar386bc822018-07-07 18:34:12 +02004010 // set b_mtime to stop further warnings (e.g., when executing
4011 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (stat_res < 0)
4013 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004014 // Check the file again later to see if it re-appears.
4015 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 buf->b_orig_size = 0;
4017 buf->b_orig_mode = 0;
4018 }
4019 else
4020 buf_store_time(buf, &st, buf->b_ffname);
4021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004022 // Don't do anything for a directory. Might contain the file
4023 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (mch_isdir(buf->b_fname))
4025 ;
4026
4027 /*
4028 * If 'autoread' is set, the buffer has no changes and the file still
4029 * exists, reload the buffer. Use the buffer-local option value if it
4030 * was set, the global option value otherwise.
4031 */
4032 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4033 && !bufIsChanged(buf) && stat_res >= 0)
4034 reload = TRUE;
4035 else
4036 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004037 if (stat_res < 0)
4038 reason = "deleted";
4039 else if (bufIsChanged(buf))
4040 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004041 /*
4042 * Check if the file contents really changed to avoid giving a
4043 * warning when only the timestamp was set (e.g., checked out of
4044 * CVS). Always warn when the buffer was changed.
4045 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004046 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4047 reason = "changed";
4048 else if (orig_mode != buf->b_orig_mode)
4049 reason = "mode";
4050 else
4051 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 /*
4054 * Only give the warning if there are no FileChangedShell
4055 * autocommands.
4056 * Avoid being called recursively by setting "busy".
4057 */
4058 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004059#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004060 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4061 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004062#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004063 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4065 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004066 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 busy = FALSE;
4068 if (n)
4069 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004070 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004071 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004072#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004073 s = get_vim_var_str(VV_FCS_CHOICE);
4074 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4075 reload = TRUE;
4076 else if (STRCMP(s, "ask") == 0)
4077 n = FALSE;
4078 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004079#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004080 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004082 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004084 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004085 {
4086 // Only give the message once.
4087 if (prev_b_mtime != -1)
4088 mesg = _("E211: File \"%s\" no longer available");
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 {
4092 helpmesg = TRUE;
4093#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4094 can_reload = TRUE;
4095#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004096 if (reason[2] == 'n')
4097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004099 mesg2 = _("See \":help W12\" for more info.");
4100 }
4101 else if (reason[1] == 'h')
4102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004104 mesg2 = _("See \":help W11\" for more info.");
4105 }
4106 else if (*reason == 'm')
4107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004109 mesg2 = _("See \":help W16\" for more info.");
4110 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004111 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004112 // Only timestamp changed, store it to avoid a warning
4113 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004114 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117 }
4118
4119 }
4120 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4121 && vim_fexists(buf->b_ffname))
4122 {
4123 retval = 1;
4124 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4125 buf->b_flags |= BF_NEW_W;
4126#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4127 can_reload = TRUE;
4128#endif
4129 }
4130
4131 if (mesg != NULL)
4132 {
4133 path = home_replace_save(buf, buf->b_fname);
4134 if (path != NULL)
4135 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004136 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004138 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004139 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004140#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004141 // Set warningmsg here, before the unimportant and output-specific
4142 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004143 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4146 if (can_reload)
4147 {
4148 if (*mesg2 != NUL)
4149 {
4150 STRCAT(tbuf, "\n");
4151 STRCAT(tbuf, mesg2);
4152 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004153 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4154 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004155 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 reload = TRUE;
4157 }
4158 else
4159#endif
4160 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4161 {
4162 if (*mesg2 != NUL)
4163 {
4164 STRCAT(tbuf, "; ");
4165 STRCAT(tbuf, mesg2);
4166 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 retval = 2;
4169 }
4170 else
4171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
4174 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004177 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 msg_clr_eos();
4179 (void)msg_end();
4180 if (emsg_silent == 0)
4181 {
4182 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004183#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004185#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004187 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004189 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 redraw_cmdline = FALSE;
4191 }
4192 }
4193 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195
4196 vim_free(path);
4197 vim_free(tbuf);
4198 }
4199 }
4200
4201 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004203 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004204 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004205#ifdef FEAT_PERSISTENT_UNDO
4206 if (buf->b_p_udf && buf->b_ffname != NULL)
4207 {
4208 char_u hash[UNDO_HASH_SIZE];
4209 buf_T *save_curbuf = curbuf;
4210
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004212 curbuf = buf;
4213 u_compute_hash(hash);
4214 u_write_undo(NULL, FALSE, buf, hash);
4215 curbuf = save_curbuf;
4216 }
4217#endif
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004220 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004221 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004222 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4223 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004225 // restore this in case an autocommand has set it; it would break
4226 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 need_mouse_correct = save_mouse_correct;
4228#endif
4229
4230 return retval;
4231}
4232
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004233/*
4234 * Reload a buffer that is already loaded.
4235 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004236 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4237 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004238 */
4239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004240buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004241{
4242 exarg_T ea;
4243 pos_T old_cursor;
4244 linenr_T old_topline;
4245 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004246 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004247 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004248 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004249 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004250 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004252 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004253 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004255 // We only want to read the text from the file, not reset the syntax
4256 // highlighting, clear marks, diff status, etc. Force the fileformat
4257 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004258 if (prep_exarg(&ea, buf) == OK)
4259 {
4260 old_cursor = curwin->w_cursor;
4261 old_topline = curwin->w_topline;
4262
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004263 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004265 // Save all the text, so that the reload can be undone.
4266 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004267 u_sync(FALSE);
4268 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4269 flags |= READ_KEEP_UNDO;
4270 }
4271
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004272 /*
4273 * To behave like when a new file is edited (matters for
4274 * BufReadPost autocommands) we first need to delete the current
4275 * buffer contents. But if reading the file fails we should keep
4276 * the old contents. Can't use memory only, the file might be
4277 * too big. Use a hidden buffer to move the buffer contents to.
4278 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004279 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004280 savebuf = NULL;
4281 else
4282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004283 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004284 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004285 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004286 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004289 curbuf = savebuf;
4290 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004291 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004292 curbuf = buf;
4293 curwin->w_buffer = buf;
4294 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004295 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004296 || move_lines(buf, savebuf) == FAIL)
4297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004298 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004299 buf->b_fname);
4300 saved = FAIL;
4301 }
4302 }
4303
4304 if (saved == OK)
4305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4307 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004308 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4309 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004310 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004311 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004312#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004313 if (!aborting())
4314#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004315 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004316 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 // Put the text back from the save buffer. First
4319 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004320 while (!BUFEMPTY())
Bram Moolenaar8424a622006-04-19 21:23:36 +00004321 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322 break;
4323 (void)move_lines(savebuf, buf);
4324 }
4325 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004326 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004329 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004330 if ((flags & READ_KEEP_UNDO) == 0)
4331 {
4332 u_blockfree(buf);
4333 u_clearall(buf);
4334 }
4335 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004338 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004339 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340 }
4341 }
4342 vim_free(ea.cmd);
4343
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004344 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 wipe_buffer(savebuf, FALSE);
4346
4347#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004348 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004349 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004350#endif
4351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 // Restore the topline and cursor position and check it (lines may
4353 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004354 if (old_topline > curbuf->b_ml.ml_line_count)
4355 curwin->w_topline = curbuf->b_ml.ml_line_count;
4356 else
4357 curwin->w_topline = old_topline;
4358 curwin->w_cursor = old_cursor;
4359 check_cursor();
4360 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362#ifdef FEAT_FOLDING
4363 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004364 win_T *wp;
4365 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004368 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 if (wp->w_buffer == curwin->w_buffer
4370 && !foldmethodIsManual(wp))
4371 foldUpdateAll(wp);
4372 }
4373#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 // If the mode didn't change and 'readonly' was set, keep the old
4375 // value; the user probably used the ":view" command. But don't
4376 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377 if (orig_mode == curbuf->b_orig_mode)
4378 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004381 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004382 }
4383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004387}
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004390buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004393 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifdef HAVE_ST_MODE
4395 buf->b_orig_mode = (int)st->st_mode;
4396#else
4397 buf->b_orig_mode = mch_getperm(fname);
4398#endif
4399}
4400
4401/*
4402 * Adjust the line with missing eol, used for the next write.
4403 * Used for do_filter(), when the input lines for the filter are deleted.
4404 */
4405 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004406write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004408 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004409 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410}
4411
Bram Moolenaarda440d22016-01-16 21:27:23 +01004412#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4413/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004414 * Core part of "readdir()" function.
4415 * Retrieve the list of files/directories of "path" into "gap".
4416 * Return OK for success, FAIL for failure.
4417 */
4418 int
4419readdir_core(
4420 garray_T *gap,
4421 char_u *path,
4422 void *context,
4423 int (*checkitem)(void *context, char_u *name))
4424{
4425 int failed = FALSE;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004426# ifdef MSWIN
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004427 char_u *buf, *p;
4428 int ok;
4429 HANDLE hFind = INVALID_HANDLE_VALUE;
4430 WIN32_FIND_DATAW wfb;
4431 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004432# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004433
4434 ga_init2(gap, (int)sizeof(char *), 20);
4435
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004436# ifdef MSWIN
Bram Moolenaar51e14382019-05-25 20:21:28 +02004437 buf = alloc(MAXPATHL);
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004438 if (buf == NULL)
4439 return FAIL;
4440 STRNCPY(buf, path, MAXPATHL-5);
Bram Moolenaar71de7202019-05-24 19:04:29 +02004441 p = buf + STRLEN(buf);
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004442 MB_PTR_BACK(buf, p);
4443 if (*p == '\\' || *p == '/')
4444 *p = NUL;
4445 STRCAT(buf, "\\*");
4446
4447 wn = enc_to_utf16(buf, NULL);
4448 if (wn != NULL)
4449 hFind = FindFirstFileW(wn, &wfb);
4450 ok = (hFind != INVALID_HANDLE_VALUE);
4451 if (!ok)
4452 {
4453 failed = TRUE;
4454 smsg(_(e_notopen), path);
4455 }
4456 else
4457 {
4458 while (ok)
4459 {
4460 int ignore;
4461
4462 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
4463 if (p == NULL)
4464 break; // out of memory
4465
4466 ignore = p[0] == '.' && (p[1] == NUL
4467 || (p[1] == '.' && p[2] == NUL));
4468 if (!ignore && checkitem != NULL)
4469 {
4470 int r = checkitem(context, p);
4471
4472 if (r < 0)
4473 {
4474 vim_free(p);
4475 break;
4476 }
4477 if (r == 0)
4478 ignore = TRUE;
4479 }
4480
4481 if (!ignore)
4482 {
4483 if (ga_grow(gap, 1) == OK)
4484 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p);
4485 else
4486 {
4487 failed = TRUE;
4488 vim_free(p);
4489 break;
4490 }
4491 }
4492
4493 vim_free(p);
4494 ok = FindNextFileW(hFind, &wfb);
4495 }
4496 FindClose(hFind);
4497 }
4498
4499 vim_free(buf);
4500 vim_free(wn);
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004501# else
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004502 DIR *dirp;
4503 struct dirent *dp;
4504 char_u *p;
4505
4506 dirp = opendir((char *)path);
4507 if (dirp == NULL)
4508 {
4509 failed = TRUE;
4510 smsg(_(e_notopen), path);
4511 }
4512 else
4513 {
4514 for (;;)
4515 {
4516 int ignore;
4517
4518 dp = readdir(dirp);
4519 if (dp == NULL)
4520 break;
4521 p = (char_u *)dp->d_name;
4522
4523 ignore = p[0] == '.' &&
4524 (p[1] == NUL ||
4525 (p[1] == '.' && p[2] == NUL));
4526 if (!ignore && checkitem != NULL)
4527 {
4528 int r = checkitem(context, p);
4529
4530 if (r < 0)
4531 break;
4532 if (r == 0)
4533 ignore = TRUE;
4534 }
4535
4536 if (!ignore)
4537 {
4538 if (ga_grow(gap, 1) == OK)
4539 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p);
4540 else
4541 {
4542 failed = TRUE;
4543 break;
4544 }
4545 }
4546 }
4547
4548 closedir(dirp);
4549 }
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004550# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004551
4552 if (!failed && gap->ga_len > 0)
4553 sort_strings((char_u **)gap->ga_data, gap->ga_len);
4554
4555 return failed ? FAIL : OK;
4556}
4557
4558/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004559 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004560 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004561 */
4562 int
4563delete_recursive(char_u *name)
4564{
4565 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004566 int i;
4567 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004568 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004569
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004570 // A symbolic link to a directory itself is deleted, not the directory it
4571 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004572 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004573# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004574 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004575# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004576 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004577# endif
4578 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004579 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004580 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004581 if (exp == NULL)
4582 return -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004583 if (readdir_core(&ga, exp, NULL, NULL) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004584 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004585 for (i = 0; i < ga.ga_len; ++i)
4586 {
4587 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4588 ((char_u **)ga.ga_data)[i]);
4589 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004590 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004591 }
4592 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004593 }
4594 else
4595 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004596 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004597 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004598 }
4599 else
4600 result = mch_remove(name) == 0 ? 0 : -1;
4601
4602 return result;
4603}
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004607static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609/*
4610 * Delete the temp directory and all files it contains.
4611 */
4612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (vim_tempdir != NULL)
4616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004617 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004618 gettail(vim_tempdir)[-1] = NUL;
4619 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004620 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004625 * Directory "tempdir" was created. Expand this name to a full path and put
4626 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4627 * "tempdir" must be no longer than MAXPATHL.
4628 */
4629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004631{
4632 char_u *buf;
4633
Bram Moolenaar964b3742019-05-24 18:54:09 +02004634 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004635 if (buf != NULL)
4636 {
4637 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
4638 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004639 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004640 vim_tempdir = vim_strsave(buf);
4641 vim_free(buf);
4642 }
4643}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00004644#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004645
4646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 * vim_tempname(): Return a unique name that can be used for a temp file.
4648 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02004649 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
4650 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 *
4652 * The returned pointer is to allocated memory.
4653 * The returned pointer is NULL if no valid name was found.
4654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004656vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004657 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004658 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
4660#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01004662#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004663 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#else
4665 char_u itmp[TEMPNAMELEN];
4666#endif
4667
4668#ifdef TEMPDIRNAMES
4669 static char *(tempdirs[]) = {TEMPDIRNAMES};
4670 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02004672 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673# endif
4674
4675 /*
4676 * This will create a directory for private use by this instance of Vim.
4677 * This is done once, and the same directory is used for all temp files.
4678 * This method avoids security problems because of symlink attacks et al.
4679 * It's also a bit faster, because we only need to check for an existing
4680 * file when creating the directory and not for each temp file.
4681 */
4682 if (vim_tempdir == NULL)
4683 {
4684 /*
4685 * Try the entries in TEMPDIRNAMES to create the temp directory.
4686 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00004687 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00004689# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004690 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004691 long nr;
4692 long off;
4693# endif
4694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004695 // Expand $TMP, leave room for "/v1100000/999999999".
4696 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01004698 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004700 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004701 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaareaf03392009-11-17 11:08:52 +00004703# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004704 {
4705# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004706 // Make sure the umask doesn't remove the executable bit.
4707 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004708 mode_t umask_save = umask(077);
4709# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004710 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004711 STRCAT(itmp, "vXXXXXX");
4712 if (mkdtemp((char *)itmp) != NULL)
4713 vim_settempdir(itmp);
4714# if defined(UNIX) || defined(VMS)
4715 (void)umask(umask_save);
4716# endif
4717 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00004718# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004719 // Get an arbitrary number of up to 6 digits. When it's
4720 // unlikely that it already exists it will be faster,
4721 // otherwise it doesn't matter. The use of mkdir() avoids any
4722 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004724 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004726 // Try up to 10000 different values until we find a name that
4727 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 for (off = 0; off < 10000L; ++off)
4729 {
4730 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004731# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaareaf03392009-11-17 11:08:52 +00004735 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
4736# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004737 // If mkdir() does not set errno to EEXIST, check for
4738 // existing file here. There is a race condition then,
4739 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (mch_stat((char *)itmp, &st) >= 0)
4741 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004742# endif
4743# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004744 // Make sure the umask doesn't remove the executable bit.
4745 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004749# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (r == 0)
4753 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00004754 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 break;
4756 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00004757# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004758 // If the mkdir() didn't fail because the file/dir exists,
4759 // we probably can't create any dir here, try another
4760 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004765# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (vim_tempdir != NULL)
4767 break;
4768 }
4769 }
4770 }
4771
4772 if (vim_tempdir != NULL)
4773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004774 // There is no need to check if the file exists, because we own the
4775 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
4777 return vim_strsave(itmp);
4778 }
4779
4780 return NULL;
4781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004782#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaar4f974752019-02-17 17:44:42 +01004784# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004785 WCHAR wszTempFile[_MAX_PATH + 1];
4786 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u *retval;
4788 char_u *p;
4789
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004790 wcscpy(itmp, L"");
4791 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01004792 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004793 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
4794 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01004795 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004796 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004798 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004800 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004801 // GetTempFileName() will create the file, we don't want that
4802 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004804 // Backslashes in a temp file name cause problems when filtering with
4805 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
4806 // didn't set 'shellslash'.
4807 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (*p_shcf == '-' || p_ssl)
4809 for (p = retval; *p; ++p)
4810 if (*p == '\\')
4811 *p = '/';
4812 return retval;
4813
Bram Moolenaar4f974752019-02-17 17:44:42 +01004814# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01004817 char_u *p;
4818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004819 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01004820 p = tmpnam((char *)itmp);
4821 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return NULL;
4823# else
4824 char_u *p;
4825
4826# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004827 // mktemp() is not working on VMS. It seems to be
4828 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 sprintf((char *)itmp, "VIM%c", extra_char);
4830 p = (char_u *)tempnam("tmp:", (char *)itmp);
4831 if (p != NULL)
4832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004833 // VMS will use '.LIS' if we don't explicitly specify an extension,
4834 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 STRCPY(itmp, p);
4836 STRCAT(itmp, ".txt");
4837 free(p);
4838 }
4839 else
4840 return NULL;
4841# else
4842 STRCPY(itmp, TEMPNAME);
4843 if ((p = vim_strchr(itmp, '?')) != NULL)
4844 *p = extra_char;
4845 if (mktemp((char *)itmp) == NULL)
4846 return NULL;
4847# endif
4848# endif
4849
4850 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01004851# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004852#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853}
4854
4855#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4856/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02004857 * Convert all backslashes in fname to forward slashes in-place, unless when
4858 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
4860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004861forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 char_u *p;
4864
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02004865 if (path_with_url(fname))
4866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004868 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004869 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01004871 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 *p = '/';
4873}
4874#endif
4875
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00004876/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
4878 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
4879 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 * Used for autocommands and 'wildignore'.
4881 * Returns TRUE if there is a match, FALSE otherwise.
4882 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004883 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004884match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 char_u *pattern, // pattern to match with
4886 regprog_T **prog, // pre-compiled regprog or NULL
4887 char_u *fname, // full path of file name
4888 char_u *sfname, // short file name or NULL
4889 char_u *tail, // tail of path
4890 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 regmatch_T regmatch;
4893 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004895 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004896 if (prog != NULL)
4897 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004899 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
4901 /*
4902 * Try for a match with the pattern with:
4903 * 1. the full file name, when the pattern has a '/'.
4904 * 2. the short file name, when the pattern has a '/'.
4905 * 3. the tail of the file name, when the pattern has no '/'.
4906 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004907 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 && ((allow_dirs
4909 && (vim_regexec(&regmatch, fname, (colnr_T)0)
4910 || (sfname != NULL
4911 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004912 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 result = TRUE;
4914
Bram Moolenaardffa5b82014-11-19 16:38:07 +01004915 if (prog != NULL)
4916 *prog = regmatch.regprog;
4917 else
Bram Moolenaar473de612013-06-08 18:19:48 +02004918 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return result;
4920}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922#if defined(FEAT_WILDIGN) || defined(PROTO)
4923/*
4924 * Return TRUE if a file matches with a pattern in "list".
4925 * "list" is a comma-separated list of patterns, like 'wildignore'.
4926 * "sfname" is the short file name or NULL, "ffname" the long file name.
4927 */
4928 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004929match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 char_u buf[100];
4932 char_u *tail;
4933 char_u *regpat;
4934 char allow_dirs;
4935 int match;
4936 char_u *p;
4937
4938 tail = gettail(sfname);
4939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004940 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 p = list;
4942 while (*p)
4943 {
4944 copy_option_part(&p, buf, 100, ",");
4945 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
4946 if (regpat == NULL)
4947 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004948 match = match_file_pat(regpat, NULL, ffname, sfname,
4949 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 vim_free(regpat);
4951 if (match)
4952 return TRUE;
4953 }
4954 return FALSE;
4955}
4956#endif
4957
4958/*
4959 * Convert the given pattern "pat" which has shell style wildcards in it, into
4960 * a regular expression, and return the result in allocated memory. If there
4961 * is a directory path separator to be matched, then TRUE is put in
4962 * allow_dirs, otherwise FALSE is put there -- webb.
4963 * Handle backslashes before special characters, like "\*" and "\ ".
4964 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * Returns NULL when out of memory.
4966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968file_pat_to_reg_pat(
4969 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004970 char_u *pat_end, // first char after pattern or NULL
4971 char *allow_dirs, // Result passed back out in here
4972 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004974 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u *endp;
4976 char_u *reg_pat;
4977 char_u *p;
4978 int i;
4979 int nested = 0;
4980 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
4982 if (allow_dirs != NULL)
4983 *allow_dirs = FALSE;
4984 if (pat_end == NULL)
4985 pat_end = pat + STRLEN(pat);
4986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 for (p = pat; p < pat_end; p++)
4988 {
4989 switch (*p)
4990 {
4991 case '*':
4992 case '.':
4993 case ',':
4994 case '{':
4995 case '}':
4996 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004997 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 break;
4999#ifdef BACKSLASH_IN_FILENAME
5000 case '\\':
5001 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004#endif
5005 default:
5006 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005007 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 ++p;
5010 ++size;
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013 }
5014 }
5015 reg_pat = alloc(size + 1);
5016 if (reg_pat == NULL)
5017 return NULL;
5018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021 if (pat[0] == '*')
5022 while (pat[0] == '*' && pat < pat_end - 1)
5023 pat++;
5024 else
5025 reg_pat[i++] = '^';
5026 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005027 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 while (endp - pat > 0 && *endp == '*')
5030 endp--;
5031 add_dollar = FALSE;
5032 }
5033 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5034 {
5035 switch (*p)
5036 {
5037 case '*':
5038 reg_pat[i++] = '.';
5039 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005041 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 case '~':
5045 reg_pat[i++] = '\\';
5046 reg_pat[i++] = *p;
5047 break;
5048 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 reg_pat[i++] = '.';
5050 break;
5051 case '\\':
5052 if (p[1] == NUL)
5053 break;
5054#ifdef BACKSLASH_IN_FILENAME
5055 if (!no_bslash)
5056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005057 // translate:
5058 // "\x" to "\\x" e.g., "dir\file"
5059 // "\*" to "\\.*" e.g., "dir\*.c"
5060 // "\?" to "\\." e.g., "dir\??.c"
5061 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5063 && p[1] != '+')
5064 {
5065 reg_pat[i++] = '[';
5066 reg_pat[i++] = '\\';
5067 reg_pat[i++] = '/';
5068 reg_pat[i++] = ']';
5069 if (allow_dirs != NULL)
5070 *allow_dirs = TRUE;
5071 break;
5072 }
5073 }
5074#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005075 // Undo escaping from ExpandEscape():
5076 // foo\?bar -> foo?bar
5077 // foo\%bar -> foo%bar
5078 // foo\,bar -> foo,bar
5079 // foo\ bar -> foo bar
5080 // Don't unescape \, * and others that are also special in a
5081 // regexp.
5082 // An escaped { must be unescaped since we use magic not
5083 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (*++p == '?'
5085#ifdef BACKSLASH_IN_FILENAME
5086 && no_bslash
5087#endif
5088 )
5089 reg_pat[i++] = '?';
5090 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005091 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005092 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005093 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005094 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5095 {
5096 reg_pat[i++] = '\\';
5097 reg_pat[i++] = '{';
5098 p += 2;
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 else
5101 {
5102 if (allow_dirs != NULL && vim_ispathsep(*p)
5103#ifdef BACKSLASH_IN_FILENAME
5104 && (!no_bslash || *p != '\\')
5105#endif
5106 )
5107 *allow_dirs = TRUE;
5108 reg_pat[i++] = '\\';
5109 reg_pat[i++] = *p;
5110 }
5111 break;
5112#ifdef BACKSLASH_IN_FILENAME
5113 case '/':
5114 reg_pat[i++] = '[';
5115 reg_pat[i++] = '\\';
5116 reg_pat[i++] = '/';
5117 reg_pat[i++] = ']';
5118 if (allow_dirs != NULL)
5119 *allow_dirs = TRUE;
5120 break;
5121#endif
5122 case '{':
5123 reg_pat[i++] = '\\';
5124 reg_pat[i++] = '(';
5125 nested++;
5126 break;
5127 case '}':
5128 reg_pat[i++] = '\\';
5129 reg_pat[i++] = ')';
5130 --nested;
5131 break;
5132 case ',':
5133 if (nested)
5134 {
5135 reg_pat[i++] = '\\';
5136 reg_pat[i++] = '|';
5137 }
5138 else
5139 reg_pat[i++] = ',';
5140 break;
5141 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005142 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005144 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 *allow_dirs = TRUE;
5146 reg_pat[i++] = *p;
5147 break;
5148 }
5149 }
5150 if (add_dollar)
5151 reg_pat[i++] = '$';
5152 reg_pat[i] = NUL;
5153 if (nested != 0)
5154 {
5155 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005156 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005158 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005159 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 return reg_pat;
5162}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005163
5164#if defined(EINTR) || defined(PROTO)
5165/*
5166 * Version of read() that retries when interrupted by EINTR (possibly
5167 * by a SIGWINCH).
5168 */
5169 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005171{
5172 long ret;
5173
5174 for (;;)
5175 {
5176 ret = vim_read(fd, buf, bufsize);
5177 if (ret >= 0 || errno != EINTR)
5178 break;
5179 }
5180 return ret;
5181}
5182
5183/*
5184 * Version of write() that retries when interrupted by EINTR (possibly
5185 * by a SIGWINCH).
5186 */
5187 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005188write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005189{
5190 long ret = 0;
5191 long wlen;
5192
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // Repeat the write() so long it didn't fail, other than being interrupted
5194 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005195 while (ret < (long)bufsize)
5196 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005197 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005198 if (wlen < 0)
5199 {
5200 if (errno != EINTR)
5201 break;
5202 }
5203 else
5204 ret += wlen;
5205 }
5206 return ret;
5207}
5208#endif