blob: 32af14c4d5c4b896b50a0c12b697563c38647fb0 [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))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200264 {
265 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200267 if (aborting())
268 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200270 // The BufReadCmd code usually uses ":read" to get the text and
271 // perhaps ":file" to change the buffer name. But we should
272 // consider this to work like ":edit", thus reset the
273 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
274 // same file.
275 if (status == OK)
276 curbuf->b_flags &= ~BF_NOTEDITED;
277 return status;
278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 }
280 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
281 FALSE, NULL, eap))
282#ifdef FEAT_EVAL
283 return aborting() ? FAIL : OK;
284#else
285 return OK;
286#endif
287
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100288 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
291 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100292 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100294 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295
296 /*
297 * If the name ends in a path separator, we can't open it. Check here,
298 * because reading the file may actually work, but then creating the swap
299 * file may destroy it! Reported on MS-DOS and Win 95.
300 * If the name is too long we might crash further on, quit here.
301 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000302 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000304 p = fname + STRLEN(fname);
305 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000306 {
307 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
308 msg_end();
309 msg_scroll = msg_save;
310 return FAIL;
311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
313
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200314 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200316#ifdef UNIX
317 /*
318 * On Unix it is possible to read a directory, so we have to
319 * check for it before the mch_open().
320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100322 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
323 && !S_ISFIFO(perm) // ... or fifo
324 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000325# ifdef OPEN_CHR_FILES
326 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100327 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 )
330 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100331 int retval = FAIL;
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100336 retval = NOTDONE;
337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 else
339 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
340 msg_end();
341 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100342 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200344#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100345#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000346 /*
347 * MS-Windows allows opening a device, but we will probably get stuck
348 * trying to read it.
349 */
350 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
351 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000352 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000353 msg_end();
354 msg_scroll = msg_save;
355 return FAIL;
356 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000357#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200358 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000359
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100360 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200361 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 /*
364 * When opening a new file we take the readonly flag from the file.
365 * Default is r/w, can be set to r/o below.
366 * Don't reset it when in readonly mode
367 * Only set/reset b_p_ro when BF_CHECK_RO is set.
368 */
369 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000370 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 curbuf->b_p_ro = FALSE;
372
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200373 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100375 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (mch_stat((char *)fname, &st) >= 0)
377 {
378 buf_store_time(curbuf, &st, fname);
379 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#ifdef UNIX
381 /*
382 * Use the protection bits of the original file for the swap file.
383 * This makes it possible for others to read the name of the
384 * edited file from the swapfile, but only if they can read the
385 * edited file.
386 * Remove the "write" and "execute" bits for group and others
387 * (they must not write the swapfile).
388 * Add the "read" and "write" bits for the user, otherwise we may
389 * not be able to write to the file ourselves.
390 * Setting the bits is done below, after creating the swap file.
391 */
392 swap_mode = (st.st_mode & 0644) | 0600;
393#endif
394#ifdef FEAT_CW_EDITOR
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 // Get the FSSpec on MacOS
396 // TODO: Update it properly when the buffer name changes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
398#endif
399#ifdef VMS
400 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000401 curbuf->b_fab_rat = st.st_fab_rat;
402 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
404 }
405 else
406 {
407 curbuf->b_mtime = 0;
408 curbuf->b_mtime_read = 0;
409 curbuf->b_orig_size = 0;
410 curbuf->b_orig_mode = 0;
411 }
412
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100413 // Reset the "new file" flag. It will be set again below when the
414 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
416 }
417
418/*
419 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100420 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 */
422 file_readonly = FALSE;
423 if (read_stdin)
424 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100425#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100426 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 setmode(0, O_BINARY);
428#endif
429 }
430 else if (!read_buffer)
431 {
432#ifdef USE_MCH_ACCESS
433 if (
434# ifdef UNIX
435 !(perm & 0222) ||
436# endif
437 mch_access((char *)fname, W_OK))
438 file_readonly = TRUE;
439 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
440#else
441 if (!newfile
442 || readonlymode
443 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
444 {
445 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100446 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
448 }
449#endif
450 }
451
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100452 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
454#ifndef UNIX
455 int isdir_f;
456#endif
457 msg_scroll = msg_save;
458#ifndef UNIX
459 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100460 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 */
462 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100463 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 if (isdir_f)
465 {
466 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100467 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 }
469 else
470#endif
471 if (newfile)
472 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200473 if (perm < 0
474#ifdef ENOENT
475 && errno == ENOENT
476#endif
477 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {
479 /*
480 * Set the 'new-file' flag, so that when the file has
481 * been created by someone else, a ":w" will complain.
482 */
483 curbuf->b_flags |= BF_NEW;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // Create a swap file now, so that other Vims are warned
486 // that we are editing this file. Don't do this for a
487 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#ifdef FEAT_QUICKFIX
489 if (!bt_dontwrite(curbuf))
490#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000494 if (curbuf != old_curbuf
495 || (using_b_ffname
496 && (old_b_ffname != curbuf->b_ffname))
497 || (using_b_fname
498 && (old_b_fname != curbuf->b_fname)))
499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100500 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000501 return FAIL;
502 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000503 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000504 if (dir_of_file_exists(fname))
505 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
506 else
507 filemess(curbuf, sfname,
508 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100510 // Even though this is a new file, it might have been
511 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 check_marks_read();
513#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100514 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200515 if (eap != NULL)
516 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
518 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100519 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 save_file_ff(curbuf);
521
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100522#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 return FAIL;
525#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100526 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528 else
529 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000530 filemess(curbuf, sfname, (char_u *)(
531# ifdef EFBIG
532 (errno == EFBIG) ? _("[File too big]") :
533# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200534# ifdef EOVERFLOW
535 (errno == EOVERFLOW) ? _("[File too big]") :
536# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000537 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 }
541
542 return FAIL;
543 }
544
545 /*
546 * Only set the 'ro' flag for readonly files the first time they are
547 * loaded. Help files always get readonly mode
548 */
549 if ((check_readonly && file_readonly) || curbuf->b_help)
550 curbuf->b_p_ro = TRUE;
551
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000552 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100554 // Don't change 'eol' if reading from buffer as it will already be
555 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000556 if (!read_buffer)
557 {
558 curbuf->b_p_eol = TRUE;
559 curbuf->b_start_eol = TRUE;
560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000562 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100565 // Create a swap file now, so that other Vims are warned that we are
566 // editing this file.
567 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#ifdef FEAT_QUICKFIX
569 if (!bt_dontwrite(curbuf))
570#endif
571 {
572 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000573 if (!read_stdin && (curbuf != old_curbuf
574 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
575 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100577 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000578 if (!read_buffer)
579 close(fd);
580 return FAIL;
581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100583 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000584 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
585 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100586 {
587 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
588
589 /*
590 * If the group-read bit is set but not the world-read bit, then
591 * the group must be equal to the group of the original file. If
592 * we can't make that happen then reset the group-read bit. This
593 * avoids making the swap file readable to more users when the
594 * primary group of the user is too permissive.
595 */
596 if ((swap_mode & 044) == 040)
597 {
598 stat_T swap_st;
599
600 if (mch_stat((char *)swap_fname, &swap_st) >= 0
601 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200602# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100603 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200604 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200605# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200606 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100607 swap_mode &= 0600;
608 }
609
610 (void)mch_setperm(swap_fname, (long)swap_mode);
611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#endif
613 }
614
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200615 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (swap_exists_action == SEA_QUIT)
617 {
618 if (!read_buffer && !read_stdin)
619 close(fd);
620 return FAIL;
621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100623 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * Set '[ mark to the line above where the lines go (line 1 if zero).
627 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100628 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
630 curbuf->b_op_start.col = 0;
631
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100632 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
633 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
634 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (!read_buffer)
637 {
638 int m = msg_scroll;
639 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 /*
642 * The file must be closed again, the autocommands may want to change
643 * the file before reading it.
644 */
645 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 /*
649 * The output from the autocommands should not overwrite anything and
650 * should not be overwritten: Set msg_scroll, restore its value if no
651 * output was done.
652 */
653 msg_scroll = TRUE;
654 if (filtering)
655 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
656 FALSE, curbuf, eap);
657 else if (read_stdin)
658 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
659 FALSE, curbuf, eap);
660 else if (newfile)
661 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
662 FALSE, curbuf, eap);
663 else
664 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
665 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100667 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
668 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
669 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100670 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (msg_scrolled == n)
673 msg_scroll = m;
674
675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
678 --no_wait_return;
679 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 return FAIL;
682 }
683#endif
684 /*
685 * Don't allow the autocommands to change the current buffer.
686 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000687 *
688 * Don't allow the autocommands to change the buffer name either
689 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
691 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000692 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
693 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
695 {
696 --no_wait_return;
697 msg_scroll = msg_save;
698 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100699 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100701 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 return FAIL;
704 }
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100707 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
709
710 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
711 {
712 /*
713 * Show the user that we are busy reading the input. Sometimes this
714 * may take a while. When reading from stdin another program may
715 * still be running, don't move the cursor to the last line, unless
716 * always using the GUI.
717 */
718 if (read_stdin)
719 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100720 if (!is_not_a_term())
721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200723# ifdef VIMDLL
724 if (!gui.in_use)
725# endif
726 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100729 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100730 if (gui.in_use && !gui.dying && !gui.starting)
731 {
732 p = (char_u *)_("Reading from stdin...");
733 gui_write(p, (int)STRLEN(p));
734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738 else if (!read_buffer)
739 filemess(curbuf, sfname, (char_u *)"", 0);
740 }
741
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100742 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 /*
745 * Set linecnt now, before the "retry" caused by a wrong guess for
746 * fileformat, and after the autocommands, which may change them.
747 */
748 linecnt = curbuf->b_ml.ml_line_count;
749
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100750 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000751 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000752 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000753 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000754 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000755 curbuf->b_bad_char = eap->bad_char;
756 }
757 else
758 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000761 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 if (eap != NULL && eap->force_enc != 0)
764 {
765 fenc = enc_canonize(eap->cmd + eap->force_enc);
766 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000767 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769 else if (curbuf->b_p_bin)
770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100771 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 fenc_alloced = FALSE;
773 }
774 else if (curbuf->b_help)
775 {
776 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000777 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100779 // Help files are either utf-8 or latin1. Try utf-8 first, if this
780 // fails it must be latin1.
781 // Always do this when 'encoding' is "utf-8". Otherwise only do
782 // this when needed to avoid [converted] remarks all the time.
783 // It is needed when the first line contains non-ASCII characters.
784 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 fenc = (char_u *)"latin1";
786 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000787 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000789 fc = fname[STRLEN(fname) - 1];
790 if (TOLOWER_ASC(fc) == 'x')
791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Read the first line (and a bit more). Immediately rewind to
793 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100794 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200795 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000796 for (p = firstline; p < firstline + len; ++p)
797 if (*p >= 0x80)
798 {
799 c = TRUE;
800 break;
801 }
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804
805 if (c)
806 {
807 fenc_next = fenc;
808 fenc = (char_u *)"utf-8";
809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // When the file is utf-8 but a character doesn't fit in
811 // 'encoding' don't retry. In help text editing utf-8 bytes
812 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000813 if (!enc_utf8)
814 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 fenc_alloced = FALSE;
817 }
818 else if (*p_fencs == NUL)
819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 fenc_alloced = FALSE;
822 }
823 else
824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200826 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 /*
830 * Jump back here to retry reading the file in different ways.
831 * Reasons to retry:
832 * - encoding conversion failed: try another one from "fenc_next"
833 * - BOM detected and fenc was set, need to setup conversion
834 * - "fileformat" check failed: try another
835 *
836 * Variables set for special retry actions:
837 * "file_rewind" Rewind the file to start reading it again.
838 * "advance_fenc" Advance "fenc" using "fenc_next".
839 * "skip_read" Re-use already read bytes (BOM detected).
840 * "did_iconv" iconv() conversion failed, try 'charconvert'.
841 * "keep_fileformat" Don't reset "fileformat".
842 *
843 * Other status indicators:
844 * "tmpname" When != NULL did conversion with 'charconvert'.
845 * Output file has to be deleted afterwards.
846 * "iconv_fd" When != -1 did conversion with iconv().
847 */
848retry:
849
850 if (file_rewind)
851 {
852 if (read_buffer)
853 {
854 read_buf_lnum = 1;
855 read_buf_col = 0;
856 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200857 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 error = TRUE;
861 goto failed;
862 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100863 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200865 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000867 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000870 curbuf->b_start_bomb = FALSE;
871 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000872 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874
875 /*
876 * When retrying with another "fenc" and the first time "fileformat"
877 * will be reset.
878 */
879 if (keep_fileformat)
880 keep_fileformat = FALSE;
881 else
882 {
883 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000886 try_unix = try_dos = try_mac = FALSE;
887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100889 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
Bram Moolenaar13505972019-01-24 15:04:48 +0100896#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (iconv_fd != (iconv_t)-1)
898 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100899 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 iconv_close(iconv_fd);
901 iconv_fd = (iconv_t)-1;
902 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
905 if (advance_fenc)
906 {
907 /*
908 * Try the next entry in 'fileencodings'.
909 */
910 advance_fenc = FALSE;
911
912 if (eap != NULL && eap->force_enc != 0)
913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100914 // Conversion given with "++cc=" wasn't possible, read
915 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000917 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (fenc_alloced)
919 vim_free(fenc);
920 fenc = (char_u *)"";
921 fenc_alloced = FALSE;
922 }
923 else
924 {
925 if (fenc_alloced)
926 vim_free(fenc);
927 if (fenc_next != NULL)
928 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200929 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931 else
932 {
933 fenc = (char_u *)"";
934 fenc_alloced = FALSE;
935 }
936 }
937 if (tmpname != NULL)
938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100939 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100940 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
942 }
943
944 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000945 * Conversion may be required when the encoding of the file is different
946 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
948 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000949 converted = need_conversion(fenc);
950 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
952
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100953 // "ucs-bom" means we need to check the first bytes of the file
954 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (STRCMP(fenc, ENC_UCSBOM) == 0)
956 fio_flags = FIO_UCSBOM;
957
958 /*
959 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
960 * done. This is handled below after read(). Prepare the
961 * fio_flags to avoid having to parse the string each time.
962 * Also check for Unicode to Latin1 conversion, because iconv()
963 * appears not to handle this correctly. This works just like
964 * conversion to UTF-8 except how the resulting character is put in
965 * the buffer.
966 */
967 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
968 fio_flags = get_fio_flags(fenc);
969
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /*
972 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
973 * is handled with MultiByteToWideChar().
974 */
975 if (fio_flags == 0)
976 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar13505972019-01-24 15:04:48 +0100979#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100980 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (fio_flags == 0)
982 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar13505972019-01-24 15:04:48 +0100985#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /*
987 * Try using iconv() if we can't convert internally.
988 */
989 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +0100990# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +0100992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 )
994 iconv_fd = (iconv_t)my_iconv_open(
995 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +0100996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar13505972019-01-24 15:04:48 +0100998#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * Use the 'charconvert' expression when conversion is required
1001 * and we can't do it internally or with iconv().
1002 */
1003 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001004 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001005# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 )
1009 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001010# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001012# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // Skip conversion when it's already done (retry for wrong
1014 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (tmpname == NULL)
1016 {
1017 tmpname = readfile_charconvert(fname, fenc, &fd);
1018 if (tmpname == NULL)
1019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001020 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 advance_fenc = TRUE;
1022 if (fd < 0)
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 error = TRUE;
1027 goto failed;
1028 }
1029 goto retry;
1030 }
1031 }
1032 }
1033 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001037#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 )
1041 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001042 // Conversion wanted but we can't.
1043 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 advance_fenc = TRUE;
1045 goto retry;
1046 }
1047 }
1048 }
1049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // Set "can_retry" when it's possible to rewind the file and try with
1051 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1052 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001053 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055 if (!skip_read)
1056 {
1057 linerest = 0;
1058 filesize = 0;
1059 skip_count = lines_to_skip;
1060 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001062#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001063 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1064 && curbuf->b_ffname != NULL
1065 && curbuf->b_p_udf
1066 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001067 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001068 && !read_stdin
1069 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001070 if (read_undo_file)
1071 sha256_start(&sha_ctx);
1072#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001073#ifdef FEAT_CRYPT
1074 if (curbuf->b_cryptstate != NULL)
1075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // Need to free the state, but keep the key, don't want to ask for
1077 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001078 crypt_free_state(curbuf->b_cryptstate);
1079 curbuf->b_cryptstate = NULL;
1080 }
1081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083
1084 while (!error && !got_int)
1085 {
1086 /*
1087 * We allocate as much space for the file as we can get, plus
1088 * space for the old line plus room for one terminating NUL.
1089 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001090 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001092 if (!skip_read)
1093 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001094#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001095 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001096#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001097 // Use buffer >= 64K. Add linerest to double the size if the
1098 // line gets very long, to avoid a lot of copying. But don't
1099 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001100 size = 0x10000L + linerest;
1101 if (size > 0x100000L)
1102 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001103#endif
1104 }
1105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001106 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001107 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 size = 1;
1112 }
1113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 if (!skip_read)
1116 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001117 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001119 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 FALSE)) != NULL)
1121 break;
1122 }
1123 if (new_buffer == NULL)
1124 {
1125 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1126 error = TRUE;
1127 break;
1128 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001129 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1131 vim_free(buffer);
1132 buffer = new_buffer;
1133 ptr = buffer + linerest;
1134 line_start = buffer;
1135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136 // May need room to translate into.
1137 // For iconv() we don't really know the required space, use a
1138 // factor ICONV_MULT.
1139 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1140 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1141 // become up to 4 bytes, size must be multiple of 2
1142 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1143 // multiple of 2
1144 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1145 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001147#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (iconv_fd != (iconv_t)-1)
1149 size = size / ICONV_MULT;
1150 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (fio_flags & FIO_LATIN1)
1153 size = size / 2;
1154 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1155 size = (size * 2 / 3) & ~1;
1156 else if (fio_flags & FIO_UCS4)
1157 size = (size * 2 / 3) & ~3;
1158 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001159 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001160#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001163#endif
1164#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167#endif
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (conv_restlen > 0)
1170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001171 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 mch_memmove(ptr, conv_rest, conv_restlen);
1173 ptr += conv_restlen;
1174 size -= conv_restlen;
1175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
1177 if (read_buffer)
1178 {
1179 /*
1180 * Read bytes from curbuf. Used for converting text read
1181 * from stdin.
1182 */
1183 if (read_buf_lnum > from)
1184 size = 0;
1185 else
1186 {
1187 int n, ni;
1188 long tlen;
1189
1190 tlen = 0;
1191 for (;;)
1192 {
1193 p = ml_get(read_buf_lnum) + read_buf_col;
1194 n = (int)STRLEN(p);
1195 if ((int)tlen + n + 1 > size)
1196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197 // Filled up to "size", append partial line.
1198 // Change NL to NUL to reverse the effect done
1199 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001200 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 for (ni = 0; ni < n; ++ni)
1202 {
1203 if (p[ni] == NL)
1204 ptr[tlen++] = NUL;
1205 else
1206 ptr[tlen++] = p[ni];
1207 }
1208 read_buf_col += n;
1209 break;
1210 }
1211 else
1212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001213 // Append whole line and new-line. Change NL
1214 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 for (ni = 0; ni < n; ++ni)
1216 {
1217 if (p[ni] == NL)
1218 ptr[tlen++] = NUL;
1219 else
1220 ptr[tlen++] = p[ni];
1221 }
1222 ptr[tlen++] = NL;
1223 read_buf_col = 0;
1224 if (++read_buf_lnum > from)
1225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // When the last line didn't have an
1227 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (!curbuf->b_p_eol)
1229 --tlen;
1230 size = tlen;
1231 break;
1232 }
1233 }
1234 }
1235 }
1236 }
1237 else
1238 {
1239 /*
1240 * Read bytes from the file.
1241 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001242 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001245#ifdef FEAT_CRYPT
1246 /*
1247 * At start of file: Check for magic number of encryption.
1248 */
1249 if (filesize == 0 && size > 0)
1250 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1251 &filesize, newfile, sfname,
1252 &did_ask_for_key);
1253 /*
1254 * Decrypt the read bytes. This is done before checking for
1255 * EOF because the crypt layer may be buffering.
1256 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001257 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1258 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001259 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001260# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001261 if (crypt_works_inplace(curbuf->b_cryptstate))
1262 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001263# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001264 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001265# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 }
1267 else
1268 {
1269 char_u *newptr = NULL;
1270 int decrypted_size;
1271
1272 decrypted_size = crypt_decode_alloc(
1273 curbuf->b_cryptstate, ptr, size, &newptr);
1274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001275 // If the crypt layer is buffering, not producing
1276 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001277 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001278 continue;
1279
1280 if (linerest == 0)
1281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001282 // Simple case: reuse returned buffer (may be
1283 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001284 new_buffer = newptr;
1285 }
1286 else
1287 {
1288 long_u new_size;
1289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001290 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001291 new_size = (long_u)(decrypted_size + linerest + 1);
1292 new_buffer = lalloc(new_size, FALSE);
1293 if (new_buffer == NULL)
1294 {
1295 do_outofmem_msg(new_size);
1296 error = TRUE;
1297 break;
1298 }
1299
1300 mch_memmove(new_buffer, buffer, linerest);
1301 if (newptr != NULL)
1302 mch_memmove(new_buffer + linerest, newptr,
1303 decrypted_size);
1304 }
1305
1306 if (new_buffer != NULL)
1307 {
1308 vim_free(buffer);
1309 buffer = new_buffer;
1310 new_buffer = NULL;
1311 line_start = buffer;
1312 ptr = buffer + linerest;
1313 }
1314 size = decrypted_size;
1315 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001316# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 }
1318#endif
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (size <= 0)
1321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001322 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001325 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001326 /*
1327 * Reached end-of-file but some trailing bytes could
1328 * not be converted. Truncated file?
1329 */
1330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001332 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001333#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001334 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001335#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001336 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001337 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001338 if (can_retry)
1339 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001340 if (conv_error == 0)
1341 conv_error = curbuf->b_ml.ml_line_count
1342 - linecnt + 1;
1343 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001344 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001345 else if (illegal_byte == 0)
1346 illegal_byte = curbuf->b_ml.ml_line_count
1347 - linecnt + 1;
1348 if (bad_char_behavior == BAD_DROP)
1349 {
1350 *(ptr - conv_restlen) = NUL;
1351 conv_restlen = 0;
1352 }
1353 else
1354 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 // Replace the trailing bytes with the replacement
1356 // character if we were converting; if we weren't,
1357 // leave the UTF8 checking code to do it, as it
1358 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001359 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001360#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001361 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001362#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001363 ))
1364 {
1365 while (conv_restlen > 0)
1366 {
1367 *(--ptr) = bad_char_behavior;
1368 --conv_restlen;
1369 }
1370 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001371 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001372#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001373 if (iconv_fd != (iconv_t)-1)
1374 {
1375 iconv_close(iconv_fd);
1376 iconv_fd = (iconv_t)-1;
1377 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001378#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001379 }
1380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 skip_read = FALSE;
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
1386 * At start of file (or after crypt magic number): Check for BOM.
1387 * Also check for a BOM for other Unicode encodings, but not after
1388 * converting with 'charconvert' or when a BOM has already been
1389 * found.
1390 */
1391 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001392#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001393 || (cryptkey != NULL
1394 && filesize == crypt_get_header_len(
1395 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 )
1398 && (fio_flags == FIO_UCSBOM
1399 || (!curbuf->b_p_bomb
1400 && tmpname == NULL
1401 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1402 {
1403 char_u *ccname;
1404 int blen;
1405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (size < 2 || curbuf->b_p_bin)
1408 ccname = NULL;
1409 else
1410 ccname = check_for_bom(ptr, size, &blen,
1411 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1412 if (ccname != NULL)
1413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001414 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 filesize += blen;
1416 size -= blen;
1417 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001418 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001421 curbuf->b_start_bomb = TRUE;
1422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424
1425 if (fio_flags == FIO_UCSBOM)
1426 {
1427 if (ccname == NULL)
1428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 advance_fenc = TRUE;
1431 }
1432 else
1433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (fenc_alloced)
1436 vim_free(fenc);
1437 fenc = ccname;
1438 fenc_alloced = FALSE;
1439 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001440 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 skip_read = TRUE;
1442 goto retry;
1443 }
1444 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001446 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001447 ptr -= conv_restlen;
1448 size += conv_restlen;
1449 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /*
1451 * Break here for a read error or end-of-file.
1452 */
1453 if (size <= 0)
1454 break;
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaar13505972019-01-24 15:04:48 +01001457#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (iconv_fd != (iconv_t)-1)
1459 {
1460 /*
1461 * Attempt conversion of the read bytes to 'encoding' using
1462 * iconv().
1463 */
1464 const char *fromp;
1465 char *top;
1466 size_t from_size;
1467 size_t to_size;
1468
1469 fromp = (char *)ptr;
1470 from_size = size;
1471 ptr += size;
1472 top = (char *)ptr;
1473 to_size = real_size - size;
1474
1475 /*
1476 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001477 * another conversion. Except for when there is no
1478 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001480 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1481 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1483 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001484 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001485 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001486 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001487 if (conv_error == 0)
1488 conv_error = readfile_linenr(linecnt,
1489 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001491 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001492 ++fromp;
1493 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001494 if (bad_char_behavior == BAD_KEEP)
1495 {
1496 *top++ = *(fromp - 1);
1497 --to_size;
1498 }
1499 else if (bad_char_behavior != BAD_DROP)
1500 {
1501 *top++ = bad_char_behavior;
1502 --to_size;
1503 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
1506 if (from_size > 0)
1507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001508 // Some remaining characters, keep them for the next
1509 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1511 conv_restlen = (int)from_size;
1512 }
1513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 line_start = ptr - linerest;
1516 mch_memmove(line_start, buffer, (size_t)linerest);
1517 size = (long)((char_u *)top - ptr);
1518 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar4f974752019-02-17 17:44:42 +01001521#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (fio_flags & FIO_CODEPAGE)
1523 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001524 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001525 WCHAR ucs2buf[3];
1526 int ucs2len;
1527 int codepage = FIO_GET_CP(fio_flags);
1528 int bytelen;
1529 int found_bad;
1530 char replstr[2];
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 /*
1533 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001534 * a codepage, using standard MS-Windows functions. This
1535 * requires two steps:
1536 * 1. convert from 'fileencoding' to ucs-2
1537 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001539 * Because there may be illegal bytes AND an incomplete byte
1540 * sequence at the end, we may have to do the conversion one
1541 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001544 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001545 if (bad_char_behavior > 0)
1546 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001548 replstr[0] = '?';
1549 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001552 * Move the bytes to the end of the buffer, so that we have
1553 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001555 src = ptr + real_size - size;
1556 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001558 /*
1559 * Do the conversion.
1560 */
1561 dst = ptr;
1562 size = size;
1563 while (size > 0)
1564 {
1565 found_bad = FALSE;
1566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001568 if (codepage == CP_UTF8)
1569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001570 // Handle CP_UTF8 input ourselves to be able to handle
1571 // trailing bytes properly.
1572 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001573 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001574 if (bytelen > size)
1575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576 // Only got some bytes of a character. Normally
1577 // it's put in "conv_rest", but if it's too long
1578 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001579 if (bytelen <= CONV_RESTLEN)
1580 break;
1581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001582 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001583 bytelen = size;
1584 found_bad = TRUE;
1585 }
1586 else
1587 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001588 int u8c = utf_ptr2char(src);
1589
Bram Moolenaar86e01082005-12-29 22:45:34 +00001590 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001591 found_bad = TRUE;
1592 ucs2buf[0] = u8c;
1593 ucs2len = 1;
1594 }
1595 }
1596 else
1597# endif
1598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 // We don't know how long the byte sequence is, try
1600 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001601 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1602 ++bytelen)
1603 {
1604 ucs2len = MultiByteToWideChar(codepage,
1605 MB_ERR_INVALID_CHARS,
1606 (LPCSTR)src, bytelen,
1607 ucs2buf, 3);
1608 if (ucs2len > 0)
1609 break;
1610 }
1611 if (ucs2len == 0)
1612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001613 // If we have only one byte then it's probably an
1614 // incomplete byte sequence. Otherwise discard
1615 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001616 if (size == 1)
1617 break;
1618 found_bad = TRUE;
1619 bytelen = 1;
1620 }
1621 }
1622
1623 if (!found_bad)
1624 {
1625 int i;
1626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001627 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001628 if (enc_utf8)
1629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001630 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001631 for (i = 0; i < ucs2len; ++i)
1632 dst += utf_char2bytes(ucs2buf[i], dst);
1633 }
1634 else
1635 {
1636 BOOL bad = FALSE;
1637 int dstlen;
1638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 // From UCS-2 to "enc_codepage". If the
1640 // conversion uses the default character "?",
1641 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001642 dstlen = WideCharToMultiByte(enc_codepage, 0,
1643 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001644 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001645 replstr, &bad);
1646 if (bad)
1647 found_bad = TRUE;
1648 else
1649 dst += dstlen;
1650 }
1651 }
1652
1653 if (found_bad)
1654 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001655 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001656 if (can_retry)
1657 goto rewind_retry;
1658 if (conv_error == 0)
1659 conv_error = readfile_linenr(linecnt, ptr, dst);
1660 if (bad_char_behavior != BAD_DROP)
1661 {
1662 if (bad_char_behavior == BAD_KEEP)
1663 {
1664 mch_memmove(dst, src, bytelen);
1665 dst += bytelen;
1666 }
1667 else
1668 *dst++ = bad_char_behavior;
1669 }
1670 }
1671
1672 src += bytelen;
1673 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001675
1676 if (size > 0)
1677 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001678 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001679 mch_memmove(conv_rest, src, size);
1680 conv_restlen = size;
1681 }
1682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001683 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001684 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001687#endif
1688#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (fio_flags & FIO_MACROMAN)
1690 {
1691 /*
1692 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001693 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001695 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (fio_flags != 0)
1701 {
1702 int u8c;
1703 char_u *dest;
1704 char_u *tail = NULL;
1705
1706 /*
1707 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1708 * "enc_utf8" not set: Convert Unicode to Latin1.
1709 * Go from end to start through the buffer, because the number
1710 * of bytes may increase.
1711 * "dest" points to after where the UTF-8 bytes go, "p" points
1712 * to after the next character to convert.
1713 */
1714 dest = ptr + real_size;
1715 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1716 {
1717 p = ptr + size;
1718 if (fio_flags == FIO_UTF8)
1719 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001720 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 tail = ptr + size - 1;
1722 while (tail > ptr && (*tail & 0xc0) == 0x80)
1723 --tail;
1724 if (tail + utf_byte2len(*tail) <= ptr + size)
1725 tail = NULL;
1726 else
1727 p = tail;
1728 }
1729 }
1730 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1731 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 p = ptr + (size & ~1);
1734 if (size & 1)
1735 tail = p;
1736 if ((fio_flags & FIO_UTF16) && p > ptr)
1737 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001738 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (fio_flags & FIO_ENDIAN_L)
1740 {
1741 u8c = (*--p << 8);
1742 u8c += *--p;
1743 }
1744 else
1745 {
1746 u8c = *--p;
1747 u8c += (*--p << 8);
1748 }
1749 if (u8c >= 0xd800 && u8c <= 0xdbff)
1750 tail = p;
1751 else
1752 p += 2;
1753 }
1754 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 p = ptr + (size & ~3);
1759 if (size & 3)
1760 tail = p;
1761 }
1762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001763 // If there is a trailing incomplete sequence move it to
1764 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (tail != NULL)
1766 {
1767 conv_restlen = (int)((ptr + size) - tail);
1768 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1769 size -= conv_restlen;
1770 }
1771
1772
1773 while (p > ptr)
1774 {
1775 if (fio_flags & FIO_LATIN1)
1776 u8c = *--p;
1777 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1778 {
1779 if (fio_flags & FIO_ENDIAN_L)
1780 {
1781 u8c = (*--p << 8);
1782 u8c += *--p;
1783 }
1784 else
1785 {
1786 u8c = *--p;
1787 u8c += (*--p << 8);
1788 }
1789 if ((fio_flags & FIO_UTF16)
1790 && u8c >= 0xdc00 && u8c <= 0xdfff)
1791 {
1792 int u16c;
1793
1794 if (p == ptr)
1795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001796 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (can_retry)
1798 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001799 if (conv_error == 0)
1800 conv_error = readfile_linenr(linecnt,
1801 ptr, p);
1802 if (bad_char_behavior == BAD_DROP)
1803 continue;
1804 if (bad_char_behavior != BAD_KEEP)
1805 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001808 // found second word of double-word, get the first
1809 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (fio_flags & FIO_ENDIAN_L)
1811 {
1812 u16c = (*--p << 8);
1813 u16c += *--p;
1814 }
1815 else
1816 {
1817 u16c = *--p;
1818 u16c += (*--p << 8);
1819 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001820 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1821 + (u8c & 0x3ff);
1822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001823 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (u16c < 0xd800 || u16c > 0xdbff)
1825 {
1826 if (can_retry)
1827 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001828 if (conv_error == 0)
1829 conv_error = readfile_linenr(linecnt,
1830 ptr, p);
1831 if (bad_char_behavior == BAD_DROP)
1832 continue;
1833 if (bad_char_behavior != BAD_KEEP)
1834 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 }
1838 else if (fio_flags & FIO_UCS4)
1839 {
1840 if (fio_flags & FIO_ENDIAN_L)
1841 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001842 u8c = (unsigned)*--p << 24;
1843 u8c += (unsigned)*--p << 16;
1844 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 u8c += *--p;
1846 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001847 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001850 u8c += (unsigned)*--p << 8;
1851 u8c += (unsigned)*--p << 16;
1852 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001855 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 if (*--p < 0x80)
1858 u8c = *p;
1859 else
1860 {
1861 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001862 p -= len;
1863 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (len == 0)
1865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001866 // Not a valid UTF-8 character, retry with
1867 // another fenc when possible, otherwise just
1868 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (can_retry)
1870 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001871 if (conv_error == 0)
1872 conv_error = readfile_linenr(linecnt,
1873 ptr, p);
1874 if (bad_char_behavior == BAD_DROP)
1875 continue;
1876 if (bad_char_behavior != BAD_KEEP)
1877 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001881 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 dest -= utf_char2len(u8c);
1884 (void)utf_char2bytes(u8c, dest);
1885 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 --dest;
1889 if (u8c >= 0x100)
1890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001891 // character doesn't fit in latin1, retry with
1892 // another fenc when possible, otherwise just
1893 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001894 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001896 if (conv_error == 0)
1897 conv_error = readfile_linenr(linecnt, ptr, p);
1898 if (bad_char_behavior == BAD_DROP)
1899 ++dest;
1900 else if (bad_char_behavior == BAD_KEEP)
1901 *dest = u8c;
1902 else if (eap != NULL && eap->bad_char != 0)
1903 *dest = bad_char_behavior;
1904 else
1905 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 else
1908 *dest = u8c;
1909 }
1910 }
1911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 line_start = dest - linerest;
1914 mch_memmove(line_start, buffer, (size_t)linerest);
1915 size = (long)((ptr + real_size) - dest);
1916 ptr = dest;
1917 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001918 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001920 int incomplete_tail = FALSE;
1921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001923 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001925 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001926 int l;
1927
1928 if (todo <= 0)
1929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (*p >= 0x80)
1931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // A length of 1 means it's an illegal byte. Accept
1933 // an incomplete character at the end though, the next
1934 // read() will get the next bytes, we'll check it
1935 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001936 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001937 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // Avoid retrying with a different encoding when
1940 // a truncated file is more likely, or attempting
1941 // to read the rest of an incomplete sequence when
1942 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001943 if (p > ptr || filesize > 0)
1944 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // Incomplete byte sequence, move it to conv_rest[]
1946 // and try to read the rest of it, unless we've
1947 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001948 if (p > ptr)
1949 {
1950 conv_restlen = todo;
1951 mch_memmove(conv_rest, p, conv_restlen);
1952 size -= conv_restlen;
1953 break;
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001956 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001957 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 // Illegal byte. If we can try another encoding
1959 // do that, unless at EOF where a truncated
1960 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001961 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001962 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01001963#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001965 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1966 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01001967#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001969 if (conv_error == 0 && illegal_byte == 0)
1970 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001973 if (bad_char_behavior == BAD_DROP)
1974 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001975 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001976 --p;
1977 --size;
1978 }
1979 else if (bad_char_behavior != BAD_KEEP)
1980 *p = bad_char_behavior;
1981 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001982 else
1983 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001986 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001988 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001990 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01001991#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001992 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001994 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001996#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001998 advance_fenc = TRUE;
1999 file_rewind = TRUE;
2000 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002004 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 filesize += size;
2006
2007 /*
2008 * when reading the first part of a file: guess EOL type
2009 */
2010 if (fileformat == EOL_UNKNOWN)
2011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (try_dos || try_unix)
2014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002016 if (try_mac)
2017 try_mac = 1;
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 for (p = ptr; p < ptr + size; ++p)
2020 {
2021 if (*p == NL)
2022 {
2023 if (!try_unix
2024 || (try_dos && p > ptr && p[-1] == CAR))
2025 fileformat = EOL_DOS;
2026 else
2027 fileformat = EOL_UNIX;
2028 break;
2029 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002030 else if (*p == CAR && try_mac)
2031 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (fileformat == EOL_UNIX && try_mac)
2036 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002037 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 try_mac = 1;
2039 try_unix = 1;
2040 for (; p >= ptr && *p != CAR; p--)
2041 ;
2042 if (p >= ptr)
2043 {
2044 for (p = ptr; p < ptr + size; ++p)
2045 {
2046 if (*p == NL)
2047 try_unix++;
2048 else if (*p == CAR)
2049 try_mac++;
2050 }
2051 if (try_mac > try_unix)
2052 fileformat = EOL_MAC;
2053 }
2054 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002055 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 // Looking for CR but found no end-of-line markers at
2057 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002058 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (fileformat == EOL_UNKNOWN && try_mac)
2063 fileformat = EOL_MAC;
2064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (fileformat == EOL_UNKNOWN)
2067 fileformat = default_fileformat();
2068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002070 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 set_fileformat(fileformat, OPT_LOCAL);
2072 }
2073 }
2074
2075 /*
2076 * This loop is executed once for every character read.
2077 * Keep it fast!
2078 */
2079 if (fileformat == EOL_MAC)
2080 {
2081 --ptr;
2082 while (++ptr, --size >= 0)
2083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if ((c = *ptr) != NUL && c != CAR && c != NL)
2086 continue;
2087 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else
2092 {
2093 if (skip_count == 0)
2094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 len = (colnr_T) (ptr - line_start + 1);
2097 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2098 {
2099 error = TRUE;
2100 break;
2101 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002102#ifdef FEAT_PERSISTENT_UNDO
2103 if (read_undo_file)
2104 sha256_update(&sha_ctx, line_start, len);
2105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 ++lnum;
2107 if (--read_count == 0)
2108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 error = TRUE; // break loop
2110 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 break;
2112 }
2113 }
2114 else
2115 --skip_count;
2116 line_start = ptr + 1;
2117 }
2118 }
2119 }
2120 else
2121 {
2122 --ptr;
2123 while (++ptr, --size >= 0)
2124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 continue;
2127 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 else
2130 {
2131 if (skip_count == 0)
2132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 len = (colnr_T)(ptr - line_start + 1);
2135 if (fileformat == EOL_DOS)
2136 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002137 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002139 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 ptr[-1] = NUL;
2141 --len;
2142 }
2143 /*
2144 * Reading in Dos format, but no CR-LF found!
2145 * When 'fileformats' includes "unix", delete all
2146 * the lines read so far and start all over again.
2147 * Otherwise give an error message later.
2148 */
2149 else if (ff_error != EOL_DOS)
2150 {
2151 if ( try_unix
2152 && !read_stdin
2153 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002154 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2155 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002158 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 set_fileformat(EOL_UNIX, OPT_LOCAL);
2160 file_rewind = TRUE;
2161 keep_fileformat = TRUE;
2162 goto retry;
2163 }
2164 ff_error = EOL_DOS;
2165 }
2166 }
2167 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2168 {
2169 error = TRUE;
2170 break;
2171 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002172#ifdef FEAT_PERSISTENT_UNDO
2173 if (read_undo_file)
2174 sha256_update(&sha_ctx, line_start, len);
2175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 ++lnum;
2177 if (--read_count == 0)
2178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 error = TRUE; // break loop
2180 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 break;
2182 }
2183 }
2184 else
2185 --skip_count;
2186 line_start = ptr + 1;
2187 }
2188 }
2189 }
2190 linerest = (long)(ptr - line_start);
2191 ui_breakcheck();
2192 }
2193
2194failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (error && read_count == 0)
2197 error = FALSE;
2198
2199 /*
2200 * If we get EOF in the middle of a line, note the fact and
2201 * complete the line ourselves.
2202 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2203 */
2204 if (!error
2205 && !got_int
2206 && linerest != 0
2207 && !(!curbuf->b_p_bin
2208 && fileformat == EOL_DOS
2209 && *line_start == Ctrl_Z
2210 && ptr == line_start + 1))
2211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002212 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002213 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 curbuf->b_p_eol = FALSE;
2215 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002216 len = (colnr_T)(ptr - line_start + 1);
2217 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 error = TRUE;
2219 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002220 {
2221#ifdef FEAT_PERSISTENT_UNDO
2222 if (read_undo_file)
2223 sha256_update(&sha_ctx, line_start, len);
2224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002233 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002234 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002235 crypt_free_state(curbuf->b_cryptstate);
2236 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002237 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002238 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2239 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 // Don't set cryptkey to NULL, it's used below as a flag that
2241 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#endif
2243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 // If editing a new file: set 'fenc' for the current buffer.
2245 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002246 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002248 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (fenc_alloced)
2250 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002251#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#endif
2255
2256 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002258#ifdef HAVE_FD_CLOEXEC
2259 else
2260 {
2261 int fdflags = fcntl(fd, F_GETFD);
2262 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002263 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002264 }
2265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 vim_free(buffer);
2267
2268#ifdef HAVE_DUP
2269 if (read_stdin)
2270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002273 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275#endif
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (tmpname != NULL)
2278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002279 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 vim_free(tmpname);
2281 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 /*
2285 * In recovery mode everything but autocommands is skipped.
2286 */
2287 if (!recoverymode)
2288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2291 {
2292#ifdef FEAT_NETBEANS_INTG
2293 netbeansFireChanges = 0;
2294#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002295 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#ifdef FEAT_NETBEANS_INTG
2297 netbeansFireChanges = 1;
2298#endif
2299 --linecnt;
2300 }
2301 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2302 if (filesize == 0)
2303 linecnt = 0;
2304 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002307#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002308 // After reading the text into the buffer the diff info needs to
2309 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002310 diff_invalidate(curbuf);
2311#endif
2312#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // All folds in the window are invalid now. Mark them for update
2314 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002315 foldUpdateAll(curwin);
2316#endif
2317 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 appended_lines_mark(from, linecnt);
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifndef ALWAYS_USE_GUI
2322 /*
2323 * If we were reading from the same terminal as where messages go,
2324 * the screen will have been messed up.
2325 * Switch on raw mode now and clear the screen.
2326 */
2327 if (read_stdin)
2328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 starttermcap();
2331 screenclear();
2332 }
2333#endif
2334
2335 if (got_int)
2336 {
2337 if (!(flags & READ_DUMMY))
2338 {
2339 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2340 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002341 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 msg_scroll = msg_save;
2344#ifdef FEAT_VIMINFO
2345 check_marks_read();
2346#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
2350 if (!filtering && !(flags & READ_DUMMY))
2351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 c = FALSE;
2354
2355#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 STRCAT(IObuff, _("[fifo]"));
2359 c = TRUE;
2360 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 STRCAT(IObuff, _("[socket]"));
2364 c = TRUE;
2365 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002366# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002367 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002368 {
2369 STRCAT(IObuff, _("[character special]"));
2370 c = TRUE;
2371 }
2372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#endif
2374 if (curbuf->b_p_ro)
2375 {
2376 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2377 c = TRUE;
2378 }
2379 if (read_no_eol_lnum)
2380 {
2381 msg_add_eol();
2382 c = TRUE;
2383 }
2384 if (ff_error == EOL_DOS)
2385 {
2386 STRCAT(IObuff, _("[CR missing]"));
2387 c = TRUE;
2388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (split)
2390 {
2391 STRCAT(IObuff, _("[long lines split]"));
2392 c = TRUE;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (notconverted)
2395 {
2396 STRCAT(IObuff, _("[NOT converted]"));
2397 c = TRUE;
2398 }
2399 else if (converted)
2400 {
2401 STRCAT(IObuff, _("[converted]"));
2402 c = TRUE;
2403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404#ifdef FEAT_CRYPT
2405 if (cryptkey != NULL)
2406 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002407 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 c = TRUE;
2409 }
2410#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002411 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002413 sprintf((char *)IObuff + STRLEN(IObuff),
2414 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 c = TRUE;
2416 }
2417 else if (illegal_byte > 0)
2418 {
2419 sprintf((char *)IObuff + STRLEN(IObuff),
2420 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2421 c = TRUE;
2422 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002423 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
2425 STRCAT(IObuff, _("[READ ERRORS]"));
2426 c = TRUE;
2427 }
2428 if (msg_add_fileformat(fileformat))
2429 c = TRUE;
2430#ifdef FEAT_CRYPT
2431 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002432 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002433 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 else
2435#endif
2436 msg_add_lines(c, (long)linecnt, filesize);
2437
Bram Moolenaard23a8232018-02-10 18:45:26 +01002438 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 msg_scrolled_ign = TRUE;
2440#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002441 // Don't show the message when reading stdin, it would end up in a
2442 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (read_stdin || read_buffer)
2444 p = msg_may_trunc(FALSE, IObuff);
2445 else
2446#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002447 {
2448 if (msg_col > 0)
2449 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002450 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002453 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002454 // Need to repeat the message after redrawing when:
2455 // - When reading from stdin (the screen will be cleared next).
2456 // - When restart_edit is set (otherwise there will be a delay
2457 // before redrawing).
2458 // - When the screen was scrolled but there is no wait-return
2459 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002460 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 msg_scrolled_ign = FALSE;
2462 }
2463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002466 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002467 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 curbuf->b_p_ro = TRUE;
2469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002470 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 /*
2473 * In Ex mode: cursor at last new line.
2474 * Otherwise: cursor at first new line.
2475 */
2476 if (exmode_active)
2477 curwin->w_cursor.lnum = from + linecnt;
2478 else
2479 curwin->w_cursor.lnum = from + 1;
2480 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002481 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002483 if (!cmdmod.lockmarks)
2484 {
2485 // Set '[ and '] marks to the newly read lines.
2486 curbuf->b_op_start.lnum = from + 1;
2487 curbuf->b_op_start.col = 0;
2488 curbuf->b_op_end.lnum = from + linecnt;
2489 curbuf->b_op_end.col = 0;
2490 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002491
Bram Moolenaar4f974752019-02-17 17:44:42 +01002492#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002493 /*
2494 * Work around a weird problem: When a file has two links (only
2495 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002496 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002497 * It's correct again after reading the file, thus reset the timestamp
2498 * here.
2499 */
2500 if (newfile && !read_stdin && !read_buffer
2501 && mch_stat((char *)fname, &st) >= 0)
2502 {
2503 buf_store_time(curbuf, &st, fname);
2504 curbuf->b_mtime_read = curbuf->b_mtime;
2505 }
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
2508 msg_scroll = msg_save;
2509
2510#ifdef FEAT_VIMINFO
2511 /*
2512 * Get the marks before executing autocommands, so they can be used there.
2513 */
2514 check_marks_read();
2515#endif
2516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002518 * We remember if the last line of the read didn't have
2519 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2520 * or writing the read again with 'binary' on. The latter is required
2521 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002523 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002525 // When reloading a buffer put the cursor at the first line that is
2526 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002527 if (flags & READ_KEEP_UNDO)
2528 u_find_first_changed();
2529
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002530#ifdef FEAT_PERSISTENT_UNDO
2531 /*
2532 * When opening a new file locate undo info and read it.
2533 */
2534 if (read_undo_file)
2535 {
2536 char_u hash[UNDO_HASH_SIZE];
2537
2538 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002539 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002540 }
2541#endif
2542
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002543 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 int m = msg_scroll;
2546 int n = msg_scrolled;
2547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 // Save the fileformat now, otherwise the buffer will be considered
2549 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002550 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 save_file_ff(curbuf);
2552
2553 /*
2554 * The output from the autocommands should not overwrite anything and
2555 * should not be overwritten: Set msg_scroll, restore its value if no
2556 * output was done.
2557 */
2558 msg_scroll = TRUE;
2559 if (filtering)
2560 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2561 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002562 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2565 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002566 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2567 /*
2568 * EVENT_FILETYPE was not triggered but the buffer already has a
2569 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2570 */
2571 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2572 TRUE, curbuf);
2573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 else
2575 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2576 FALSE, NULL, eap);
2577 if (msg_scrolled == n)
2578 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002579# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002580 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 if (recoverymode && error)
2586 return FAIL;
2587 return OK;
2588}
2589
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002590#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002591/*
2592 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2593 * which is the name of files used for process substitution output by
2594 * some shells on some operating systems, e.g., bash on SunOS.
2595 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2596 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002598is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002599{
2600 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2601 && VIM_ISDIGIT(fname[8])
2602 && *skipdigits(fname + 9) == NUL
2603 && (fname[9] != NUL
2604 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2605}
2606#endif
2607
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002608/*
2609 * From the current line count and characters read after that, estimate the
2610 * line number where we are now.
2611 * Used for error messages that include a line number.
2612 */
2613 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002615 linenr_T linecnt, // line count before reading more bytes
2616 char_u *p, // start of more bytes read
2617 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002618{
2619 char_u *s;
2620 linenr_T lnum;
2621
2622 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2623 for (s = p; s < endp; ++s)
2624 if (*s == '\n')
2625 ++lnum;
2626 return lnum;
2627}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002630 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2631 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 * Returns OK or FAIL.
2633 */
2634 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002635prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaar13505972019-01-24 15:04:48 +01002637 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (eap->cmd == NULL)
2639 return FAIL;
2640
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002641 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2642 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002643 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002644 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002645
2646 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002647 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002648 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 return OK;
2650}
2651
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002652/*
2653 * Set default or forced 'fileformat' and 'binary'.
2654 */
2655 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002656set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002657{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002658 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002659 if (set_options)
2660 {
2661 if (eap != NULL && eap->force_ff != 0)
2662 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2663 else if (*p_ffs != NUL)
2664 set_fileformat(default_fileformat(), OPT_LOCAL);
2665 }
2666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002668 if (eap != NULL && eap->force_bin != 0)
2669 {
2670 int oldval = curbuf->b_p_bin;
2671
2672 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2673 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2674 }
2675}
2676
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002677/*
2678 * Set forced 'fileencoding'.
2679 */
2680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002681set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002682{
2683 if (eap->force_enc != 0)
2684 {
2685 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2686
2687 if (fenc != NULL)
2688 set_string_option_direct((char_u *)"fenc", -1,
2689 fenc, OPT_FREE|OPT_LOCAL, 0);
2690 vim_free(fenc);
2691 }
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694/*
2695 * Find next fileencoding to use from 'fileencodings'.
2696 * "pp" points to fenc_next. It's advanced to the next item.
2697 * When there are no more items, an empty string is returned and *pp is set to
2698 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002699 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2700 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
2702 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002703next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 char_u *p;
2706 char_u *r;
2707
Bram Moolenaarf077db22019-08-13 00:18:24 +02002708 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (**pp == NUL)
2710 {
2711 *pp = NULL;
2712 return (char_u *)"";
2713 }
2714 p = vim_strchr(*pp, ',');
2715 if (p == NULL)
2716 {
2717 r = enc_canonize(*pp);
2718 *pp += STRLEN(*pp);
2719 }
2720 else
2721 {
2722 r = vim_strnsave(*pp, (int)(p - *pp));
2723 *pp = p + 1;
2724 if (r != NULL)
2725 {
2726 p = enc_canonize(r);
2727 vim_free(r);
2728 r = p;
2729 }
2730 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002731 if (r != NULL)
2732 *alloced = TRUE;
2733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002735 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 r = (char_u *)"";
2737 *pp = NULL;
2738 }
2739 return r;
2740}
2741
Bram Moolenaar13505972019-01-24 15:04:48 +01002742#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743/*
2744 * Convert a file with the 'charconvert' expression.
2745 * This closes the file which is to be read, converts it and opens the
2746 * resulting file for reading.
2747 * Returns name of the resulting converted file (the caller should delete it
2748 * after reading it).
2749 * Returns NULL if the conversion failed ("*fdp" is not set) .
2750 */
2751 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002752readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002753 char_u *fname, // name of input file
2754 char_u *fenc, // converted from
2755 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002758 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002760 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002762 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002765 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 *fdp = -1;
2767 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2768 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002769 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2771 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002772 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774
2775 if (errmsg != NULL)
2776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002777 // Don't use emsg(), it breaks mappings, the retry with
2778 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002779 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (tmpname != NULL)
2781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002782 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002783 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 }
2786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002787 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (*fdp < 0)
2789 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2790
2791 return tmpname;
2792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793#endif
2794
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002795#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002797 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2799 * *filesizep are updated.
2800 * Return the (new) encryption key, NULL for no encryption.
2801 */
2802 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002803check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002804 char_u *cryptkey, // previous encryption key or NULL
2805 char_u *ptr, // pointer to read bytes
2806 long *sizep, // length of read bytes
2807 off_T *filesizep, // nr of bytes used from file
2808 int newfile, // editing a new buffer
2809 char_u *fname, // file name to display
2810 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002812 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002813 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002814
2815 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // Mark the buffer as read-only until the decryption has taken place.
2818 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002819 curbuf->b_p_ro = TRUE;
2820
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002821 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002822 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002823 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 if (*curbuf->b_p_key)
2826 cryptkey = curbuf->b_p_key;
2827 else
2828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // When newfile is TRUE, store the typed key in the 'key'
2830 // option and don't free it. bf needs hash of the key saved.
2831 // Don't ask for the key again when first time Enter was hit.
2832 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002833 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002834 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002835 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002836 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002837 *did_ask = TRUE;
2838
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (cryptkey != NULL && *cryptkey == NUL)
2841 {
2842 if (cryptkey != curbuf->b_p_key)
2843 vim_free(cryptkey);
2844 cryptkey = NULL;
2845 }
2846 }
2847 }
2848
2849 if (cryptkey != NULL)
2850 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002851 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002852
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002853 curbuf->b_cryptstate = crypt_create_from_header(
2854 method, cryptkey, ptr);
2855 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002858 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002859 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002860 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002861 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002862 *filesizep += header_len;
2863 *sizep -= header_len;
2864 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2865
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002866 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002867 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002870 // When starting to edit a new file which does not have encryption, clear
2871 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002872 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2874
2875 return cryptkey;
2876}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002877#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002880 * Return TRUE if a file appears to be read-only from the file permissions.
2881 */
2882 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002883check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002884 char_u *fname, // full path to file
2885 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002886{
2887#ifndef USE_MCH_ACCESS
2888 int fd = 0;
2889#endif
2890
2891 return (
2892#ifdef USE_MCH_ACCESS
2893# ifdef UNIX
2894 (perm & 0222) == 0 ||
2895# endif
2896 mch_access((char *)fname, W_OK)
2897#else
2898 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2899 ? TRUE : (close(fd), FALSE)
2900#endif
2901 );
2902}
2903
Bram Moolenaara7870192019-02-14 12:56:36 +01002904#if defined(HAVE_FSYNC) || defined(PROTO)
2905/*
2906 * Call fsync() with Mac-specific exception.
2907 * Return fsync() result: zero for success.
2908 */
2909 int
2910vim_fsync(int fd)
2911{
2912 int r;
2913
2914# ifdef MACOS_X
2915 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002916 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002917# endif
2918 r = fsync(fd);
2919 return r;
2920}
2921#endif
2922
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002924 * Set the name of the current buffer. Use when the buffer doesn't have a
2925 * name and a ":r" or ":w" command with a file name is used.
2926 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002927 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002928set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002929{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002930 buf_T *buf = curbuf;
2931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002932 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002933 if (curbuf->b_p_bl)
2934 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2935 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002936#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002938 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002939#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002940 if (curbuf != buf)
2941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002943 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002944 return FAIL;
2945 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002946
2947 if (setfname(curbuf, fname, sfname, FALSE) == OK)
2948 curbuf->b_flags |= BF_NOTEDITED;
2949
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002950 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002951 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
2952 if (curbuf->b_p_bl)
2953 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002954#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002956 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002957#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002959 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002960 if (*curbuf->b_p_ft == NUL)
2961 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002962 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02002963 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002964 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002965 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002966
2967 return OK;
2968}
2969
2970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 * Put file name into IObuff with quotes.
2972 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00002973 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002974msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 if (fname == NULL)
2977 fname = (char_u *)"-stdin-";
2978 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
2979 IObuff[0] = '"';
2980 STRCAT(IObuff, "\" ");
2981}
2982
2983/*
2984 * Append message for text mode to IObuff.
2985 * Return TRUE if something appended.
2986 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002987 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002988msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990#ifndef USE_CRNL
2991 if (eol_type == EOL_DOS)
2992 {
2993 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
2994 return TRUE;
2995 }
2996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 if (eol_type == EOL_MAC)
2998 {
2999 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3000 return TRUE;
3001 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003002#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (eol_type == EOL_UNIX)
3004 {
3005 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3006 return TRUE;
3007 }
3008#endif
3009 return FALSE;
3010}
3011
3012/*
3013 * Append line and character count to IObuff.
3014 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003015 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016msg_add_lines(
3017 int insert_space,
3018 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003019 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 char_u *p;
3022
3023 p = IObuff + STRLEN(IObuff);
3024
3025 if (insert_space)
3026 *p++ = ' ';
3027 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003028 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003029 "%ldL, %lldC", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003032 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003034 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
3035 NGETTEXT("%lld character", "%lld characters", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003036 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038}
3039
3040/*
3041 * Append message for missing line separator to IObuff.
3042 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003043 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003044msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3047}
3048
Bram Moolenaar473952e2019-09-28 16:30:04 +02003049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003052#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003053 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3054 // the seconds. Since the roundoff is done when flushing the inode, the
3055 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 return (t1 - t2 > 1 || t2 - t1 > 1);
3057#else
3058 return (t1 != t2);
3059#endif
3060}
3061
3062/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003063 * Return TRUE if file encoding "fenc" requires conversion from or to
3064 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003067need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003069 int same_encoding;
3070 int enc_flags;
3071 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003073 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003074 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003075 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003076 fenc_flags = 0;
3077 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003078 else
3079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003080 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3081 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003082 enc_flags = get_fio_flags(p_enc);
3083 fenc_flags = get_fio_flags(fenc);
3084 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3085 }
3086 if (same_encoding)
3087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003088 // Specified encoding matches with 'encoding'. This requires
3089 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003090 return enc_unicode != 0;
3091 }
3092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003093 // Encodings differ. However, conversion is not needed when 'enc' is any
3094 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003095 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096}
3097
3098/*
3099 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3100 * internal conversion.
3101 * if "ptr" is an empty string, use 'encoding'.
3102 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003103 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003104get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 int prop;
3107
3108 if (*ptr == NUL)
3109 ptr = p_enc;
3110
3111 prop = enc_canon_props(ptr);
3112 if (prop & ENC_UNICODE)
3113 {
3114 if (prop & ENC_2BYTE)
3115 {
3116 if (prop & ENC_ENDIAN_L)
3117 return FIO_UCS2 | FIO_ENDIAN_L;
3118 return FIO_UCS2;
3119 }
3120 if (prop & ENC_4BYTE)
3121 {
3122 if (prop & ENC_ENDIAN_L)
3123 return FIO_UCS4 | FIO_ENDIAN_L;
3124 return FIO_UCS4;
3125 }
3126 if (prop & ENC_2WORD)
3127 {
3128 if (prop & ENC_ENDIAN_L)
3129 return FIO_UTF16 | FIO_ENDIAN_L;
3130 return FIO_UTF16;
3131 }
3132 return FIO_UTF8;
3133 }
3134 if (prop & ENC_LATIN1)
3135 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003136 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 return 0;
3138}
3139
Bram Moolenaar473952e2019-09-28 16:30:04 +02003140#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141/*
3142 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3143 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3144 * Used for conversion between 'encoding' and 'fileencoding'.
3145 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 int cp;
3150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003151 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (!enc_utf8 && enc_codepage <= 0)
3153 return 0;
3154
3155 cp = encname2codepage(ptr);
3156 if (cp == 0)
3157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003158# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (STRCMP(ptr, "utf-8") == 0)
3160 cp = CP_UTF8;
3161 else
3162# endif
3163 return 0;
3164 }
3165 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3166}
3167#endif
3168
Bram Moolenaar473952e2019-09-28 16:30:04 +02003169#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170/*
3171 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3172 * needed for the internal conversion to/from utf-8 or latin1.
3173 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003174 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003175get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3178 && (enc_canon_props(ptr) & ENC_MACROMAN))
3179 return FIO_MACROMAN;
3180 return 0;
3181}
3182#endif
3183
3184/*
3185 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3186 * "size" must be at least 2.
3187 * Return the name of the encoding and set "*lenp" to the length.
3188 * Returns NULL when no BOM found.
3189 */
3190 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003191check_for_bom(
3192 char_u *p,
3193 long size,
3194 int *lenp,
3195 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 char *name = NULL;
3198 int len = 2;
3199
3200 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003201 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003203 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 len = 3;
3205 }
3206 else if (p[0] == 0xff && p[1] == 0xfe)
3207 {
3208 if (size >= 4 && p[2] == 0 && p[3] == 0
3209 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003211 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 len = 4;
3213 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003214 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003215 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003216 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003217 // utf-16le is preferred, it also works for ucs-2le text
3218 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220 else if (p[0] == 0xfe && p[1] == 0xff
3221 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003223 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003224 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003225 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003226 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3230 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 len = 4;
3234 }
3235
3236 *lenp = len;
3237 return (char_u *)name;
3238}
3239
3240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * Try to find a shortname by comparing the fullname with the current
3242 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003243 * Returns "full_path" or pointer into "full_path" if shortened.
3244 */
3245 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003246shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003247{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003248 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003249 char_u *p = full_path;
3250
Bram Moolenaard9462e32011-04-11 21:35:11 +02003251 dirname = alloc(MAXPATHL);
3252 if (dirname == NULL)
3253 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003254 if (mch_dirname(dirname, MAXPATHL) == OK)
3255 {
3256 p = shorten_fname(full_path, dirname);
3257 if (p == NULL || *p == NUL)
3258 p = full_path;
3259 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003260 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003261 return p;
3262}
3263
3264/*
3265 * Try to find a shortname by comparing the fullname with the current
3266 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * Returns NULL if not shorter name possible, pointer into "full_path"
3268 * otherwise.
3269 */
3270 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 int len;
3274 char_u *p;
3275
3276 if (full_path == NULL)
3277 return NULL;
3278 len = (int)STRLEN(dir_name);
3279 if (fnamencmp(dir_name, full_path, len) == 0)
3280 {
3281 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003282#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003284 * MS-Windows: when a file is in the root directory, dir_name will end
3285 * in a slash, since C: by itself does not define a specific dir. In
3286 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 if (!((len > 2) && (*(p - 2) == ':')))
3289#endif
3290 {
3291 if (vim_ispathsep(*p))
3292 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003293#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 else
3295 p = NULL;
3296#endif
3297 }
3298 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003299#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 /*
3301 * When using a file in the current drive, remove the drive name:
3302 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3303 * a floppy from "A:\dir" to "B:\dir".
3304 */
3305 else if (len > 3
3306 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3307 && full_path[1] == ':'
3308 && vim_ispathsep(full_path[2]))
3309 p = full_path + 2;
3310#endif
3311 else
3312 p = NULL;
3313 return p;
3314}
3315
3316/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003317 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * When "force" is TRUE: Use full path from now on for files currently being
3319 * edited, both for file name and swap file name. Try to shorten the file
3320 * names a bit, if safe to do so.
3321 * When "force" is FALSE: Only try to shorten absolute file names.
3322 * For buffers that have buftype "nofile" or "scratch": never change the file
3323 * name.
3324 */
3325 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003326shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3327{
3328 char_u *p;
3329
3330 if (buf->b_fname != NULL
3331#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003332 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003333#endif
3334 && !path_with_url(buf->b_fname)
3335 && (force
3336 || buf->b_sfname == NULL
3337 || mch_isFullName(buf->b_sfname)))
3338 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003339 if (buf->b_sfname != buf->b_ffname)
3340 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003341 p = shorten_fname(buf->b_ffname, dirname);
3342 if (p != NULL)
3343 {
3344 buf->b_sfname = vim_strsave(p);
3345 buf->b_fname = buf->b_sfname;
3346 }
3347 if (p == NULL || buf->b_fname == NULL)
3348 buf->b_fname = buf->b_ffname;
3349 }
3350}
3351
3352/*
3353 * Shorten filenames for all buffers.
3354 */
3355 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003356shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 char_u dirname[MAXPATHL];
3359 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003362 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003364 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003366 // Always make the swap file name a full path, a "nofile" buffer may
3367 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003368 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003371 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003372#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003373 popup_update_preview_title();
3374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3378 || defined(FEAT_GUI_MSWIN) \
3379 || defined(FEAT_GUI_MAC) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003380 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 || defined(PROTO)
3382/*
3383 * Shorten all filenames in "fnames[count]" by current directory.
3384 */
3385 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 int i;
3389 char_u dirname[MAXPATHL];
3390 char_u *p;
3391
3392 if (fnames == NULL || count < 1)
3393 return;
3394 mch_dirname(dirname, sizeof(dirname));
3395 for (i = 0; i < count; ++i)
3396 {
3397 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003399 // shorten_fname() returns pointer in given "fnames[i]". If free
3400 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3401 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 p = vim_strsave(p);
3403 vim_free(fnames[i]);
3404 fnames[i] = p;
3405 }
3406 }
3407}
3408#endif
3409
3410/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003411 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * fo_o_h.ext for MSDOS or when shortname option set.
3413 *
3414 * Assumed that fname is a valid name found in the filesystem we assure that
3415 * the return value is a different name and ends in 'ext'.
3416 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3417 * characters otherwise.
3418 * Space for the returned name is allocated, must be freed later.
3419 * Returns NULL when out of memory.
3420 */
3421 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003422modname(
3423 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{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003427 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 fname, ext, prepend_dot);
3429}
3430
3431 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003432buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003433 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003434 char_u *fname,
3435 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003436 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 char_u *retval;
3439 char_u *s;
3440 char_u *e;
3441 char_u *ptr;
3442 int fnamelen, extlen;
3443
3444 extlen = (int)STRLEN(ext);
3445
3446 /*
3447 * If there is no file name we must get the name of the current directory
3448 * (we need the full path in case :cd is used).
3449 */
3450 if (fname == NULL || *fname == NUL)
3451 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003452 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (retval == NULL)
3454 return NULL;
3455 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3456 (fnamelen = (int)STRLEN(retval)) == 0)
3457 {
3458 vim_free(retval);
3459 return NULL;
3460 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003461 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 retval[fnamelen++] = PATHSEP;
3464 retval[fnamelen] = NUL;
3465 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003466 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468 else
3469 {
3470 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003471 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (retval == NULL)
3473 return NULL;
3474 STRCPY(retval, fname);
3475#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003476 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477#endif
3478 }
3479
3480 /*
3481 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3482 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3483 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3484 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3485 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003486 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003488 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003489 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003492 {
3493 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003498 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3500 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 s = ptr + STRLEN(ptr);
3503
3504 /*
3505 * For 8.3 file names we may have to reduce the length.
3506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 /*
3510 * If there is no file name, or the file name ends in '/', and the
3511 * extension starts with '.', put a '_' before the dot, because just
3512 * ".ext" is invalid.
3513 */
3514 if (fname == NULL || *fname == NUL
3515 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 *s++ = '_';
3519 }
3520 /*
3521 * If the extension starts with '.', truncate the base name at 8
3522 * characters
3523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003526 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 s = ptr + 8;
3529 *s = '\0';
3530 }
3531 }
3532 /*
3533 * If the extension doesn't start with '.', and the file name
3534 * doesn't have an extension yet, append a '.'
3535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else if ((e = vim_strchr(ptr, '.')) == NULL)
3537 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 /*
3539 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003540 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 */
3542 else if ((int)STRLEN(e) + extlen > 4)
3543 s = e + 4 - extlen;
3544 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003545#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /*
3547 * If there is no file name, and the extension starts with '.', put a
3548 * '_' before the dot, because just ".ext" may be invalid if it's on a
3549 * FAT partition, and on HPFS it doesn't matter.
3550 */
3551 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3552 *s++ = '_';
3553#endif
3554
3555 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003556 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 * ext can start with '.' and cannot exceed 3 more characters.
3558 */
3559 STRCPY(s, ext);
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 /*
3562 * Prepend the dot.
3563 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003564 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003566 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 /*
3571 * Check that, after appending the extension, the file name is really
3572 * different.
3573 */
3574 if (fname != NULL && STRCMP(fname, retval) == 0)
3575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003576 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 while (--s >= ptr)
3578 {
3579 if (*s != '_')
3580 {
3581 *s = '_';
3582 break;
3583 }
3584 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003585 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 *ptr = 'v';
3587 }
3588 return retval;
3589}
3590
3591/*
3592 * Like fgets(), but if the file line is too long, it is truncated and the
3593 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003594 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 */
3596 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003597vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 char *eof;
3600#define FGETS_SIZE 200
3601 char tbuf[FGETS_SIZE];
3602
3603 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003607 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003609 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 do
3611 {
3612 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003613 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3615 }
3616 return (eof == NULL);
3617}
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619/*
3620 * rename() only works if both files are on the same file system, this
3621 * function will (attempts to?) copy the file across if rename fails -- webb
3622 * Return -1 for failure, 0 for success.
3623 */
3624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003625vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 int fd_in;
3628 int fd_out;
3629 int n;
3630 char *errmsg = NULL;
3631 char *buffer;
3632#ifdef AMIGA
3633 BPTR flock;
3634#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003635 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003636 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003637#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003639#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003640 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003643 * When the names are identical, there is nothing to do. When they refer
3644 * to the same file (ignoring case and slash/backslash differences) but
3645 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
3647 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003648 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003649 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003650 use_tmp_file = TRUE;
3651 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003652 return 0;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /*
3656 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3657 */
3658 if (mch_stat((char *)from, &st) < 0)
3659 return -1;
3660
Bram Moolenaar3576da72008-12-30 15:15:57 +00003661#ifdef UNIX
3662 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003663 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003665 // It's possible for the source and destination to be the same file.
3666 // This happens when "from" and "to" differ in case and are on a FAT32
3667 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003668 if (mch_stat((char *)to, &st_to) >= 0
3669 && st.st_dev == st_to.st_dev
3670 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003671 use_tmp_file = TRUE;
3672 }
3673#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003674#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003675 {
3676 BY_HANDLE_FILE_INFORMATION info1, info2;
3677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003678 // It's possible for the source and destination to be the same file.
3679 // In that case go through a temp file name. This makes rename("foo",
3680 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003681 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3682 && win32_fileinfo(to, &info2) == FILEINFO_OK
3683 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3684 && info1.nFileIndexHigh == info2.nFileIndexHigh
3685 && info1.nFileIndexLow == info2.nFileIndexLow)
3686 use_tmp_file = TRUE;
3687 }
3688#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003689
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003690 if (use_tmp_file)
3691 {
3692 char tempname[MAXPATHL + 1];
3693
3694 /*
3695 * Find a name that doesn't exist and is in the same directory.
3696 * Rename "from" to "tempname" and then rename "tempname" to "to".
3697 */
3698 if (STRLEN(from) >= MAXPATHL - 5)
3699 return -1;
3700 STRCPY(tempname, from);
3701 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003702 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003703 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3704 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003705 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003706 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003707 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003708 if (mch_rename(tempname, (char *)to) == 0)
3709 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003710 // Strange, the second step failed. Try moving the
3711 // file back and return failure.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003712 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003713 return -1;
3714 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 // If it fails for one temp name it will most likely fail
3716 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003717 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003718 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003719 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003720 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003721 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 /*
3724 * Delete the "to" file, this is required on some systems to make the
3725 * mch_rename() work, on other systems it makes sure that we don't have
3726 * two files when the mch_rename() fails.
3727 */
3728
3729#ifdef AMIGA
3730 /*
3731 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3732 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003733 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 * deleting the "from" file (horror!) we lock it during the remove.
3735 *
3736 * When used for making a backup before writing the file: This should not
3737 * happen with ":w", because startscript() should detect this problem and
3738 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3739 * name. This problem does exist with ":w filename", but then the
3740 * original file will be somewhere else so the backup isn't really
3741 * important. If autoscripting is off the rename may fail.
3742 */
3743 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3744#endif
3745 mch_remove(to);
3746#ifdef AMIGA
3747 if (flock)
3748 UnLock(flock);
3749#endif
3750
3751 /*
3752 * First try a normal rename, return if it works.
3753 */
3754 if (mch_rename((char *)from, (char *)to) == 0)
3755 return 0;
3756
3757 /*
3758 * Rename() failed, try copying the file.
3759 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003760 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003761#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003762 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003763 acl = mch_get_acl(from);
3764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3766 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003767 {
3768#ifdef HAVE_ACL
3769 mch_free_acl(acl);
3770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003772 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003774 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003775 fd_out = mch_open((char *)to,
3776 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (fd_out == -1)
3778 {
3779 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003780#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 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (buffer == NULL)
3788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003790 close(fd_in);
3791#ifdef HAVE_ACL
3792 mch_free_acl(acl);
3793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return -1;
3795 }
3796
Bram Moolenaar473952e2019-09-28 16:30:04 +02003797 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003798 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
3800 errmsg = _("E208: Error writing to \"%s\"");
3801 break;
3802 }
3803
3804 vim_free(buffer);
3805 close(fd_in);
3806 if (close(fd_out) < 0)
3807 errmsg = _("E209: Error closing \"%s\"");
3808 if (n < 0)
3809 {
3810 errmsg = _("E210: Error reading \"%s\"");
3811 to = from;
3812 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003813#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003814 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003815#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003816#ifdef HAVE_ACL
3817 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003818 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003819#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003820#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003821 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (errmsg != NULL)
3824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003825 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return -1;
3827 }
3828 mch_remove(from);
3829 return 0;
3830}
3831
3832static int already_warned = FALSE;
3833
3834/*
3835 * Check if any not hidden buffer has been changed.
3836 * Postpone the check if there are characters in the stuff buffer, a global
3837 * command is being executed, a mapping is being executed or an autocommand is
3838 * busy.
3839 * Returns TRUE if some message was written (screen should be redrawn and
3840 * cursor positioned).
3841 */
3842 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003843check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003844 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 buf_T *buf;
3847 int didit = 0;
3848 int n;
3849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003850 // Don't check timestamps while system() or another low-level function may
3851 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (no_check_timestamps > 0)
3853 return FALSE;
3854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003855 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3856 // event and we would keep on checking if the file is steadily growing.
3857 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (focus && did_check_timestamps)
3859 {
3860 need_check_timestamps = TRUE;
3861 return FALSE;
3862 }
3863
3864 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003865 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003866 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 else
3868 {
3869 ++no_wait_return;
3870 did_check_timestamps = TRUE;
3871 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003872 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003874 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (buf->b_nwindows > 0)
3876 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003877 bufref_T bufref;
3878
3879 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 n = buf_check_timestamp(buf, focus);
3881 if (didit < n)
3882 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003883 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003885 // Autocommands have removed the buffer, start at the
3886 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 buf = firstbuf;
3888 continue;
3889 }
3890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892 --no_wait_return;
3893 need_check_timestamps = FALSE;
3894 if (need_wait_return && didit == 2)
3895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003896 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003897 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 out_flush();
3899 }
3900 }
3901 return didit;
3902}
3903
3904/*
3905 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3906 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3907 * empty.
3908 */
3909 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003910move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 buf_T *tbuf = curbuf;
3913 int retval = OK;
3914 linenr_T lnum;
3915 char_u *p;
3916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003917 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 curbuf = tobuf;
3919 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3920 {
3921 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3922 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3923 {
3924 vim_free(p);
3925 retval = FAIL;
3926 break;
3927 }
3928 vim_free(p);
3929 }
3930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003931 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (retval != FAIL)
3933 {
3934 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003935 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003936 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003938 // Oops! We could try putting back the saved lines, but that
3939 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 retval = FAIL;
3941 break;
3942 }
3943 }
3944
3945 curbuf = tbuf;
3946 return retval;
3947}
3948
3949/*
3950 * Check if buffer "buf" has been changed.
3951 * Also check if the file for a new buffer unexpectedly appeared.
3952 * return 1 if a changed buffer was found.
3953 * return 2 if a message has been displayed.
3954 * return 0 otherwise.
3955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003957buf_check_timestamp(
3958 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003961 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 int stat_res;
3963 int retval = 0;
3964 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003965 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00003967 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 int helpmesg = FALSE;
3969 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003970 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3972 int can_reload = FALSE;
3973#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003974 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int orig_mode = buf->b_orig_mode;
3976#ifdef FEAT_GUI
3977 int save_mouse_correct = need_mouse_correct;
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003980 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003981#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003982 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003983#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003984 bufref_T bufref;
3985
3986 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003988 // If there is no file name, the buffer is not loaded, 'buftype' is
3989 // set, we are in the middle of a save or being called recursively: ignore
3990 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (buf->b_ffname == NULL
3992 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02003993 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00003996#ifdef FEAT_NETBEANS_INTG
3997 || isNetbeansBuffer(buf)
3998#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02003999#ifdef FEAT_TERMINAL
4000 || buf->b_term != NULL
4001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 )
4003 return 0;
4004
4005 if ( !(buf->b_flags & BF_NOTEDITED)
4006 && buf->b_mtime != 0
4007 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4008 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004009 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010#ifdef HAVE_ST_MODE
4011 || (int)st.st_mode != buf->b_orig_mode
4012#else
4013 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4014#endif
4015 ))
4016 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004017 long prev_b_mtime = buf->b_mtime;
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 retval = 1;
4020
Bram Moolenaar386bc822018-07-07 18:34:12 +02004021 // set b_mtime to stop further warnings (e.g., when executing
4022 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (stat_res < 0)
4024 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004025 // Check the file again later to see if it re-appears.
4026 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 buf->b_orig_size = 0;
4028 buf->b_orig_mode = 0;
4029 }
4030 else
4031 buf_store_time(buf, &st, buf->b_ffname);
4032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004033 // Don't do anything for a directory. Might contain the file
4034 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (mch_isdir(buf->b_fname))
4036 ;
4037
4038 /*
4039 * If 'autoread' is set, the buffer has no changes and the file still
4040 * exists, reload the buffer. Use the buffer-local option value if it
4041 * was set, the global option value otherwise.
4042 */
4043 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4044 && !bufIsChanged(buf) && stat_res >= 0)
4045 reload = TRUE;
4046 else
4047 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004048 if (stat_res < 0)
4049 reason = "deleted";
4050 else if (bufIsChanged(buf))
4051 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004052 /*
4053 * Check if the file contents really changed to avoid giving a
4054 * warning when only the timestamp was set (e.g., checked out of
4055 * CVS). Always warn when the buffer was changed.
4056 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004057 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4058 reason = "changed";
4059 else if (orig_mode != buf->b_orig_mode)
4060 reason = "mode";
4061 else
4062 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 /*
4065 * Only give the warning if there are no FileChangedShell
4066 * autocommands.
4067 * Avoid being called recursively by setting "busy".
4068 */
4069 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004070#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004071 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4072 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004073#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004074 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4076 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004077 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 busy = FALSE;
4079 if (n)
4080 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004081 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004082 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004083#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004084 s = get_vim_var_str(VV_FCS_CHOICE);
4085 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4086 reload = TRUE;
4087 else if (STRCMP(s, "ask") == 0)
4088 n = FALSE;
4089 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004090#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004093 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004095 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004096 {
4097 // Only give the message once.
4098 if (prev_b_mtime != -1)
4099 mesg = _("E211: File \"%s\" no longer available");
4100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 else
4102 {
4103 helpmesg = TRUE;
4104#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4105 can_reload = TRUE;
4106#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004107 if (reason[2] == 'n')
4108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004110 mesg2 = _("See \":help W12\" for more info.");
4111 }
4112 else if (reason[1] == 'h')
4113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 mesg2 = _("See \":help W11\" for more info.");
4116 }
4117 else if (*reason == 'm')
4118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004120 mesg2 = _("See \":help W16\" for more info.");
4121 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004122 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004123 // Only timestamp changed, store it to avoid a warning
4124 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004125 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 }
4128 }
4129
4130 }
4131 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4132 && vim_fexists(buf->b_ffname))
4133 {
4134 retval = 1;
4135 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4136 buf->b_flags |= BF_NEW_W;
4137#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4138 can_reload = TRUE;
4139#endif
4140 }
4141
4142 if (mesg != NULL)
4143 {
4144 path = home_replace_save(buf, buf->b_fname);
4145 if (path != NULL)
4146 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004147 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004149 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004150 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004151#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004152 // Set warningmsg here, before the unimportant and output-specific
4153 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4157 if (can_reload)
4158 {
4159 if (*mesg2 != NUL)
4160 {
4161 STRCAT(tbuf, "\n");
4162 STRCAT(tbuf, mesg2);
4163 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4165 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004166 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 reload = TRUE;
4168 }
4169 else
4170#endif
4171 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4172 {
4173 if (*mesg2 != NUL)
4174 {
4175 STRCAT(tbuf, "; ");
4176 STRCAT(tbuf, mesg2);
4177 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004178 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 retval = 2;
4180 }
4181 else
4182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004186 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004188 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 msg_clr_eos();
4190 (void)msg_end();
4191 if (emsg_silent == 0)
4192 {
4193 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004194#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004196#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004197 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004198 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 redraw_cmdline = FALSE;
4202 }
4203 }
4204 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206
4207 vim_free(path);
4208 vim_free(tbuf);
4209 }
4210 }
4211
4212 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004214 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004215 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004216#ifdef FEAT_PERSISTENT_UNDO
4217 if (buf->b_p_udf && buf->b_ffname != NULL)
4218 {
4219 char_u hash[UNDO_HASH_SIZE];
4220 buf_T *save_curbuf = curbuf;
4221
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004222 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004223 curbuf = buf;
4224 u_compute_hash(hash);
4225 u_write_undo(NULL, FALSE, buf, hash);
4226 curbuf = save_curbuf;
4227 }
4228#endif
4229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004231 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004232 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004233 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4234 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004236 // restore this in case an autocommand has set it; it would break
4237 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 need_mouse_correct = save_mouse_correct;
4239#endif
4240
4241 return retval;
4242}
4243
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004244/*
4245 * Reload a buffer that is already loaded.
4246 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004247 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4248 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004249 */
4250 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004251buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004252{
4253 exarg_T ea;
4254 pos_T old_cursor;
4255 linenr_T old_topline;
4256 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004257 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004258 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004259 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004260 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004261 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004263 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004264 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004266 // We only want to read the text from the file, not reset the syntax
4267 // highlighting, clear marks, diff status, etc. Force the fileformat
4268 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004269 if (prep_exarg(&ea, buf) == OK)
4270 {
4271 old_cursor = curwin->w_cursor;
4272 old_topline = curwin->w_topline;
4273
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004274 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 // Save all the text, so that the reload can be undone.
4277 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004278 u_sync(FALSE);
4279 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4280 flags |= READ_KEEP_UNDO;
4281 }
4282
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004283 /*
4284 * To behave like when a new file is edited (matters for
4285 * BufReadPost autocommands) we first need to delete the current
4286 * buffer contents. But if reading the file fails we should keep
4287 * the old contents. Can't use memory only, the file might be
4288 * too big. Use a hidden buffer to move the buffer contents to.
4289 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004290 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004291 savebuf = NULL;
4292 else
4293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004294 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004295 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004296 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004297 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004300 curbuf = savebuf;
4301 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004302 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004303 curbuf = buf;
4304 curwin->w_buffer = buf;
4305 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004306 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004307 || move_lines(buf, savebuf) == FAIL)
4308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004309 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004310 buf->b_fname);
4311 saved = FAIL;
4312 }
4313 }
4314
4315 if (saved == OK)
4316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4318 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4320 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004321 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004323#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004324 if (!aborting())
4325#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004326 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004327 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004329 // Put the text back from the save buffer. First
4330 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004331 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004332 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004333 break;
4334 (void)move_lines(savebuf, buf);
4335 }
4336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004340 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004341 if ((flags & READ_KEEP_UNDO) == 0)
4342 {
4343 u_blockfree(buf);
4344 u_clearall(buf);
4345 }
4346 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004348 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004349 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004350 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004351 }
4352 }
4353 vim_free(ea.cmd);
4354
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004355 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004356 wipe_buffer(savebuf, FALSE);
4357
4358#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004359 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004360 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361#endif
4362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // Restore the topline and cursor position and check it (lines may
4364 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365 if (old_topline > curbuf->b_ml.ml_line_count)
4366 curwin->w_topline = curbuf->b_ml.ml_line_count;
4367 else
4368 curwin->w_topline = old_topline;
4369 curwin->w_cursor = old_cursor;
4370 check_cursor();
4371 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004373#ifdef FEAT_FOLDING
4374 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004375 win_T *wp;
4376 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004379 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004380 if (wp->w_buffer == curwin->w_buffer
4381 && !foldmethodIsManual(wp))
4382 foldUpdateAll(wp);
4383 }
4384#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 // If the mode didn't change and 'readonly' was set, keep the old
4386 // value; the user probably used the ":view" command. But don't
4387 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004388 if (orig_mode == curbuf->b_orig_mode)
4389 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004392 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 }
4394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004395 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004396 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004397 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004398}
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004401buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004404 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef HAVE_ST_MODE
4406 buf->b_orig_mode = (int)st->st_mode;
4407#else
4408 buf->b_orig_mode = mch_getperm(fname);
4409#endif
4410}
4411
4412/*
4413 * Adjust the line with missing eol, used for the next write.
4414 * Used for do_filter(), when the input lines for the filter are deleted.
4415 */
4416 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004417write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004420 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421}
4422
Bram Moolenaarda440d22016-01-16 21:27:23 +01004423#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4424/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004425 * Core part of "readdir()" function.
4426 * Retrieve the list of files/directories of "path" into "gap".
4427 * Return OK for success, FAIL for failure.
4428 */
4429 int
4430readdir_core(
4431 garray_T *gap,
4432 char_u *path,
4433 void *context,
4434 int (*checkitem)(void *context, char_u *name))
4435{
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004436 int failed = FALSE;
4437 char_u *p;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004438
4439 ga_init2(gap, (int)sizeof(char *), 20);
4440
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004441# ifdef MSWIN
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004442 {
4443 char_u *buf;
4444 int ok;
4445 HANDLE hFind = INVALID_HANDLE_VALUE;
4446 WIN32_FIND_DATAW wfb;
4447 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004448
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004449 buf = alloc(MAXPATHL);
4450 if (buf == NULL)
4451 return FAIL;
4452 STRNCPY(buf, path, MAXPATHL-5);
4453 p = buf + STRLEN(buf);
4454 MB_PTR_BACK(buf, p);
4455 if (*p == '\\' || *p == '/')
4456 *p = NUL;
4457 STRCAT(buf, "\\*");
4458
4459 wn = enc_to_utf16(buf, NULL);
4460 if (wn != NULL)
4461 hFind = FindFirstFileW(wn, &wfb);
4462 ok = (hFind != INVALID_HANDLE_VALUE);
4463 if (!ok)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004464 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004465 failed = TRUE;
4466 smsg(_(e_notopen), path);
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004467 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004468 else
4469 {
4470 while (ok)
4471 {
4472 int ignore;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004473
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004474 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
4475 if (p == NULL)
4476 break; // out of memory
4477
4478 ignore = p[0] == '.' && (p[1] == NUL
4479 || (p[1] == '.' && p[2] == NUL));
4480 if (!ignore && checkitem != NULL)
4481 {
4482 int r = checkitem(context, p);
4483
4484 if (r < 0)
4485 {
4486 vim_free(p);
4487 break;
4488 }
4489 if (r == 0)
4490 ignore = TRUE;
4491 }
4492
4493 if (!ignore)
4494 {
4495 if (ga_grow(gap, 1) == OK)
4496 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p);
4497 else
4498 {
4499 failed = TRUE;
4500 vim_free(p);
4501 break;
4502 }
4503 }
4504
4505 vim_free(p);
4506 ok = FindNextFileW(hFind, &wfb);
4507 }
4508 FindClose(hFind);
4509 }
4510
4511 vim_free(buf);
4512 vim_free(wn);
4513 }
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004514# else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004515 {
4516 DIR *dirp;
4517 struct dirent *dp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004518
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004519 dirp = opendir((char *)path);
4520 if (dirp == NULL)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004521 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004522 failed = TRUE;
4523 smsg(_(e_notopen), path);
4524 }
4525 else
4526 {
4527 for (;;)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004528 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004529 int ignore;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004530
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004531 dp = readdir(dirp);
4532 if (dp == NULL)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004533 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004534 p = (char_u *)dp->d_name;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004535
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004536 ignore = p[0] == '.' &&
4537 (p[1] == NUL ||
4538 (p[1] == '.' && p[2] == NUL));
4539 if (!ignore && checkitem != NULL)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004540 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004541 int r = checkitem(context, p);
4542
4543 if (r < 0)
4544 break;
4545 if (r == 0)
4546 ignore = TRUE;
4547 }
4548
4549 if (!ignore)
4550 {
4551 if (ga_grow(gap, 1) == OK)
4552 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p);
4553 else
4554 {
4555 failed = TRUE;
4556 break;
4557 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004558 }
4559 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004560
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004561 closedir(dirp);
4562 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004563 }
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004564# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004565
4566 if (!failed && gap->ga_len > 0)
4567 sort_strings((char_u **)gap->ga_data, gap->ga_len);
4568
4569 return failed ? FAIL : OK;
4570}
4571
4572/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004573 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004574 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004575 */
4576 int
4577delete_recursive(char_u *name)
4578{
4579 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004580 int i;
4581 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004582 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004583
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004584 // A symbolic link to a directory itself is deleted, not the directory it
4585 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004586 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004587# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004588 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004589# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004590 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004591# endif
4592 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004593 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004594 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004595 if (exp == NULL)
4596 return -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004597 if (readdir_core(&ga, exp, NULL, NULL) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004598 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004599 for (i = 0; i < ga.ga_len; ++i)
4600 {
4601 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4602 ((char_u **)ga.ga_data)[i]);
4603 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004604 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004605 }
4606 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004607 }
4608 else
4609 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004610 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004611 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004612 }
4613 else
4614 result = mch_remove(name) == 0 ? 0 : -1;
4615
4616 return result;
4617}
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004621static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004623# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4624/*
4625 * Open temporary directory and take file lock to prevent
4626 * to be auto-cleaned.
4627 */
4628 static void
4629vim_opentempdir(void)
4630{
4631 DIR *dp = NULL;
4632
4633 if (vim_tempdir_dp != NULL)
4634 return;
4635
4636 dp = opendir((const char*)vim_tempdir);
4637
4638 if (dp != NULL)
4639 {
4640 vim_tempdir_dp = dp;
4641 flock(dirfd(vim_tempdir_dp), LOCK_SH);
4642 }
4643}
4644
4645/*
4646 * Close temporary directory - it automatically release file lock.
4647 */
4648 static void
4649vim_closetempdir(void)
4650{
4651 if (vim_tempdir_dp != NULL)
4652 {
4653 closedir(vim_tempdir_dp);
4654 vim_tempdir_dp = NULL;
4655 }
4656}
4657# endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659/*
4660 * Delete the temp directory and all files it contains.
4661 */
4662 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004663vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (vim_tempdir != NULL)
4666 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004667# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4668 vim_closetempdir();
4669# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004670 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01004671 gettail(vim_tempdir)[-1] = NUL;
4672 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004673 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00004678 * Directory "tempdir" was created. Expand this name to a full path and put
4679 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
4680 * "tempdir" must be no longer than MAXPATHL.
4681 */
4682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004683vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004684{
4685 char_u *buf;
4686
Bram Moolenaar964b3742019-05-24 18:54:09 +02004687 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004688 if (buf != NULL)
4689 {
4690 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
4691 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004692 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004693 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004694# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4695 vim_opentempdir();
4696# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004697 vim_free(buf);
4698 }
4699}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00004700#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00004701
4702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 * vim_tempname(): Return a unique name that can be used for a temp file.
4704 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02004705 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
4706 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 *
4708 * The returned pointer is to allocated memory.
4709 * The returned pointer is NULL if no valid name was found.
4710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004712vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004713 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004714 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004717 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01004718#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004719 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#else
4721 char_u itmp[TEMPNAMELEN];
4722#endif
4723
4724#ifdef TEMPDIRNAMES
4725 static char *(tempdirs[]) = {TEMPDIRNAMES};
4726 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02004728 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729# endif
4730
4731 /*
4732 * This will create a directory for private use by this instance of Vim.
4733 * This is done once, and the same directory is used for all temp files.
4734 * This method avoids security problems because of symlink attacks et al.
4735 * It's also a bit faster, because we only need to check for an existing
4736 * file when creating the directory and not for each temp file.
4737 */
4738 if (vim_tempdir == NULL)
4739 {
4740 /*
4741 * Try the entries in TEMPDIRNAMES to create the temp directory.
4742 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00004743 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00004745# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004746 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004747 long nr;
4748 long off;
4749# endif
4750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004751 // Expand $TMP, leave room for "/v1100000/999999999".
4752 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01004754 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004756 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004757 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaareaf03392009-11-17 11:08:52 +00004759# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004760 {
4761# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004762 // Make sure the umask doesn't remove the executable bit.
4763 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004764 mode_t umask_save = umask(077);
4765# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004766 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02004767 STRCAT(itmp, "vXXXXXX");
4768 if (mkdtemp((char *)itmp) != NULL)
4769 vim_settempdir(itmp);
4770# if defined(UNIX) || defined(VMS)
4771 (void)umask(umask_save);
4772# endif
4773 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00004774# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004775 // Get an arbitrary number of up to 6 digits. When it's
4776 // unlikely that it already exists it will be faster,
4777 // otherwise it doesn't matter. The use of mkdir() avoids any
4778 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004780 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004782 // Try up to 10000 different values until we find a name that
4783 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 for (off = 0; off < 10000L; ++off)
4785 {
4786 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004787# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaareaf03392009-11-17 11:08:52 +00004791 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
4792# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 // If mkdir() does not set errno to EEXIST, check for
4794 // existing file here. There is a race condition then,
4795 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (mch_stat((char *)itmp, &st) >= 0)
4797 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00004798# endif
4799# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004800 // Make sure the umask doesn't remove the executable bit.
4801 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004805# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00004807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (r == 0)
4809 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00004810 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 break;
4812 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00004813# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004814 // If the mkdir() didn't fail because the file/dir exists,
4815 // we probably can't create any dir here, try another
4816 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00004818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 break;
4820 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004821# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (vim_tempdir != NULL)
4823 break;
4824 }
4825 }
4826 }
4827
4828 if (vim_tempdir != NULL)
4829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 // There is no need to check if the file exists, because we own the
4831 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
4833 return vim_strsave(itmp);
4834 }
4835
4836 return NULL;
4837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004838#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar4f974752019-02-17 17:44:42 +01004840# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004841 WCHAR wszTempFile[_MAX_PATH + 1];
4842 WCHAR buf4[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 char_u *retval;
4844 char_u *p;
4845
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004846 wcscpy(itmp, L"");
4847 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01004848 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004849 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
4850 wszTempFile[1] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01004851 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004852 wcscpy(buf4, L"VIM");
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004853 buf4[2] = extra_char; // make it "VIa", "VIb", etc.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004854 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004856 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004857 // GetTempFileName() will create the file, we don't want that
4858 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01004860 // Backslashes in a temp file name cause problems when filtering with
4861 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
4862 // didn't set 'shellslash'.
4863 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (*p_shcf == '-' || p_ssl)
4865 for (p = retval; *p; ++p)
4866 if (*p == '\\')
4867 *p = '/';
4868 return retval;
4869
Bram Moolenaar4f974752019-02-17 17:44:42 +01004870# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01004873 char_u *p;
4874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004875 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01004876 p = tmpnam((char *)itmp);
4877 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 return NULL;
4879# else
4880 char_u *p;
4881
4882# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004883 // mktemp() is not working on VMS. It seems to be
4884 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 sprintf((char *)itmp, "VIM%c", extra_char);
4886 p = (char_u *)tempnam("tmp:", (char *)itmp);
4887 if (p != NULL)
4888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004889 // VMS will use '.LIS' if we don't explicitly specify an extension,
4890 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 STRCPY(itmp, p);
4892 STRCAT(itmp, ".txt");
4893 free(p);
4894 }
4895 else
4896 return NULL;
4897# else
4898 STRCPY(itmp, TEMPNAME);
4899 if ((p = vim_strchr(itmp, '?')) != NULL)
4900 *p = extra_char;
4901 if (mktemp((char *)itmp) == NULL)
4902 return NULL;
4903# endif
4904# endif
4905
4906 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01004907# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004908#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909}
4910
4911#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4912/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02004913 * Convert all backslashes in fname to forward slashes in-place, unless when
4914 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
4916 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 char_u *p;
4920
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02004921 if (path_with_url(fname))
4922 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004924 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004925 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01004927 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 *p = '/';
4929}
4930#endif
4931
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00004932/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00004933 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
4934 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
4935 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 * Used for autocommands and 'wildignore'.
4937 * Returns TRUE if there is a match, FALSE otherwise.
4938 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004939 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004940match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004941 char_u *pattern, // pattern to match with
4942 regprog_T **prog, // pre-compiled regprog or NULL
4943 char_u *fname, // full path of file name
4944 char_u *sfname, // short file name or NULL
4945 char_u *tail, // tail of path
4946 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
4948 regmatch_T regmatch;
4949 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004951 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004952 if (prog != NULL)
4953 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004955 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 /*
4958 * Try for a match with the pattern with:
4959 * 1. the full file name, when the pattern has a '/'.
4960 * 2. the short file name, when the pattern has a '/'.
4961 * 3. the tail of the file name, when the pattern has no '/'.
4962 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004963 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 && ((allow_dirs
4965 && (vim_regexec(&regmatch, fname, (colnr_T)0)
4966 || (sfname != NULL
4967 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01004968 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 result = TRUE;
4970
Bram Moolenaardffa5b82014-11-19 16:38:07 +01004971 if (prog != NULL)
4972 *prog = regmatch.regprog;
4973 else
Bram Moolenaar473de612013-06-08 18:19:48 +02004974 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return result;
4976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978#if defined(FEAT_WILDIGN) || defined(PROTO)
4979/*
4980 * Return TRUE if a file matches with a pattern in "list".
4981 * "list" is a comma-separated list of patterns, like 'wildignore'.
4982 * "sfname" is the short file name or NULL, "ffname" the long file name.
4983 */
4984 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004985match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 char_u buf[100];
4988 char_u *tail;
4989 char_u *regpat;
4990 char allow_dirs;
4991 int match;
4992 char_u *p;
4993
4994 tail = gettail(sfname);
4995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004996 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 p = list;
4998 while (*p)
4999 {
5000 copy_option_part(&p, buf, 100, ",");
5001 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5002 if (regpat == NULL)
5003 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005004 match = match_file_pat(regpat, NULL, ffname, sfname,
5005 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 vim_free(regpat);
5007 if (match)
5008 return TRUE;
5009 }
5010 return FALSE;
5011}
5012#endif
5013
5014/*
5015 * Convert the given pattern "pat" which has shell style wildcards in it, into
5016 * a regular expression, and return the result in allocated memory. If there
5017 * is a directory path separator to be matched, then TRUE is put in
5018 * allow_dirs, otherwise FALSE is put there -- webb.
5019 * Handle backslashes before special characters, like "\*" and "\ ".
5020 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 * Returns NULL when out of memory.
5022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005024file_pat_to_reg_pat(
5025 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 char_u *pat_end, // first char after pattern or NULL
5027 char *allow_dirs, // Result passed back out in here
5028 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005030 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 char_u *endp;
5032 char_u *reg_pat;
5033 char_u *p;
5034 int i;
5035 int nested = 0;
5036 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 if (allow_dirs != NULL)
5039 *allow_dirs = FALSE;
5040 if (pat_end == NULL)
5041 pat_end = pat + STRLEN(pat);
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 for (p = pat; p < pat_end; p++)
5044 {
5045 switch (*p)
5046 {
5047 case '*':
5048 case '.':
5049 case ',':
5050 case '{':
5051 case '}':
5052 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055#ifdef BACKSLASH_IN_FILENAME
5056 case '\\':
5057 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005058 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 break;
5060#endif
5061 default:
5062 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005063 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 ++p;
5066 ++size;
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069 }
5070 }
5071 reg_pat = alloc(size + 1);
5072 if (reg_pat == NULL)
5073 return NULL;
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 if (pat[0] == '*')
5078 while (pat[0] == '*' && pat < pat_end - 1)
5079 pat++;
5080 else
5081 reg_pat[i++] = '^';
5082 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005083 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 while (endp - pat > 0 && *endp == '*')
5086 endp--;
5087 add_dollar = FALSE;
5088 }
5089 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5090 {
5091 switch (*p)
5092 {
5093 case '*':
5094 reg_pat[i++] = '.';
5095 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005097 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 case '~':
5101 reg_pat[i++] = '\\';
5102 reg_pat[i++] = *p;
5103 break;
5104 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 reg_pat[i++] = '.';
5106 break;
5107 case '\\':
5108 if (p[1] == NUL)
5109 break;
5110#ifdef BACKSLASH_IN_FILENAME
5111 if (!no_bslash)
5112 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 // translate:
5114 // "\x" to "\\x" e.g., "dir\file"
5115 // "\*" to "\\.*" e.g., "dir\*.c"
5116 // "\?" to "\\." e.g., "dir\??.c"
5117 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5119 && p[1] != '+')
5120 {
5121 reg_pat[i++] = '[';
5122 reg_pat[i++] = '\\';
5123 reg_pat[i++] = '/';
5124 reg_pat[i++] = ']';
5125 if (allow_dirs != NULL)
5126 *allow_dirs = TRUE;
5127 break;
5128 }
5129 }
5130#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 // Undo escaping from ExpandEscape():
5132 // foo\?bar -> foo?bar
5133 // foo\%bar -> foo%bar
5134 // foo\,bar -> foo,bar
5135 // foo\ bar -> foo bar
5136 // Don't unescape \, * and others that are also special in a
5137 // regexp.
5138 // An escaped { must be unescaped since we use magic not
5139 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (*++p == '?'
5141#ifdef BACKSLASH_IN_FILENAME
5142 && no_bslash
5143#endif
5144 )
5145 reg_pat[i++] = '?';
5146 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005147 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005148 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005149 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005150 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5151 {
5152 reg_pat[i++] = '\\';
5153 reg_pat[i++] = '{';
5154 p += 2;
5155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else
5157 {
5158 if (allow_dirs != NULL && vim_ispathsep(*p)
5159#ifdef BACKSLASH_IN_FILENAME
5160 && (!no_bslash || *p != '\\')
5161#endif
5162 )
5163 *allow_dirs = TRUE;
5164 reg_pat[i++] = '\\';
5165 reg_pat[i++] = *p;
5166 }
5167 break;
5168#ifdef BACKSLASH_IN_FILENAME
5169 case '/':
5170 reg_pat[i++] = '[';
5171 reg_pat[i++] = '\\';
5172 reg_pat[i++] = '/';
5173 reg_pat[i++] = ']';
5174 if (allow_dirs != NULL)
5175 *allow_dirs = TRUE;
5176 break;
5177#endif
5178 case '{':
5179 reg_pat[i++] = '\\';
5180 reg_pat[i++] = '(';
5181 nested++;
5182 break;
5183 case '}':
5184 reg_pat[i++] = '\\';
5185 reg_pat[i++] = ')';
5186 --nested;
5187 break;
5188 case ',':
5189 if (nested)
5190 {
5191 reg_pat[i++] = '\\';
5192 reg_pat[i++] = '|';
5193 }
5194 else
5195 reg_pat[i++] = ',';
5196 break;
5197 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005198 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005200 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *allow_dirs = TRUE;
5202 reg_pat[i++] = *p;
5203 break;
5204 }
5205 }
5206 if (add_dollar)
5207 reg_pat[i++] = '$';
5208 reg_pat[i] = NUL;
5209 if (nested != 0)
5210 {
5211 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005212 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005214 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005215 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 return reg_pat;
5218}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005219
5220#if defined(EINTR) || defined(PROTO)
5221/*
5222 * Version of read() that retries when interrupted by EINTR (possibly
5223 * by a SIGWINCH).
5224 */
5225 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005226read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005227{
5228 long ret;
5229
5230 for (;;)
5231 {
5232 ret = vim_read(fd, buf, bufsize);
5233 if (ret >= 0 || errno != EINTR)
5234 break;
5235 }
5236 return ret;
5237}
5238
5239/*
5240 * Version of write() that retries when interrupted by EINTR (possibly
5241 * by a SIGWINCH).
5242 */
5243 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005244write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005245{
5246 long ret = 0;
5247 long wlen;
5248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 // Repeat the write() so long it didn't fail, other than being interrupted
5250 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005251 while (ret < (long)bufsize)
5252 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005253 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005254 if (wlen < 0)
5255 {
5256 if (errno != EINTR)
5257 break;
5258 }
5259 else
5260 ret += wlen;
5261 }
5262 return ret;
5263}
5264#endif