feat!: make iconv a non-optional dep

This commit is contained in:
Lewis Russell 2023-01-23 16:33:45 +00:00 committed by GitHub
parent 3b75485043
commit f08051c2e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 37 additions and 133 deletions

View File

@ -342,11 +342,6 @@ install_helper(
FILES ${CMAKE_SOURCE_DIR}/src/man/nvim.1
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
option(ENABLE_LIBICONV "enable libiconv" ON)
if(ENABLE_LIBICONV)
find_package(Iconv REQUIRED)
endif()
#
# Go down the tree.
#

View File

@ -51,11 +51,6 @@ check_function_exists(getpwent HAVE_GETPWENT)
check_function_exists(getpwnam HAVE_GETPWNAM)
check_function_exists(getpwuid HAVE_GETPWUID)
check_function_exists(readv HAVE_READV)
if(Iconv_FOUND)
set(HAVE_ICONV 1)
endif()
check_function_exists(opendir HAVE_OPENDIR)
check_function_exists(readlink HAVE_READLINK)
check_function_exists(setpgid HAVE_SETPGID)

View File

@ -24,7 +24,6 @@
#cmakedefine HAVE_GETPWENT
#cmakedefine HAVE_GETPWNAM
#cmakedefine HAVE_GETPWUID
#cmakedefine HAVE_ICONV
#cmakedefine HAVE_LANGINFO_H
#cmakedefine HAVE_LOCALE_H
#cmakedefine HAVE_NL_LANGINFO_CODESET

View File

@ -45,6 +45,8 @@ The following changes may require adaptations in user config or plugins.
- `printheader`
- `printmbcharset`
• libiconv is now a required build dependency.
==============================================================================
NEW FEATURES *news-features*

View File

@ -34,12 +34,9 @@ find_package(LIBVTERM 0.3 REQUIRED)
target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LIBVTERM_INCLUDE_DIRS})
target_link_libraries(main_lib INTERFACE ${LIBVTERM_LIBRARIES})
if(Iconv_FOUND)
target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${Iconv_INCLUDE_DIRS})
if(Iconv_LIBRARIES)
target_link_libraries(main_lib INTERFACE ${Iconv_LIBRARIES})
endif()
endif()
find_package(Iconv REQUIRED)
target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${Iconv_INCLUDE_DIRS})
target_link_libraries(main_lib INTERFACE ${Iconv_LIBRARIES})
option(ENABLE_LIBINTL "enable libintl" ON)
if(ENABLE_LIBINTL)

View File

