diff options
Diffstat (limited to 'src/nvim/fileio.c')
-rw-r--r-- | src/nvim/fileio.c | 238 |
1 files changed, 118 insertions, 120 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index b7dbda3d99..4a33d74011 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -65,7 +65,7 @@ // For compatibility with libuv < 1.20.0 (tested on 1.18.0) #ifndef UV_FS_COPYFILE_FICLONE -#define UV_FS_COPYFILE_FICLONE 0 +# define UV_FS_COPYFILE_FICLONE 0 #endif #define HAS_BW_FLAGS @@ -105,9 +105,9 @@ struct bw_info { int bw_conv_error; // set for conversion error linenr_T bw_conv_error_lnum; // first line with error or zero linenr_T bw_start_lnum; // line number at start of buffer -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV iconv_t bw_iconv_fd; // descriptor for iconv() or -1 -# endif +#endif }; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -233,11 +233,11 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski char_u *fenc_next = NULL; // next item in 'fencs' or NULL bool advance_fenc = false; long real_size = 0; -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1 bool did_iconv = false; // true when iconv() failed and trying // 'charconvert' next -# endif +#endif bool converted = false; // true if conversion done bool notconverted = false; // true if conversion wanted but it wasn't possible char_u conv_rest[CONV_RESTLEN]; @@ -269,10 +269,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski } } - /* Remember the initial values of curbuf, curbuf->b_ffname and - * curbuf->b_fname to detect whether they are altered as a result of - * executing nasty autocommands. Also check if "fname" and "sfname" - * point to one of these values. */ + // Remember the initial values of curbuf, curbuf->b_ffname and + // curbuf->b_fname to detect whether they are altered as a result of + // executing nasty autocommands. Also check if "fname" and "sfname" + // point to one of these values. old_curbuf = curbuf; old_b_ffname = curbuf->b_ffname; old_b_fname = curbuf->b_fname; @@ -373,10 +373,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski if (perm >= 0 && !S_ISREG(perm) // not a regular file ... && !S_ISFIFO(perm) // ... or fifo && !S_ISSOCK(perm) // ... or socket -# ifdef OPEN_CHR_FILES +#ifdef OPEN_CHR_FILES && !(S_ISCHR(perm) && is_dev_fd_file(fname)) // ... or a character special file named /dev/fd/<n> -# endif +#endif ) { if (S_ISDIR(perm)) { filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); @@ -429,8 +429,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski curbuf->b_orig_mode = 0; } - /* Reset the "new file" flag. It will be set again below when the - * file doesn't exist. */ + // Reset the "new file" flag. It will be set again below when the + // file doesn't exist. curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); } @@ -493,11 +493,11 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski } else { filemess(curbuf, sfname, (char_u *)( (fd == UV_EFBIG) ? _("[File too big]") : -# if defined(UNIX) && defined(EOVERFLOW) +#if defined(UNIX) && defined(EOVERFLOW) // libuv only returns -errno in Unix and in Windows open() does not // set EOVERFLOW (fd == -EOVERFLOW) ? _("[File too big]") : -# endif +#endif _("[Permission Denied]")), 0); curbuf->b_p_ro = TRUE; // must use "w!" now } @@ -514,8 +514,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski } if (set_options) { - /* Don't change 'eol' if reading from buffer as it will already be - * correctly set when reading stdin. */ + // Don't change 'eol' if reading from buffer as it will already be + // correctly set when reading stdin. if (!read_buffer) { curbuf->b_p_eol = TRUE; curbuf->b_start_eol = TRUE; @@ -768,13 +768,13 @@ retry: } } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { // aborted conversion with iconv(), close the descriptor iconv_close(iconv_fd); iconv_fd = (iconv_t)-1; } -# endif +#endif if (advance_fenc) { /* @@ -816,8 +816,8 @@ retry: fio_flags = 0; converted = need_conversion(fenc); if (converted) { - /* "ucs-bom" means we need to check the first bytes of the file - * for a BOM. */ + // "ucs-bom" means we need to check the first bytes of the file + // for a BOM. if (STRCMP(fenc, ENC_UCSBOM) == 0) { fio_flags = FIO_UCSBOM; } else { @@ -833,13 +833,13 @@ retry: -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV // Try using iconv() if we can't convert internally. if (fio_flags == 0 && !did_iconv) { iconv_fd = (iconv_t)my_iconv_open((char_u *)"utf-8", fenc); } -# endif +#endif /* * Use the 'charconvert' expression when conversion is required @@ -847,13 +847,13 @@ retry: */ if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL && !read_fifo -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV && iconv_fd == (iconv_t)-1 -# endif +#endif ) { -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV did_iconv = false; -# endif +#endif /* Skip conversion when it's already done (retry for wrong * "fileformat"). */ if (tmpname == NULL) { @@ -872,9 +872,9 @@ retry: } } else { if (fio_flags == 0 -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV && iconv_fd == (iconv_t)-1 -# endif +#endif ) { /* Conversion wanted but we can't. * Try the next conversion in 'fileencodings' */ @@ -949,22 +949,22 @@ retry: ptr = buffer + linerest; line_start = buffer; - /* May need room to translate into. - * For iconv() we don't really know the required space, use a - * factor ICONV_MULT. - * latin1 to utf-8: 1 byte becomes up to 2 bytes - * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes - * become up to 4 bytes, size must be multiple of 2 - * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be - * multiple of 2 - * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be - * multiple of 4 */ + // May need room to translate into. + // For iconv() we don't really know the required space, use a + // factor ICONV_MULT. + // latin1 to utf-8: 1 byte becomes up to 2 bytes + // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes + // become up to 4 bytes, size must be multiple of 2 + // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be + // multiple of 2 + // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be + // multiple of 4 real_size = (int)size; -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { size = size / ICONV_MULT; } else { -# endif +#endif if (fio_flags & FIO_LATIN1) { size = size / 2; } else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) { @@ -974,9 +974,9 @@ retry: } else if (fio_flags == FIO_UCSBOM) { size = size / ICONV_MULT; // worst case } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV } -# endif +#endif if (conv_restlen > 0) { // Insert unconverted bytes from previous line. memmove(ptr, conv_rest, conv_restlen); // -V614 @@ -1000,9 +1000,9 @@ retry: p = ml_get(read_buf_lnum) + read_buf_col; n = (int)STRLEN(p); if ((int)tlen + n + 1 > size) { - /* Filled up to "size", append partial line. - * Change NL to NUL to reverse the effect done - * below. */ + // Filled up to "size", append partial line. + // Change NL to NUL to reverse the effect done + // below. n = (int)(size - tlen); for (ni = 0; ni < n; ++ni) { if (p[ni] == NL) { @@ -1026,8 +1026,8 @@ retry: ptr[tlen++] = NL; read_buf_col = 0; if (++read_buf_lnum > from) { - /* When the last line didn't have an - * end-of-line don't add it now either. */ + // When the last line didn't have an + // end-of-line don't add it now either. if (!curbuf->b_p_eol) { --tlen; } @@ -1055,9 +1055,9 @@ retry: // When we did a conversion report an error. if (fio_flags != 0 -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV || iconv_fd != (iconv_t)-1 -# endif +#endif ) { if (can_retry) { goto rewind_retry; @@ -1081,9 +1081,9 @@ retry: * leave the UTF8 checking code to do it, as it * works slightly differently. */ if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV || iconv_fd != (iconv_t)-1 -# endif +#endif )) { while (conv_restlen > 0) { *(--ptr) = bad_char_behavior; @@ -1091,12 +1091,12 @@ retry: } } fio_flags = 0; // don't convert this -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { iconv_close(iconv_fd); iconv_fd = (iconv_t)-1; } -# endif +#endif } } } @@ -1165,7 +1165,7 @@ retry: break; } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { /* * Attempt conversion of the read bytes to 'encoding' using @@ -1223,7 +1223,7 @@ retry: memmove(line_start, buffer, (size_t)linerest); size = ((char_u *)top - ptr); } -# endif +#endif if (fio_flags != 0) { unsigned int u8c; @@ -1279,8 +1279,8 @@ retry: } } - /* If there is a trailing incomplete sequence move it to - * conv_rest[]. */ + // If there is a trailing incomplete sequence move it to + // conv_rest[]. if (tail != NULL) { conv_restlen = (int)((ptr + size) - tail); memmove(conv_rest, tail, conv_restlen); @@ -1320,8 +1320,8 @@ retry: } } - /* found second word of double-word, get the first - * word and compute the resulting character */ + // found second word of double-word, get the first + // word and compute the resulting character if (fio_flags & FIO_ENDIAN_L) { u16c = (*--p << 8); u16c += *--p; @@ -1369,9 +1369,9 @@ retry: p -= len; u8c = utf_ptr2char(p); if (len == 0) { - /* Not a valid UTF-8 character, retry with - * another fenc when possible, otherwise just - * report the error. */ + // Not a valid UTF-8 character, retry with + // another fenc when possible, otherwise just + // report the error. if (can_retry) { goto rewind_retry; } @@ -1435,18 +1435,18 @@ retry: } } if (l == 1 || l > todo) { - /* Illegal byte. If we can try another encoding - * do that, unless at EOF where a truncated - * file is more likely than a conversion error. */ + // Illegal byte. If we can try another encoding + // do that, unless at EOF where a truncated + // file is more likely than a conversion error. if (can_retry && !incomplete_tail) { break; } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV // When we did a conversion report an error. if (iconv_fd != (iconv_t)-1 && conv_error == 0) { conv_error = readfile_linenr(linecnt, ptr, p); } -# endif +#endif // Remember the first linenr with an illegal byte if (conv_error == 0 && illegal_byte == 0) { illegal_byte = readfile_linenr(linecnt, ptr, p); @@ -1469,17 +1469,17 @@ retry: // Detected a UTF-8 error. rewind_retry: // Retry reading with another conversion. -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) { // iconv() failed, try 'charconvert' did_iconv = true; } else { -# endif +#endif // use next item from 'fileencodings' advance_fenc = true; -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV } -# endif +#endif file_rewind = true; goto retry; } @@ -1700,11 +1700,11 @@ failed: if (fenc_alloced) { xfree(fenc); } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { iconv_close(iconv_fd); } -# endif +#endif if (!read_buffer && !read_stdin) { close(fd); // errors are ignored @@ -1909,8 +1909,8 @@ failed: */ curbuf->b_no_eol_lnum = read_no_eol_lnum; - /* When reloading a buffer put the cursor at the first line that is - * different. */ + // When reloading a buffer put the cursor at the first line that is + // different. if (flags & READ_KEEP_UNDO) { u_find_first_changed(); } @@ -1929,8 +1929,8 @@ failed: int m = msg_scroll; int n = msg_scrolled; - /* Save the fileformat now, otherwise the buffer will be considered - * modified if the format/encoding was automatically detected. */ + // Save the fileformat now, otherwise the buffer will be considered + // modified if the format/encoding was automatically detected. if (set_options) { save_file_ff(curbuf); } @@ -2280,9 +2280,9 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ write_info.bw_conv_error = FALSE; write_info.bw_conv_error_lnum = 0; write_info.bw_restlen = 0; -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV write_info.bw_iconv_fd = (iconv_t)-1; -# endif +#endif /* After writing a file changedtick changes but we don't want to display * the line. */ @@ -2690,7 +2690,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (fd < 0) { // can't write in directory backup_copy = TRUE; } else { -# ifdef UNIX +#ifdef UNIX os_fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid); if (!os_fileinfo((char *)IObuff, &file_info) || file_info.stat.st_uid != file_info_old.stat.st_uid @@ -2698,7 +2698,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ || (long)file_info.stat.st_mode != perm) { backup_copy = TRUE; } -# endif +#endif /* Close the file before removing it, on MS-Windows we * can't delete an open file. */ close(fd); @@ -2711,7 +2711,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * Break symlinks and/or hardlinks if we've been asked to. */ if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) { -# ifdef UNIX +#ifdef UNIX bool file_info_link_ok = os_fileinfo_link((char *)fname, &file_info); // Symlinks. @@ -2728,7 +2728,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ || os_fileinfo_id_equal(&file_info, &file_info_old))) { backup_copy = FALSE; } -# endif +#endif } // make sure we have a valid backup extension to use @@ -3085,7 +3085,7 @@ nobackup: if (converted && wb_flags == 0) { -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV // Use iconv() conversion when conversion is needed and it's not done // internally. write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, (char_u *)"utf-8"); @@ -3098,7 +3098,7 @@ nobackup: } write_info.bw_first = TRUE; } else -# endif +#endif /* * When the file needs to be converted with 'charconvert' after @@ -3114,9 +3114,9 @@ nobackup: } } if (converted && wb_flags == 0 -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV && write_info.bw_iconv_fd == (iconv_t)-1 -# endif +#endif && wfname == fname) { if (!forceit) { SET_ERRMSG(_("E213: Cannot convert (add ! to write without conversion)")); @@ -3154,8 +3154,8 @@ nobackup: O_WRONLY | (append ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) - : (O_CREAT | O_TRUNC)) - , perm < 0 ? 0666 : (perm & 0777))) < 0) { + : (O_CREAT | O_TRUNC)), + perm < 0 ? 0666 : (perm & 0777))) < 0) { // A forced write will try to create a new file if the old one // is still readonly. This may also happen when the directory // is read-only. In that case the mch_remove() will fail. @@ -3644,12 +3644,12 @@ nofail: } xfree(fenc_tofree); xfree(write_info.bw_conv_buf); -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (write_info.bw_iconv_fd != (iconv_t)-1) { iconv_close(write_info.bw_iconv_fd); write_info.bw_iconv_fd = (iconv_t)-1; } -# endif +#endif #ifdef HAVE_ACL mch_free_acl(acl); #endif @@ -4034,7 +4034,7 @@ static int buf_write_bytes(struct bw_info *ip) } } -# ifdef HAVE_ICONV +#ifdef HAVE_ICONV if (ip->bw_iconv_fd != (iconv_t)-1) { const char *from; size_t fromlen; @@ -4096,7 +4096,7 @@ static int buf_write_bytes(struct bw_info *ip) buf = ip->bw_conv_buf; len = (int)((char_u *)to - ip->bw_conv_buf); } -# endif +#endif } if (ip->bw_fd < 0) { @@ -4684,7 +4684,7 @@ int vim_rename(const char_u *from, const char_u *to) } STRCPY(tempname, from); for (n = 123; n < 99999; n++) { - char * tail = (char *)path_tail(tempname); + char *tail = (char *)path_tail(tempname); snprintf(tail, (MAXPATHL + 1) - (tail - (char *)tempname - 1), "%d", n); if (!os_path_exists(tempname)) { @@ -4804,15 +4804,15 @@ int check_timestamps(int focus) { int didit = 0; - /* Don't check timestamps while system() or another low-level function may - * cause us to lose and gain focus. */ + // Don't check timestamps while system() or another low-level function may + // cause us to lose and gain focus. if (no_check_timestamps > 0) { return FALSE; } - /* Avoid doing a check twice. The OK/Reload dialog can cause a focus - * event and we would keep on checking if the file is steadily growing. - * Do check again after typing something. */ + // Avoid doing a check twice. The OK/Reload dialog can cause a focus + // event and we would keep on checking if the file is steadily growing. + // Do check again after typing something. if (focus && did_check_timestamps) { need_check_timestamps = TRUE; return FALSE; @@ -5025,8 +5025,8 @@ int buf_check_timestamp(buf_T *buf) mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); mesg2 = _("See \":help W16\" for more info."); } else { - /* Only timestamp changed, store it to avoid a warning - * in check_mtime() later. */ + // Only timestamp changed, store it to avoid a warning + // in check_mtime() later. buf->b_mtime_read = buf->b_mtime; } } @@ -5538,8 +5538,8 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) /// @param no_bslash Don't use a backward slash as pathsep /// /// @return NULL on failure. -char_u * file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allow_dirs, - int no_bslash) +char_u *file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allow_dirs, + int no_bslash) FUNC_ATTR_NONNULL_ARG(1) { const char_u *endp; @@ -5623,12 +5623,11 @@ char_u * file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *all } #ifdef BACKSLASH_IN_FILENAME if (!no_bslash) { - /* translate: - * "\x" to "\\x" e.g., "dir\file" - * "\*" to "\\.*" e.g., "dir\*.c" - * "\?" to "\\." e.g., "dir\??.c" - * "\+" to "\+" e.g., "fileX\+.c" - */ + // translate: + // "\x" to "\\x" e.g., "dir\file" + // "\*" to "\\.*" e.g., "dir\*.c" + // "\?" to "\\." e.g., "dir\??.c" + // "\+" to "\+" e.g., "fileX\+.c" if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') && p[1] != '+') { reg_pat[i++] = '['; @@ -5642,16 +5641,15 @@ char_u * file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *all } } #endif - /* Undo escaping from ExpandEscape(): - * foo\?bar -> foo?bar - * foo\%bar -> foo%bar - * foo\,bar -> foo,bar - * foo\ bar -> foo bar - * Don't unescape \, * and others that are also special in a - * regexp. - * An escaped { must be unescaped since we use magic not - * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". - */ + // Undo escaping from ExpandEscape(): + // foo\?bar -> foo?bar + // foo\%bar -> foo%bar + // foo\,bar -> foo,bar + // foo\ bar -> foo bar + // Don't unescape \, * and others that are also special in a + // regexp. + // An escaped { must be unescaped since we use magic not + // verymagic. Use "\\\{n,m\}"" to get "\{n,m}". if (*++p == '?' #ifdef BACKSLASH_IN_FILENAME && no_bslash |