refactor(fileio.c): reduce scope of locals

This commit is contained in:
Lewis Russell 2023-01-26 15:17:23 +00:00 committed by GitHub
parent 41aa5ce3eb
commit 3c2bb1b2be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -968,13 +968,11 @@ retry:
if (read_buf_lnum > from) { if (read_buf_lnum > from) {
size = 0; size = 0;
} else { } else {
int n, ni; int ni;
long tlen; long tlen = 0;
tlen = 0;
for (;;) { for (;;) {
p = (char_u *)ml_get(read_buf_lnum) + read_buf_col; p = (char_u *)ml_get(read_buf_lnum) + read_buf_col;
n = (int)strlen((char *)p); int n = (int)strlen((char *)p);
if ((int)tlen + n + 1 > size) { if ((int)tlen + n + 1 > size) {
// Filled up to "size", append partial line. // Filled up to "size", append partial line.
// Change NL to NUL to reverse the effect done // Change NL to NUL to reverse the effect done
@ -2126,7 +2124,6 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
bool errmsg_allocated = false; bool errmsg_allocated = false;
char *buffer; char *buffer;
char smallbuf[SMBUFSIZE]; char smallbuf[SMBUFSIZE];
char *backup_ext;
int bufsize; int bufsize;
long perm; // file permissions long perm; // file permissions
int retval = OK; int retval = OK;
@ -2555,8 +2552,6 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
if ((bkc & BKC_YES) || append) { // "yes" if ((bkc & BKC_YES) || append) { // "yes"
backup_copy = true; backup_copy = true;
} else if ((bkc & BKC_AUTO)) { // "auto" } else if ((bkc & BKC_AUTO)) { // "auto"
int i;
// Don't rename the file when: // Don't rename the file when:
// - it's a hard link // - it's a hard link
// - it's a symbolic link // - it's a symbolic link
@ -2571,7 +2566,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
// First find a file name that doesn't exist yet (use some // First find a file name that doesn't exist yet (use some
// arbitrary numbers). // arbitrary numbers).
STRCPY(IObuff, fname); STRCPY(IObuff, fname);
for (i = 4913;; i += 123) { for (int i = 4913;; i += 123) {
char *tail = path_tail(IObuff); char *tail = path_tail(IObuff);
size_t size = (size_t)(tail - IObuff); size_t size = (size_t)(tail - IObuff);
snprintf(tail, IOSIZE - size, "%d", i); snprintf(tail, IOSIZE - size, "%d", i);
@ -2624,18 +2619,10 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
} }
// make sure we have a valid backup extension to use // make sure we have a valid backup extension to use
if (*p_bex == NUL) { char *backup_ext = *p_bex == NUL ? ".bak" : p_bex;
backup_ext = ".bak";
} else {
backup_ext = p_bex;
}
if (backup_copy) { if (backup_copy) {
char *wp;
int some_error = false; int some_error = false;
char *dirp;
char *rootname;
char *p;
// Try to make the backup in each directory in the 'bdir' option. // Try to make the backup in each directory in the 'bdir' option.
// //
@ -2647,11 +2634,11 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
// //
// For these reasons, the existing writable file must be truncated // For these reasons, the existing writable file must be truncated
// and reused. Creation of a backup COPY will be attempted. // and reused. Creation of a backup COPY will be attempted.
dirp = p_bdir; char *dirp = p_bdir;
while (*dirp) { while (*dirp) {
// Isolate one directory name, using an entry in 'bdir'. // Isolate one directory name, using an entry in 'bdir'.
size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ",");
p = IObuff + dir_len; char *p = IObuff + dir_len;
bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2];
if (trailing_pathseps) { if (trailing_pathseps) {
IObuff[dir_len - 2] = NUL; IObuff[dir_len - 2] = NUL;
@ -2674,7 +2661,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
} }
} }
rootname = get_file_in_dir(fname, IObuff); char *rootname = get_file_in_dir(fname, IObuff);
if (rootname == NULL) { if (rootname == NULL) {
some_error = true; // out of memory some_error = true; // out of memory
goto nobackup; goto nobackup;
@ -2710,7 +2697,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
// delete an existing one, and try to use another name instead. // delete an existing one, and try to use another name instead.
// Change one character, just before the extension. // Change one character, just before the extension.
// //
wp = backup + strlen(backup) - 1 - strlen(backup_ext); char *wp = backup + strlen(backup) - 1 - strlen(backup_ext);
if (wp < backup) { // empty file name ??? if (wp < backup) { // empty file name ???
wp = backup; wp = backup;
} }
@ -2781,10 +2768,6 @@ nobackup:
} }
SET_ERRMSG(NULL); SET_ERRMSG(NULL);
} else { } else {
char *dirp;
char *p;
char *rootname;
// Make a backup by renaming the original file. // Make a backup by renaming the original file.
// If 'cpoptions' includes the "W" flag, we don't want to // If 'cpoptions' includes the "W" flag, we don't want to
@ -2798,11 +2781,11 @@ nobackup:
// Form the backup file name - change path/fo.o.h to // Form the backup file name - change path/fo.o.h to
// path/fo.o.h.bak Try all directories in 'backupdir', first one // path/fo.o.h.bak Try all directories in 'backupdir', first one
// that works is used. // that works is used.
dirp = p_bdir; char *dirp = p_bdir;
while (*dirp) { while (*dirp) {
// Isolate one directory name and make the backup file name. // Isolate one directory name and make the backup file name.
size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ",");
p = IObuff + dir_len; char *p = IObuff + dir_len;
bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2];
if (trailing_pathseps) { if (trailing_pathseps) {
IObuff[dir_len - 2] = NUL; IObuff[dir_len - 2] = NUL;
@ -2826,7 +2809,7 @@ nobackup:
} }
if (backup == NULL) { if (backup == NULL) {
rootname = get_file_in_dir(fname, IObuff); char *rootname = get_file_in_dir(fname, IObuff);
if (rootname == NULL) { if (rootname == NULL) {
backup = NULL; backup = NULL;
} else { } else {
@ -3690,9 +3673,7 @@ static bool msg_add_fileformat(int eol_type)
/// Append line and character count to IObuff. /// Append line and character count to IObuff.
void msg_add_lines(int insert_space, long lnum, off_T nchars) void msg_add_lines(int insert_space, long lnum, off_T nchars)
{ {
char *p; char *p = IObuff + strlen(IObuff);
p = IObuff + strlen(IObuff);
if (insert_space) { if (insert_space) {
*p++ = ' '; *p++ = ' ';
@ -3758,7 +3739,6 @@ static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) F
/// @return FAIL for failure, OK otherwise. /// @return FAIL for failure, OK otherwise.
static int buf_write_bytes(struct bw_info *ip) static int buf_write_bytes(struct bw_info *ip)
{ {
int wlen;
char *buf = ip->bw_buf; // data to write char *buf = ip->bw_buf; // data to write
int len = ip->bw_len; // length of data int len = ip->bw_len; // length of data
#ifdef HAS_BW_FLAGS #ifdef HAS_BW_FLAGS
@ -3767,37 +3747,27 @@ static int buf_write_bytes(struct bw_info *ip)
// Skip conversion when writing the BOM. // Skip conversion when writing the BOM.
if (!(flags & FIO_NOCONVERT)) { if (!(flags & FIO_NOCONVERT)) {
char *p;
unsigned c;
int n;
if (flags & FIO_UTF8) { if (flags & FIO_UTF8) {
// Convert latin1 in the buffer to UTF-8 in the file. // Convert latin1 in the buffer to UTF-8 in the file.
p = ip->bw_conv_buf; // translate to buffer char *p = ip->bw_conv_buf; // translate to buffer
for (wlen = 0; wlen < len; wlen++) { for (int wlen = 0; wlen < len; wlen++) {
p += utf_char2bytes((uint8_t)buf[wlen], p); p += utf_char2bytes((uint8_t)buf[wlen], p);
} }
buf = ip->bw_conv_buf; buf = ip->bw_conv_buf;
len = (int)(p - ip->bw_conv_buf); len = (int)(p - ip->bw_conv_buf);
} else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) { } else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) {
unsigned c;
int n = 0;
// Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or // Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
// Latin1 chars in the file. // Latin1 chars in the file.
if (flags & FIO_LATIN1) { // translate in-place (can only get shorter) or to buffer
p = buf; // translate in-place (can only get shorter) char *p = flags & FIO_LATIN1 ? buf : ip->bw_conv_buf;
} else { for (int wlen = 0; wlen < len; wlen += n) {
p = ip->bw_conv_buf; // translate to buffer
}
for (wlen = 0; wlen < len; wlen += n) {
if (wlen == 0 && ip->bw_restlen != 0) { if (wlen == 0 && ip->bw_restlen != 0) {
int l;
// Use remainder of previous call. Append the start of // Use remainder of previous call. Append the start of
// buf[] to get a full sequence. Might still be too // buf[] to get a full sequence. Might still be too
// short! // short!
l = CONV_RESTLEN - ip->bw_restlen; int l = MIN(len, CONV_RESTLEN - ip->bw_restlen);
if (l > len) {
l = len;
}
memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
n = utf_ptr2len_len((char *)ip->bw_rest, ip->bw_restlen + l); n = utf_ptr2len_len((char *)ip->bw_rest, ip->bw_restlen + l);
if (n > ip->bw_restlen + len) { if (n > ip->bw_restlen + len) {
@ -3864,18 +3834,15 @@ static int buf_write_bytes(struct bw_info *ip)
if (ip->bw_iconv_fd != (iconv_t)-1) { if (ip->bw_iconv_fd != (iconv_t)-1) {
const char *from; const char *from;
size_t fromlen; size_t fromlen;
char *to;
size_t tolen; size_t tolen;
// Convert with iconv(). // Convert with iconv().
if (ip->bw_restlen > 0) { if (ip->bw_restlen > 0) {
char *fp;
// Need to concatenate the remainder of the previous call and // Need to concatenate the remainder of the previous call and
// the bytes of the current call. Use the end of the // the bytes of the current call. Use the end of the
// conversion buffer for this. // conversion buffer for this.
fromlen = (size_t)len + (size_t)ip->bw_restlen; fromlen = (size_t)len + (size_t)ip->bw_restlen;
fp = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; char *fp = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
memmove(fp + ip->bw_restlen, buf, (size_t)len); memmove(fp + ip->bw_restlen, buf, (size_t)len);
from = fp; from = fp;
@ -3885,7 +3852,7 @@ static int buf_write_bytes(struct bw_info *ip)
fromlen = (size_t)len; fromlen = (size_t)len;
tolen = ip->bw_conv_buflen; tolen = ip->bw_conv_buflen;
} }
to = ip->bw_conv_buf; char *to = ip->bw_conv_buf;
if (ip->bw_first) { if (ip->bw_first) {
size_t save_len = tolen; size_t save_len = tolen;
@ -3925,7 +3892,7 @@ static int buf_write_bytes(struct bw_info *ip)
// Only checking conversion, which is OK if we get here. // Only checking conversion, which is OK if we get here.
return OK; return OK;
} }
wlen = (int)write_eintr(ip->bw_fd, buf, (size_t)len); int wlen = (int)write_eintr(ip->bw_fd, buf, (size_t)len);
return (wlen < len) ? FAIL : OK; return (wlen < len) ? FAIL : OK;
} }
@ -3940,7 +3907,6 @@ static bool ucs2bytes(unsigned c, char **pp, int flags) FUNC_ATTR_NONNULL_ALL
{ {
char_u *p = (char_u *)(*pp); char_u *p = (char_u *)(*pp);
bool error = false; bool error = false;
int cc;
if (flags & FIO_UCS4) { if (flags & FIO_UCS4) {
if (flags & FIO_ENDIAN_L) { if (flags & FIO_ENDIAN_L) {
@ -3963,7 +3929,7 @@ static bool ucs2bytes(unsigned c, char **pp, int flags) FUNC_ATTR_NONNULL_ALL
if (c >= 0x100000) { if (c >= 0x100000) {
error = true; error = true;
} }
cc = (int)(((c >> 10) & 0x3ff) + 0xd800); int cc = (int)(((c >> 10) & 0x3ff) + 0xd800);
if (flags & FIO_ENDIAN_L) { if (flags & FIO_ENDIAN_L) {
*p++ = (uint8_t)cc; *p++ = (uint8_t)cc;
*p++ = (uint8_t)(cc >> 8); *p++ = (uint8_t)(cc >> 8);
@ -4006,7 +3972,6 @@ static bool need_conversion(const char *fenc)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
int same_encoding; int same_encoding;
int enc_flags;
int fenc_flags; int fenc_flags;
if (*fenc == NUL || strcmp(p_enc, fenc) == 0) { if (*fenc == NUL || strcmp(p_enc, fenc) == 0) {
@ -4015,7 +3980,7 @@ static bool need_conversion(const char *fenc)
} else { } else {
// Ignore difference between "ansi" and "latin1", "ucs-4" and // Ignore difference between "ansi" and "latin1", "ucs-4" and
// "ucs-4be", etc. // "ucs-4be", etc.
enc_flags = get_fio_flags(p_enc); int enc_flags = get_fio_flags(p_enc);
fenc_flags = get_fio_flags(fenc); fenc_flags = get_fio_flags(fenc);
same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
} }
@ -4036,12 +4001,10 @@ static bool need_conversion(const char *fenc)
/// @param name string to check for encoding /// @param name string to check for encoding
static int get_fio_flags(const char *name) static int get_fio_flags(const char *name)
{ {
int prop;
if (*name == NUL) { if (*name == NUL) {
name = p_enc; name = p_enc;
} }
prop = enc_canon_props(name); int prop = enc_canon_props(name);
if (prop & ENC_UNICODE) { if (prop & ENC_UNICODE) {
if (prop & ENC_2BYTE) { if (prop & ENC_2BYTE) {
if (prop & ENC_ENDIAN_L) { if (prop & ENC_ENDIAN_L) {
@ -4121,10 +4084,7 @@ static char *check_for_bom(const char *p_in, long size, int *lenp, int flags)
/// @return the length of the BOM (zero when no BOM). /// @return the length of the BOM (zero when no BOM).
static int make_bom(char_u *buf, char *name) static int make_bom(char_u *buf, char *name)
{ {
int flags; int flags = get_fio_flags(name);
char *p;
flags = get_fio_flags(name);
// Can't put a BOM in a non-Unicode file. // Can't put a BOM in a non-Unicode file.
if (flags == FIO_LATIN1 || flags == 0) { if (flags == FIO_LATIN1 || flags == 0) {
@ -4137,7 +4097,7 @@ static int make_bom(char_u *buf, char *name)
buf[2] = 0xbf; buf[2] = 0xbf;
return 3; return 3;
} }
p = (char *)buf; char *p = (char *)buf;
(void)ucs2bytes(0xfeff, &p, flags); (void)ucs2bytes(0xfeff, &p, flags);
return (int)((char_u *)p - buf); return (int)((char_u *)p - buf);
} }
@ -4153,8 +4113,6 @@ static int make_bom(char_u *buf, char *name)
/// name. /// name.
void shorten_buf_fname(buf_T *buf, char *dirname, int force) void shorten_buf_fname(buf_T *buf, char *dirname, int force)
{ {
char *p;
if (buf->b_fname != NULL if (buf->b_fname != NULL
&& !bt_nofilename(buf) && !bt_nofilename(buf)
&& !path_with_url(buf->b_fname) && !path_with_url(buf->b_fname)
@ -4164,7 +4122,7 @@ void shorten_buf_fname(buf_T *buf, char *dirname, int force)
if (buf->b_sfname != buf->b_ffname) { if (buf->b_sfname != buf->b_ffname) {
XFREE_CLEAR(buf->b_sfname); XFREE_CLEAR(buf->b_sfname);
} }
p = path_shorten_fname(buf->b_ffname, dirname); char *p = path_shorten_fname(buf->b_ffname, dirname);
if (p != NULL) { if (p != NULL) {
buf->b_sfname = xstrdup(p); buf->b_sfname = xstrdup(p);
buf->b_fname = buf->b_sfname; buf->b_fname = buf->b_sfname;
@ -4461,10 +4419,7 @@ int vim_rename(const char *from, const char *to)
{ {
int fd_in; int fd_in;
int fd_out; int fd_out;
int n;
char *errmsg = NULL; char *errmsg = NULL;
char *buffer;
long perm;
#ifdef HAVE_ACL #ifdef HAVE_ACL
vim_acl_T acl; // ACL from original file vim_acl_T acl; // ACL from original file
#endif #endif
@ -4506,7 +4461,7 @@ int vim_rename(const char *from, const char *to)
return -1; return -1;
} }
STRCPY(tempname, from); STRCPY(tempname, from);
for (n = 123; n < 99999; n++) { for (int n = 123; n < 99999; n++) {
char *tail = path_tail(tempname); char *tail = path_tail(tempname);
snprintf(tail, (size_t)((MAXPATHL + 1) - (tail - tempname - 1)), "%d", n); snprintf(tail, (size_t)((MAXPATHL + 1) - (tail - tempname - 1)), "%d", n);
@ -4540,7 +4495,7 @@ int vim_rename(const char *from, const char *to)
} }
// Rename() failed, try copying the file. // Rename() failed, try copying the file.
perm = os_getperm(from); long perm = os_getperm(from);
#ifdef HAVE_ACL #ifdef HAVE_ACL
// For systems that support ACL: get the ACL from the original file. // For systems that support ACL: get the ACL from the original file.
acl = os_get_acl(from); acl = os_get_acl(from);
@ -4566,7 +4521,7 @@ int vim_rename(const char *from, const char *to)
// Avoid xmalloc() here as vim_rename() is called by buf_write() when nvim // Avoid xmalloc() here as vim_rename() is called by buf_write() when nvim
// is `preserve_exit()`ing. // is `preserve_exit()`ing.
buffer = try_malloc(BUFSIZE); char *buffer = try_malloc(BUFSIZE);
if (buffer == NULL) { if (buffer == NULL) {
close(fd_out); close(fd_out);
close(fd_in); close(fd_in);
@ -4576,6 +4531,7 @@ int vim_rename(const char *from, const char *to)
return -1; return -1;
} }
int n;
while ((n = (int)read_eintr(fd_in, buffer, BUFSIZE)) > 0) { while ((n = (int)read_eintr(fd_in, buffer, BUFSIZE)) > 0) {
if (write_eintr(fd_out, buffer, (size_t)n) != n) { if (write_eintr(fd_out, buffer, (size_t)n) != n) {
errmsg = _("E208: Error writing to \"%s\""); errmsg = _("E208: Error writing to \"%s\"");
@ -4619,8 +4575,6 @@ static int already_warned = false;
/// @return true if some message was written (screen should be redrawn and cursor positioned). /// @return true if some message was written (screen should be redrawn and cursor positioned).
int check_timestamps(int focus) int check_timestamps(int focus)
{ {
int didit = 0;
// Don't check timestamps while system() or another low-level function may // Don't check timestamps while system() or another low-level function may
// cause us to lose and gain focus. // cause us to lose and gain focus.
if (no_check_timestamps > 0) { if (no_check_timestamps > 0) {
@ -4635,6 +4589,8 @@ int check_timestamps(int focus)
return false; return false;
} }
int didit = 0;
if (!stuff_empty() || global_busy || !typebuf_typed() if (!stuff_empty() || global_busy || !typebuf_typed()
|| autocmd_busy || curbuf->b_ro_locked > 0 || autocmd_busy || curbuf->b_ro_locked > 0
|| allbuf_lock > 0) { || allbuf_lock > 0) {
@ -4678,13 +4634,11 @@ static int move_lines(buf_T *frombuf, buf_T *tobuf)
{ {
buf_T *tbuf = curbuf; buf_T *tbuf = curbuf;
int retval = OK; int retval = OK;
linenr_T lnum;
char *p;
// Copy the lines in "frombuf" to "tobuf". // Copy the lines in "frombuf" to "tobuf".
curbuf = tobuf; curbuf = tobuf;
for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; lnum++) { for (linenr_T lnum = 1; lnum <= frombuf->b_ml.ml_line_count; lnum++) {
p = xstrdup(ml_get_buf(frombuf, lnum, false)); char *p = xstrdup(ml_get_buf(frombuf, lnum, false));
if (ml_append(lnum - 1, p, 0, false) == FAIL) { if (ml_append(lnum - 1, p, 0, false) == FAIL) {
xfree(p); xfree(p);
retval = FAIL; retval = FAIL;
@ -4696,7 +4650,7 @@ static int move_lines(buf_T *frombuf, buf_T *tobuf)
// Delete all the lines in "frombuf". // Delete all the lines in "frombuf".
if (retval != FAIL) { if (retval != FAIL) {
curbuf = frombuf; curbuf = frombuf;
for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; lnum--) { for (linenr_T lnum = curbuf->b_ml.ml_line_count; lnum > 0; lnum--) {
if (ml_delete(lnum, false) == FAIL) { if (ml_delete(lnum, false) == FAIL) {
// Oops! We could try putting back the saved lines, but that // Oops! We could try putting back the saved lines, but that
// might fail again... // might fail again...
@ -4720,7 +4674,6 @@ int buf_check_timestamp(buf_T *buf)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
int retval = 0; int retval = 0;
char *path;
char *mesg = NULL; char *mesg = NULL;
char *mesg2 = ""; char *mesg2 = "";
bool helpmesg = false; bool helpmesg = false;
@ -4735,8 +4688,6 @@ int buf_check_timestamp(buf_T *buf)
uint64_t orig_size = buf->b_orig_size; uint64_t orig_size = buf->b_orig_size;
int orig_mode = buf->b_orig_mode; int orig_mode = buf->b_orig_mode;
static bool busy = false; static bool busy = false;
char *s;
char *reason;
bufref_T bufref; bufref_T bufref;
set_bufref(&bufref, buf); set_bufref(&bufref, buf);
@ -4784,6 +4735,7 @@ int buf_check_timestamp(buf_T *buf)
// was set, the global option value otherwise. // was set, the global option value otherwise.
reload = RELOAD_NORMAL; reload = RELOAD_NORMAL;
} else { } else {
char *reason;
if (!file_info_ok) { if (!file_info_ok) {
reason = "deleted"; reason = "deleted";
} else if (bufIsChanged(buf)) { } else if (bufIsChanged(buf)) {
@ -4810,7 +4762,7 @@ int buf_check_timestamp(buf_T *buf)
if (!bufref_valid(&bufref)) { if (!bufref_valid(&bufref)) {
emsg(_("E246: FileChangedShell autocommand deleted buffer")); emsg(_("E246: FileChangedShell autocommand deleted buffer"));
} }
s = get_vim_var_str(VV_FCS_CHOICE); char *s = get_vim_var_str(VV_FCS_CHOICE);
if (strcmp(s, "reload") == 0 && *reason != 'd') { if (strcmp(s, "reload") == 0 && *reason != 'd') {
reload = RELOAD_NORMAL; reload = RELOAD_NORMAL;
} else if (strcmp(s, "edit") == 0) { } else if (strcmp(s, "edit") == 0) {
@ -4863,7 +4815,7 @@ int buf_check_timestamp(buf_T *buf)
} }
if (mesg != NULL) { if (mesg != NULL) {
path = home_replace_save(buf, buf->b_fname); char *path = home_replace_save(buf, buf->b_fname);
if (!helpmesg) { if (!helpmesg) {
mesg2 = ""; mesg2 = "";
} }
@ -4946,8 +4898,6 @@ int buf_check_timestamp(buf_T *buf)
void buf_reload(buf_T *buf, int orig_mode, bool reload_options) void buf_reload(buf_T *buf, int orig_mode, bool reload_options)
{ {
exarg_T ea; exarg_T ea;
pos_T old_cursor;
linenr_T old_topline;
int old_ro = buf->b_p_ro; int old_ro = buf->b_p_ro;
buf_T *savebuf; buf_T *savebuf;
bufref_T bufref; bufref_T bufref;
@ -4967,8 +4917,8 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options)
prep_exarg(&ea, buf); prep_exarg(&ea, buf);
} }
old_cursor = curwin->w_cursor; pos_T old_cursor = curwin->w_cursor;
old_topline = curwin->w_topline; linenr_T old_topline = curwin->w_topline;
if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) { if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) {
// Save all the text, so that the reload can be undone. // Save all the text, so that the reload can be undone.
@ -5454,24 +5404,19 @@ bool match_file_pat(char *pattern, regprog_T **prog, char *fname, char *sfname,
bool match_file_list(char *list, char *sfname, char *ffname) bool match_file_list(char *list, char *sfname, char *ffname)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1, 3) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1, 3)
{ {
char buf[100]; char *tail = path_tail(sfname);
char *tail;
char *regpat;
char allow_dirs;
bool match;
char *p;
tail = path_tail(sfname);
// try all patterns in 'wildignore' // try all patterns in 'wildignore'
p = list; char *p = list;
while (*p) { while (*p) {
char buf[100];
copy_option_part(&p, buf, ARRAY_SIZE(buf), ","); copy_option_part(&p, buf, ARRAY_SIZE(buf), ",");
regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, false); char allow_dirs;
char *regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, false);
if (regpat == NULL) { if (regpat == NULL) {
break; break;
} }
match = match_file_pat(regpat, NULL, ffname, sfname, tail, (int)allow_dirs); bool match = match_file_pat(regpat, NULL, ffname, sfname, tail, (int)allow_dirs);
xfree(regpat); xfree(regpat);
if (match) { if (match) {
return true; return true;
@ -5494,15 +5439,6 @@ bool match_file_list(char *list, char *sfname, char *ffname)
char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs, int no_bslash) char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs, int no_bslash)
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(1)
{ {
const char *endp;
char *reg_pat;
const char *p;
int nested = 0;
bool add_dollar = true;
if (allow_dirs != NULL) {
*allow_dirs = false;
}
if (pat_end == NULL) { if (pat_end == NULL) {
pat_end = pat + strlen(pat); pat_end = pat + strlen(pat);
} }
@ -5511,9 +5447,13 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
return xstrdup("^$"); return xstrdup("^$");
} }
if (allow_dirs != NULL) {
*allow_dirs = false;
}
size_t size = 2; // '^' at start, '$' at end. size_t size = 2; // '^' at start, '$' at end.
for (p = pat; p < pat_end; p++) { for (const char *p = pat; p < pat_end; p++) {
switch (*p) { switch (*p) {
case '*': case '*':
case '.': case '.':
@ -5534,7 +5474,7 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
break; break;
} }
} }
reg_pat = xmalloc(size + 1); char *reg_pat = xmalloc(size + 1);
size_t i = 0; size_t i = 0;
@ -5545,14 +5485,16 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
} else { } else {
reg_pat[i++] = '^'; reg_pat[i++] = '^';
} }
endp = pat_end - 1; const char *endp = pat_end - 1;
bool add_dollar = true;
if (endp >= pat && *endp == '*') { if (endp >= pat && *endp == '*') {
while (endp - pat > 0 && *endp == '*') { while (endp - pat > 0 && *endp == '*') {
endp--; endp--;
} }
add_dollar = false; add_dollar = false;
} }
for (p = pat; *p && nested >= 0 && p <= endp; p++) { int nested = 0;
for (const char *p = pat; *p && nested >= 0 && p <= endp; p++) {
switch (*p) { switch (*p) {
case '*': case '*':
reg_pat[i++] = '.'; reg_pat[i++] = '.';