@ -3082,9 +3082,7 @@ static void f_has(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
"fork",
#endif
"gettext",
#if defined(HAVE_ICONV)
"iconv",
#endif
"insert_expand",
"jumplist",
"keymap",

View File

@ -118,9 +118,7 @@ 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
iconv_t bw_iconv_fd; // descriptor for iconv() or -1
#endif
};
#ifdef INCLUDE_GENERATED_DECLARATIONS
@ -248,11 +246,9 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip,
char *fenc_next = NULL; // next item in 'fencs' or NULL
bool advance_fenc = false;
long real_size = 0;
#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
bool converted = false; // true if conversion done
bool notconverted = false; // true if conversion wanted but it wasn't possible
char conv_rest[CONV_RESTLEN];
@ -779,13 +775,11 @@ retry:
}
}
#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
if (advance_fenc) {
// Try the next entry in 'fileencodings'.
@ -838,25 +832,17 @@ retry:
fio_flags = get_fio_flags(fenc);
}
#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("utf-8", fenc);
}
#endif
// Use the 'charconvert' expression when conversion is required
// and we can't do it internally or with iconv().
if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
&& !read_fifo
#ifdef HAVE_ICONV
&& iconv_fd == (iconv_t)-1
#endif
) {
#ifdef HAVE_ICONV
&& !read_fifo && iconv_fd == (iconv_t)-1) {
did_iconv = false;
#endif
// Skip conversion when it's already done (retry for wrong
// "fileformat").
if (tmpname == NULL) {
@ -874,11 +860,7 @@ retry:
}
}
} else {
if (fio_flags == 0
#ifdef HAVE_ICONV
&& iconv_fd == (iconv_t)-1
#endif
) {
if (fio_flags == 0 && iconv_fd == (iconv_t)-1) {
// Conversion wanted but we can't.
// Try the next conversion in 'fileencodings'
advance_fenc = true;
@ -961,12 +943,9 @@ retry:
// 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
if (iconv_fd != (iconv_t)-1) {
size = size / ICONV_MULT;
} else {
#endif
if (fio_flags & FIO_LATIN1) {
} else if (fio_flags & FIO_LATIN1) {
size = size / 2;
} else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) {
size = (size * 2 / 3) & ~1;
@ -975,9 +954,7 @@ retry:
} else if (fio_flags == FIO_UCSBOM) {
size = size / ICONV_MULT; // worst case
}
#ifdef HAVE_ICONV
}
#endif
if (conv_restlen > 0) {
// Insert unconverted bytes from previous line.
memmove(ptr, conv_rest, (size_t)conv_restlen); // -V614
@ -1049,11 +1026,7 @@ retry:
// not be converted. Truncated file?
// When we did a conversion report an error.
if (fio_flags != 0
#ifdef HAVE_ICONV
|| iconv_fd != (iconv_t)-1
#endif
) {
if (fio_flags != 0 || iconv_fd != (iconv_t)-1) {
if (can_retry) {
goto rewind_retry;
}
@ -1074,23 +1047,17 @@ retry:
// character if we were converting; if we weren't,
// 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
|| iconv_fd != (iconv_t)-1
#endif
)) { // NOLINT(whitespace/parens)
if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 || iconv_fd != (iconv_t)-1)) {
while (conv_restlen > 0) {
*(--ptr) = (char)bad_char_behavior;
conv_restlen--;
}
}
fio_flags = 0; // don't convert this
#ifdef HAVE_ICONV
if (iconv_fd != (iconv_t)-1) {
iconv_close(iconv_fd);
iconv_fd = (iconv_t)-1;
}
#endif
}
}
}
@ -1155,7 +1122,6 @@ retry:
break;
}
#ifdef HAVE_ICONV
if (iconv_fd != (iconv_t)-1) {
// Attempt conversion of the read bytes to 'encoding' using iconv().
const char *fromp = ptr;
@ -1202,7 +1168,6 @@ retry:
memmove(line_start, buffer, (size_t)linerest);
size = (top - ptr);
}
#endif
if (fio_flags != 0) {
unsigned int u8c;
@ -1420,12 +1385,12 @@ retry:
if (can_retry && !incomplete_tail) {
break;
}
#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, (char *)p);
}
#endif
// Remember the first linenr with an illegal byte
if (conv_error == 0 && illegal_byte == 0) {
illegal_byte = readfile_linenr(linecnt, ptr, (char *)p);
@ -1448,17 +1413,13 @@ retry:
// Detected a UTF-8 error.
rewind_retry:
// Retry reading with another conversion.
#ifdef HAVE_ICONV
if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) {
// iconv() failed, try 'charconvert'
did_iconv = true;
} else {
#endif
// use next item from 'fileencodings'
advance_fenc = true;
#ifdef HAVE_ICONV
}
#endif
// use next item from 'fileencodings'
advance_fenc = true;
}
file_rewind = true;
goto retry;
}
@ -1683,11 +1644,9 @@ failed:
if (fenc_alloced) {
xfree(fenc);
}
#ifdef HAVE_ICONV
if (iconv_fd != (iconv_t)-1) {
iconv_close(iconv_fd);
}
#endif
if (!read_buffer && !read_stdin) {
close(fd); // errors are ignored
@ -2233,9 +2192,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
write_info.bw_conv_error = false;
write_info.bw_conv_error_lnum = 0;
write_info.bw_restlen = 0;
#ifdef HAVE_ICONV
write_info.bw_iconv_fd = (iconv_t)-1;
#endif
// After writing a file changedtick changes but we don't want to display
// the line.
@ -2996,7 +2953,6 @@ nobackup:
}
if (converted && wb_flags == 0) {
#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, "utf-8");
@ -3009,28 +2965,21 @@ nobackup:
}
write_info.bw_first = true;
} else {
#endif
// When the file needs to be converted with 'charconvert' after
// writing, write to a temp file instead and let the conversion
// overwrite the original file.
if (*p_ccv != NUL) {
wfname = vim_tempname();
if (wfname == NULL) { // Can't write without a tempfile!
SET_ERRMSG(_("E214: Can't find temp file for writing"));
goto restore_backup;
// When the file needs to be converted with 'charconvert' after
// writing, write to a temp file instead and let the conversion
// overwrite the original file.
if (*p_ccv != NUL) {
wfname = vim_tempname();
if (wfname == NULL) { // Can't write without a tempfile!
SET_ERRMSG(_("E214: Can't find temp file for writing"));
goto restore_backup;
}
}
}
}
#ifdef HAVE_ICONV
}
#endif
if (converted && wb_flags == 0
#ifdef HAVE_ICONV
&& write_info.bw_iconv_fd == (iconv_t)-1
#endif
&& wfname == fname) {
if (!forceit) {
SET_ERRMSG(_("E213: Cannot convert (add ! to write without conversion)"));
@ -3551,12 +3500,10 @@ nofail:
}
xfree(fenc_tofree);
xfree(write_info.bw_conv_buf);
#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
#ifdef HAVE_ACL
os_free_acl(acl);
#endif
@ -3801,7 +3748,7 @@ static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) F
|| file_info->stat.st_mtim.tv_sec - mtime > 1
|| mtime - file_info->stat.st_mtim.tv_sec > 1;
#else
|| (long)file_info->stat.st_mtim.tv_sec != mtime;
|| file_info->stat.st_mtim.tv_sec != mtime;
#endif
}
@ -3914,7 +3861,6 @@ static int buf_write_bytes(struct bw_info *ip)
}
}
#ifdef HAVE_ICONV
if (ip->bw_iconv_fd != (iconv_t)-1) {
const char *from;
size_t fromlen;
@ -3973,7 +3919,6 @@ static int buf_write_bytes(struct bw_info *ip)
buf = ip->bw_conv_buf;
len = (int)(to - ip->bw_conv_buf);
}
#endif
}
if (ip->bw_fd < 0) {

View File

@ -1,20 +1,18 @@
#ifndef NVIM_ICONV_H
#define NVIM_ICONV_H
#include <errno.h>
#include <iconv.h>
#include "auto/config.h"
#ifdef HAVE_ICONV
# include <errno.h>
# include <iconv.h>
// define some missing constants if necessary
# ifndef EILSEQ
# define EILSEQ 123
# endif
# define ICONV_ERRNO errno
# define ICONV_E2BIG E2BIG
# define ICONV_EINVAL EINVAL
# define ICONV_EILSEQ EILSEQ
#ifndef EILSEQ
# define EILSEQ 123
#endif
#define ICONV_ERRNO errno
#define ICONV_E2BIG E2BIG
#define ICONV_EINVAL EINVAL
#define ICONV_EILSEQ EILSEQ
#endif // NVIM_ICONV_H

View File

@ -482,8 +482,6 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
return 1;
}
#if defined(HAVE_ICONV)
/// Convert string from one encoding to another
static int nlua_iconv(lua_State *lstate)
{
@ -526,8 +524,6 @@ static int nlua_iconv(lua_State *lstate)
return 1;
}
#endif
void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
{
if (!is_thread) {
@ -574,12 +570,10 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
luaopen_spell(lstate);
lua_setfield(lstate, -2, "spell");
#if defined(HAVE_ICONV)
// vim.iconv
// depends on p_ambw, p_emoji
lua_pushcfunction(lstate, &nlua_iconv);
lua_setfield(lstate, -2, "iconv");
#endif
}
// vim.mpack

View File

@ -2263,8 +2263,6 @@ enc_locale_copy_enc:
return enc_canonize(buf);
}
#if defined(HAVE_ICONV)
// Call iconv_open() with a check if iconv() works properly (there are broken
// versions).
// Returns (void *)-1 if failed.
@ -2272,7 +2270,7 @@ enc_locale_copy_enc:
void *my_iconv_open(char *to, char *from)
{
iconv_t fd;
# define ICONV_TESTLEN 400
#define ICONV_TESTLEN 400
char tobuf[ICONV_TESTLEN];
char *p;
size_t tolen;
@ -2386,8 +2384,6 @@ static char *iconv_string(const vimconv_T *const vcp, const char *str, size_t sl
return result;
}
#endif // HAVE_ICONV
/// Setup "vcp" for conversion from "from" to "to".
/// The names must have been made canonical with enc_canonize().
/// vcp->vc_type must have been initialized to CONV_NONE.
@ -2412,11 +2408,9 @@ int convert_setup_ext(vimconv_T *vcp, char *from, bool from_unicode_is_utf8, cha
int to_is_utf8;
// Reset to no conversion.
#ifdef HAVE_ICONV
if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1) {
iconv_close(vcp->vc_fd);
}
#endif
*vcp = (vimconv_T)MBYTE_NONE_CONV;
// No conversion when one of the names is empty or they are equal.
@ -2452,9 +2446,7 @@ int convert_setup_ext(vimconv_T *vcp, char *from, bool from_unicode_is_utf8, cha
} else if (from_is_utf8 && (to_prop & ENC_LATIN9)) {
// Internal utf-8 -> latin9 conversion.
vcp->vc_type = CONV_TO_LATIN9;
}
#ifdef HAVE_ICONV
else { // NOLINT(readability/braces)
} else {
// Use iconv() for conversion.
vcp->vc_fd = (iconv_t)my_iconv_open(to_is_utf8 ? "utf-8" : to,
from_is_utf8 ? "utf-8" : from);
@ -2463,7 +2455,6 @@ int convert_setup_ext(vimconv_T *vcp, char *from, bool from_unicode_is_utf8, cha
vcp->vc_factor = 4; // could be longer too...
}
}
#endif
if (vcp->vc_type == CONV_NONE) {
return FAIL;
}
@ -2626,11 +2617,9 @@ char *string_convert_ext(const vimconv_T *const vcp, char *ptr, size_t *lenp, si
}
break;
#ifdef HAVE_ICONV
case CONV_ICONV: // conversion with vcp->vc_fd
retval = (char_u *)iconv_string(vcp, ptr, len, unconvlenp, lenp);
break;
#endif
}
return (char *)retval;

View File

@ -46,9 +46,7 @@ typedef enum {
typedef struct {
int vc_type; ///< Zero or more ConvFlags.
int vc_factor; ///< Maximal expansion factor.
#ifdef HAVE_ICONV
iconv_t vc_fd; ///< Value for CONV_ICONV.
#endif
bool vc_fail; ///< What to do with invalid characters: if true, fail,
///< otherwise use '?'.
} vimconv_T;

View File

@ -63,12 +63,6 @@ static char *features[] = {
"-acl",
#endif
#if defined(HAVE_ICONV)
"+iconv",
#else
"-iconv",
#endif
"+tui",
NULL
};