Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 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 | /* |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 11 | * os_macosx.c -- Mac specific things for Mac OS/X. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifdef MACOS_X_UNIX |
| 15 | # ifdef HAVE_CONFIG_H /* Using Makefile. */ |
| 16 | # include "vim.h" |
| 17 | # else |
| 18 | # include "os_unix.c" /* Using Project Builder */ |
| 19 | # endif |
| 20 | #else |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 21 | Error: MACOS 9 is no longer supported in Vim 7 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | #ifdef _DEBUG |
| 25 | void |
| 26 | Trace(char* fmt, ...) |
| 27 | { |
| 28 | char buf[2048]; |
| 29 | va_list args; |
| 30 | |
| 31 | va_start(args, fmt); |
| 32 | /* vsnprintf(buf, sizeof(buf), fmt, args);*/ |
| 33 | fprintf(stderr, "%s", buf); |
| 34 | va_end(args); |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | #ifdef MACOS_X_ICONVEMU |
| 39 | /* |
| 40 | * Libiconv emulation layer |
| 41 | */ |
| 42 | |
| 43 | struct _iconv_t |
| 44 | { |
| 45 | TECObjectRef tec; |
| 46 | TECObjectRef tecReverse; |
| 47 | TECSnifferObjectRef sniff; |
| 48 | TextEncoding from; |
| 49 | TextEncoding to; |
| 50 | }; |
| 51 | /* typedef struct _iconv_t *iconv_t; */ |
| 52 | |
| 53 | |
| 54 | static int last_errno = 0; |
| 55 | |
| 56 | /* |
| 57 | * Get TextEncoding from iconv's encoding name |
| 58 | */ |
| 59 | static TextEncoding |
| 60 | get_textencoding(const char* encodename) |
| 61 | { |
| 62 | static struct { |
| 63 | const char* name; |
| 64 | TextEncoding encode; |
| 65 | } encodetable[] = { |
| 66 | /* ISO-8859 encodings family */ |
| 67 | {"latin1", kTextEncodingISOLatin1}, |
| 68 | {"latin2", kTextEncodingISOLatin2}, |
| 69 | {"latin3", kTextEncodingISOLatin3}, |
| 70 | {"latin4", kTextEncodingISOLatin4}, |
| 71 | {"latin5", kTextEncodingISOLatin5}, |
| 72 | {"latin6", kTextEncodingISOLatin6}, |
| 73 | {"latin7", kTextEncodingISOLatin7}, |
| 74 | {"latin8", kTextEncodingISOLatin8}, |
| 75 | {"latin9", kTextEncodingISOLatin9}, |
| 76 | {"iso-8859-1", kTextEncodingISOLatin1}, |
| 77 | {"iso-8859-2", kTextEncodingISOLatin2}, |
| 78 | {"iso-8859-3", kTextEncodingISOLatin3}, |
| 79 | {"iso-8859-4", kTextEncodingISOLatin4}, |
| 80 | {"iso-8859-5", kTextEncodingISOLatinCyrillic}, |
| 81 | {"iso-8859-6", kTextEncodingISOLatinArabic}, |
| 82 | {"iso-8859-7", kTextEncodingISOLatinGreek}, |
| 83 | {"iso-8859-8", kTextEncodingISOLatinHebrew}, |
| 84 | {"iso-8859-9", kTextEncodingISOLatin5}, |
| 85 | {"iso-8859-10", kTextEncodingISOLatin6}, |
Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 86 | {"iso-8859-15", kTextEncodingISOLatin9}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | |
| 88 | /* Unicode encodings. */ |
| 89 | /* TODO: Add other type of unicode */ |
| 90 | {"ucs-2", kTextEncodingMacUnicode}, |
| 91 | |
| 92 | /* Japanese encoding aliases */ |
| 93 | {"cp932", kTextEncodingShiftJIS}, |
| 94 | {"shift-jis", kTextEncodingShiftJIS}, |
| 95 | {"euc-jp", kTextEncodingEUC_JP}, |
| 96 | {"iso-2022-jp", kTextEncodingISO_2022_JP}, |
| 97 | {"iso-2022-jp-1", kTextEncodingISO_2022_JP_1}, |
| 98 | {"iso-2022-jp-2", kTextEncodingISO_2022_JP_2}, |
| 99 | {"iso-2022-jp-3", kTextEncodingISO_2022_JP_3}, |
| 100 | |
| 101 | /* Other aliases. These aliases in this block are just guessed. */ |
| 102 | /* TODO: Must be verified. */ |
| 103 | {"gb2312", kTextEncodingGB_2312_80}, |
| 104 | {"cp936", kTextEncodingMacChineseSimp}, |
| 105 | {"euc-cn", kTextEncodingEUC_CN}, |
| 106 | {"cp950", kTextEncodingMacChineseTrad}, |
| 107 | {"euc-tw", kTextEncodingEUC_TW}, |
| 108 | {"cp949", kTextEncodingMacKorean}, |
| 109 | {"euc-kr", kTextEncodingEUC_KR}, |
| 110 | |
| 111 | /* |
| 112 | * All encodings supported by Macintosh. You can find these values |
| 113 | * in a file: |
| 114 | * /System/Library/Frameworks/CoreServices.framework/Versions/A/ |
| 115 | * Frameworks/CarbonCore.framework/Versions/A/Headers/TextCommon.h |
| 116 | */ |
| 117 | {"MacRoman", kTextEncodingMacRoman}, |
| 118 | {"MacJapanese", kTextEncodingMacJapanese}, |
| 119 | {"MacChineseTrad", kTextEncodingMacChineseTrad}, |
| 120 | {"MacKorean", kTextEncodingMacKorean}, |
| 121 | {"MacArabic", kTextEncodingMacArabic}, |
| 122 | {"MacHebrew", kTextEncodingMacHebrew}, |
| 123 | {"MacGreek", kTextEncodingMacGreek}, |
| 124 | {"MacCyrillic", kTextEncodingMacCyrillic}, |
| 125 | {"MacDevanagari", kTextEncodingMacDevanagari}, |
| 126 | {"MacGurmukhi", kTextEncodingMacGurmukhi}, |
| 127 | {"MacGujarati", kTextEncodingMacGujarati}, |
| 128 | {"MacOriya", kTextEncodingMacOriya}, |
| 129 | {"MacBengali", kTextEncodingMacBengali}, |
| 130 | {"MacTamil", kTextEncodingMacTamil}, |
| 131 | {"MacTelugu", kTextEncodingMacTelugu}, |
| 132 | {"MacKannada", kTextEncodingMacKannada}, |
| 133 | {"MacMalayalam", kTextEncodingMacMalayalam}, |
| 134 | {"MacSinhalese", kTextEncodingMacSinhalese}, |
| 135 | {"MacBurmese", kTextEncodingMacBurmese}, |
| 136 | {"MacKhmer", kTextEncodingMacKhmer}, |
| 137 | {"MacThai", kTextEncodingMacThai}, |
| 138 | {"MacLaotian", kTextEncodingMacLaotian}, |
| 139 | {"MacGeorgian", kTextEncodingMacGeorgian}, |
| 140 | {"MacArmenian", kTextEncodingMacArmenian}, |
| 141 | {"MacChineseSimp", kTextEncodingMacChineseSimp}, |
| 142 | {"MacTibetan", kTextEncodingMacTibetan}, |
| 143 | {"MacMongolian", kTextEncodingMacMongolian}, |
| 144 | {"MacEthiopic", kTextEncodingMacEthiopic}, |
| 145 | {"MacCentralEurRoman", kTextEncodingMacCentralEurRoman}, |
| 146 | {"MacVietnamese", kTextEncodingMacVietnamese}, |
| 147 | {"MacExtArabic", kTextEncodingMacExtArabic}, |
| 148 | {"MacSymbol", kTextEncodingMacSymbol}, |
| 149 | {"MacDingbats", kTextEncodingMacDingbats}, |
| 150 | {"MacTurkish", kTextEncodingMacTurkish}, |
| 151 | {"MacCroatian", kTextEncodingMacCroatian}, |
| 152 | {"MacIcelandic", kTextEncodingMacIcelandic}, |
| 153 | {"MacRomanian", kTextEncodingMacRomanian}, |
| 154 | {"MacCeltic", kTextEncodingMacCeltic}, |
| 155 | {"MacGaelic", kTextEncodingMacGaelic}, |
| 156 | {"MacKeyboardGlyphs", kTextEncodingMacKeyboardGlyphs}, |
| 157 | {"MacTradChinese", kTextEncodingMacTradChinese}, |
| 158 | {"MacRSymbol", kTextEncodingMacRSymbol}, |
| 159 | {"MacSimpChinese", kTextEncodingMacSimpChinese}, |
| 160 | {"MacGeez", kTextEncodingMacGeez}, |
| 161 | {"MacEastEurRoman", kTextEncodingMacEastEurRoman}, |
| 162 | {"MacUninterp", kTextEncodingMacUninterp}, |
| 163 | {"MacUnicode", kTextEncodingMacUnicode}, |
| 164 | {"MacFarsi", kTextEncodingMacFarsi}, |
| 165 | {"MacUkrainian", kTextEncodingMacUkrainian}, |
| 166 | {"MacInuit", kTextEncodingMacInuit}, |
| 167 | {"MacVT100", kTextEncodingMacVT100}, |
| 168 | {"MacHFS", kTextEncodingMacHFS}, |
| 169 | {"UnicodeDefault", kTextEncodingUnicodeDefault}, |
| 170 | {"UnicodeV1_1", kTextEncodingUnicodeV1_1}, |
| 171 | {"ISO10646_1993", kTextEncodingISO10646_1993}, |
| 172 | {"UnicodeV2_0", kTextEncodingUnicodeV2_0}, |
| 173 | {"UnicodeV2_1", kTextEncodingUnicodeV2_1}, |
| 174 | {"UnicodeV3_0", kTextEncodingUnicodeV3_0}, |
| 175 | {"UnicodeV3_1", kTextEncodingUnicodeV3_1}, |
| 176 | {"UnicodeV3_2", kTextEncodingUnicodeV3_2}, |
| 177 | {"ISOLatin1", kTextEncodingISOLatin1}, |
| 178 | {"ISOLatin2", kTextEncodingISOLatin2}, |
| 179 | {"ISOLatin3", kTextEncodingISOLatin3}, |
| 180 | {"ISOLatin4", kTextEncodingISOLatin4}, |
| 181 | {"ISOLatinCyrillic", kTextEncodingISOLatinCyrillic}, |
| 182 | {"ISOLatinArabic", kTextEncodingISOLatinArabic}, |
| 183 | {"ISOLatinGreek", kTextEncodingISOLatinGreek}, |
| 184 | {"ISOLatinHebrew", kTextEncodingISOLatinHebrew}, |
| 185 | {"ISOLatin5", kTextEncodingISOLatin5}, |
| 186 | {"ISOLatin6", kTextEncodingISOLatin6}, |
| 187 | {"ISOLatin7", kTextEncodingISOLatin7}, |
| 188 | {"ISOLatin8", kTextEncodingISOLatin8}, |
| 189 | {"ISOLatin9", kTextEncodingISOLatin9}, |
| 190 | {"DOSLatinUS", kTextEncodingDOSLatinUS}, |
| 191 | {"DOSGreek", kTextEncodingDOSGreek}, |
| 192 | {"DOSBalticRim", kTextEncodingDOSBalticRim}, |
| 193 | {"DOSLatin1", kTextEncodingDOSLatin1}, |
| 194 | {"DOSGreek1", kTextEncodingDOSGreek1}, |
| 195 | {"DOSLatin2", kTextEncodingDOSLatin2}, |
| 196 | {"DOSCyrillic", kTextEncodingDOSCyrillic}, |
| 197 | {"DOSTurkish", kTextEncodingDOSTurkish}, |
| 198 | {"DOSPortuguese", kTextEncodingDOSPortuguese}, |
| 199 | {"DOSIcelandic", kTextEncodingDOSIcelandic}, |
| 200 | {"DOSHebrew", kTextEncodingDOSHebrew}, |
| 201 | {"DOSCanadianFrench", kTextEncodingDOSCanadianFrench}, |
| 202 | {"DOSArabic", kTextEncodingDOSArabic}, |
| 203 | {"DOSNordic", kTextEncodingDOSNordic}, |
| 204 | {"DOSRussian", kTextEncodingDOSRussian}, |
| 205 | {"DOSGreek2", kTextEncodingDOSGreek2}, |
| 206 | {"DOSThai", kTextEncodingDOSThai}, |
| 207 | {"DOSJapanese", kTextEncodingDOSJapanese}, |
| 208 | {"DOSChineseSimplif", kTextEncodingDOSChineseSimplif}, |
| 209 | {"DOSKorean", kTextEncodingDOSKorean}, |
| 210 | {"DOSChineseTrad", kTextEncodingDOSChineseTrad}, |
| 211 | {"WindowsLatin1", kTextEncodingWindowsLatin1}, |
| 212 | {"WindowsANSI", kTextEncodingWindowsANSI}, |
| 213 | {"WindowsLatin2", kTextEncodingWindowsLatin2}, |
| 214 | {"WindowsCyrillic", kTextEncodingWindowsCyrillic}, |
| 215 | {"WindowsGreek", kTextEncodingWindowsGreek}, |
| 216 | {"WindowsLatin5", kTextEncodingWindowsLatin5}, |
| 217 | {"WindowsHebrew", kTextEncodingWindowsHebrew}, |
| 218 | {"WindowsArabic", kTextEncodingWindowsArabic}, |
| 219 | {"WindowsBalticRim", kTextEncodingWindowsBalticRim}, |
| 220 | {"WindowsVietnamese", kTextEncodingWindowsVietnamese}, |
| 221 | {"WindowsKoreanJohab", kTextEncodingWindowsKoreanJohab}, |
| 222 | {"US_ASCII", kTextEncodingUS_ASCII}, |
| 223 | {"JIS_X0201_76", kTextEncodingJIS_X0201_76}, |
| 224 | {"JIS_X0208_83", kTextEncodingJIS_X0208_83}, |
| 225 | {"JIS_X0208_90", kTextEncodingJIS_X0208_90}, |
| 226 | {"JIS_X0212_90", kTextEncodingJIS_X0212_90}, |
| 227 | {"JIS_C6226_78", kTextEncodingJIS_C6226_78}, |
| 228 | {"ShiftJIS_X0213_00", kTextEncodingShiftJIS_X0213_00}, |
| 229 | {"GB_2312_80", kTextEncodingGB_2312_80}, |
| 230 | {"GBK_95", kTextEncodingGBK_95}, |
| 231 | {"GB_18030_2000", kTextEncodingGB_18030_2000}, |
| 232 | {"KSC_5601_87", kTextEncodingKSC_5601_87}, |
| 233 | {"KSC_5601_92_Johab", kTextEncodingKSC_5601_92_Johab}, |
| 234 | {"CNS_11643_92_P1", kTextEncodingCNS_11643_92_P1}, |
| 235 | {"CNS_11643_92_P2", kTextEncodingCNS_11643_92_P2}, |
| 236 | {"CNS_11643_92_P3", kTextEncodingCNS_11643_92_P3}, |
| 237 | {"ISO_2022_JP", kTextEncodingISO_2022_JP}, |
| 238 | {"ISO_2022_JP_2", kTextEncodingISO_2022_JP_2}, |
| 239 | {"ISO_2022_JP_1", kTextEncodingISO_2022_JP_1}, |
| 240 | {"ISO_2022_JP_3", kTextEncodingISO_2022_JP_3}, |
| 241 | {"ISO_2022_CN", kTextEncodingISO_2022_CN}, |
| 242 | {"ISO_2022_CN_EXT", kTextEncodingISO_2022_CN_EXT}, |
| 243 | {"ISO_2022_KR", kTextEncodingISO_2022_KR}, |
| 244 | {"EUC_JP", kTextEncodingEUC_JP}, |
| 245 | {"EUC_CN", kTextEncodingEUC_CN}, |
| 246 | {"EUC_TW", kTextEncodingEUC_TW}, |
| 247 | {"EUC_KR", kTextEncodingEUC_KR}, |
| 248 | {"ShiftJIS", kTextEncodingShiftJIS}, |
| 249 | {"KOI8_R", kTextEncodingKOI8_R}, |
| 250 | {"Big5", kTextEncodingBig5}, |
| 251 | {"MacRomanLatin1", kTextEncodingMacRomanLatin1}, |
| 252 | {"HZ_GB_2312", kTextEncodingHZ_GB_2312}, |
| 253 | {"Big5_HKSCS_1999", kTextEncodingBig5_HKSCS_1999}, |
| 254 | {"NextStepLatin", kTextEncodingNextStepLatin}, |
| 255 | {"EBCDIC_US", kTextEncodingEBCDIC_US}, |
| 256 | {"EBCDIC_CP037", kTextEncodingEBCDIC_CP037}, |
| 257 | {"MultiRun", kTextEncodingMultiRun}, |
| 258 | |
| 259 | /* Terminator */ |
| 260 | {NULL, -1}, |
| 261 | }; |
| 262 | int i; |
| 263 | |
| 264 | i = 0; |
| 265 | for (i = 0; encodetable[i].name != NULL; ++i) |
| 266 | { |
| 267 | if (STRICMP(encodename, encodetable[i].name) == 0) |
| 268 | break; |
| 269 | } |
| 270 | return encodetable[i].encode; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * iconv interfaces |
| 275 | */ |
| 276 | |
| 277 | iconv_t |
| 278 | iconv_open(const char* tocode, const char* fromcode) |
| 279 | { |
| 280 | TextEncoding toEnc, fromEnc; |
| 281 | iconv_t cd = NULL; |
| 282 | OSStatus st; |
| 283 | |
| 284 | /* Verify to/from encoding name */ |
| 285 | toEnc = get_textencoding(tocode); |
| 286 | fromEnc = get_textencoding(fromcode); |
| 287 | if (toEnc < 0 || fromEnc < 0) |
| 288 | goto ICONV_OPEN_ERR; |
| 289 | |
| 290 | /* Allocate memory to object */ |
| 291 | cd = (iconv_t)alloc(sizeof(struct _iconv_t)); |
| 292 | if (!cd) |
| 293 | goto ICONV_OPEN_ERR; |
| 294 | memset(cd, 0, sizeof(struct _iconv_t)); |
| 295 | |
| 296 | /* Create converter */ |
| 297 | if (fromEnc != toEnc) |
| 298 | { |
| 299 | TRACE("*** fromEnc=%d toEnc=%d\n", (int)fromEnc, (int)toEnc); |
| 300 | st = TECCreateConverter(&cd->tec, fromEnc, toEnc); |
| 301 | if (st != 0) |
| 302 | { |
| 303 | TRACE("*** TECCreateConverter()=%d\n", (int)st); |
| 304 | goto ICONV_OPEN_ERR; |
| 305 | } |
| 306 | /* Create reverse converter */ |
| 307 | st = TECCreateConverter(&cd->tecReverse, toEnc, fromEnc); |
| 308 | if (st != 0) |
| 309 | { |
| 310 | TRACE("*** TECCreateConverter()=%d (reverse)\n", (int)st); |
| 311 | goto ICONV_OPEN_ERR; |
| 312 | } |
| 313 | /* Create Sniffer */ |
| 314 | st = TECCreateSniffer(&cd->sniff, &fromEnc, 1); |
| 315 | if (st != 0) |
| 316 | { |
| 317 | TRACE("*** TECCreateSniffer()=%d\n", (int)st); |
| 318 | goto ICONV_OPEN_ERR; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | cd->from = fromEnc; |
| 323 | cd->to = toEnc; |
| 324 | last_errno = 0; |
| 325 | return cd; |
| 326 | |
| 327 | ICONV_OPEN_ERR: |
| 328 | if (cd) |
| 329 | iconv_close(cd); |
| 330 | last_errno = EINVAL; |
| 331 | return (iconv_t)-1; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Used when there are same value in 'from encoding' and 'to encoding'. |
| 336 | * TEC doesn't support conversion between same encodings, and |
| 337 | * TECCreateConverter() failed. |
| 338 | */ |
| 339 | static size_t |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 340 | null_conv(iconv_t cd, const char **inbuf, size_t *inbytesleft, |
| 341 | char **outbuf, size_t *outbytesleft) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 342 | { |
| 343 | const char* buf_in = inbuf && *inbuf ? *inbuf : NULL; |
| 344 | char* buf_out = outbuf && *outbuf ? *outbuf : NULL; |
| 345 | |
| 346 | if (buf_in) |
| 347 | { |
| 348 | int in_len = inbytesleft ? *inbytesleft : 0; |
| 349 | int out_len = outbytesleft ? *outbytesleft : 0; |
| 350 | |
| 351 | if (!buf_out || out_len <= 0) |
| 352 | { |
| 353 | last_errno = E2BIG; |
| 354 | return -1; |
| 355 | } |
| 356 | else if (in_len > 0) |
| 357 | { |
| 358 | int len = in_len < out_len ? in_len : out_len; |
| 359 | |
| 360 | memcpy (buf_out, buf_in, len); |
| 361 | *inbuf += len; |
| 362 | *outbuf += len; |
| 363 | *inbytesleft -= len; |
| 364 | *outbytesleft -= len; |
| 365 | if (*outbytesleft <= 0) |
| 366 | { |
| 367 | last_errno = E2BIG; |
| 368 | return -1; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | last_errno = 0; |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | size_t |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 377 | iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, |
| 378 | char **outbuf, size_t *outbytesleft) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | { |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 380 | ConstTextPtr buf_in; |
| 381 | TextPtr buf_out; |
| 382 | ByteCount out_len, out_true; |
| 383 | ByteCount in_len, in_true; |
| 384 | OSStatus st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | |
| 386 | if (!cd) |
| 387 | { |
| 388 | last_errno = ENOENT; /* TODO: Another error code should be set */ |
| 389 | return -1; |
| 390 | } |
| 391 | if (cd->from == cd->to) |
| 392 | return null_conv(cd, inbuf, inbytesleft, outbuf, outbytesleft) ; |
| 393 | |
| 394 | buf_in = (TextPtr) inbuf ; |
| 395 | buf_out = (TextPtr) outbuf ; |
| 396 | out_len = out_true = -1; |
| 397 | in_len = in_true = -1; |
| 398 | |
| 399 | if (buf_in && buf_out) |
| 400 | { |
| 401 | ItemCount error, feature; |
| 402 | |
| 403 | /* Normal convert mode */ |
| 404 | if (!inbytesleft || !outbytesleft) |
| 405 | { |
| 406 | last_errno = EFAULT; |
| 407 | return -1; |
| 408 | } |
| 409 | in_len = *inbytesleft; |
| 410 | out_len = *outbytesleft; |
| 411 | |
| 412 | /* Check stream is form in expected encoding or not */ |
| 413 | st = TECSniffTextEncoding(cd->sniff, (TextPtr)buf_in, in_len, |
| 414 | &cd->from, 1, &error, 1, &feature, 1); |
| 415 | TRACE("TECSniffTextEncoding()=%d error=%d feature=%d\n", |
| 416 | (int)st, (int)error, (int)feature); |
| 417 | if ((error != 0 || feature == 0) |
| 418 | && !(error == 0xffffffff && feature == 0xffffffff)) |
| 419 | /* Not expected encoding */ |
| 420 | st = kTECUnmappableElementErr; |
| 421 | else |
| 422 | { |
| 423 | /* Do convert */ |
| 424 | st = TECConvertText(cd->tec, |
| 425 | buf_in, in_len, &in_true, |
| 426 | buf_out, out_len, &out_true); |
| 427 | /* Verify converted text. Compare original text with reverse |
| 428 | * converted text. If not match, there is some problem on |
| 429 | * converting. */ |
| 430 | if (st == 0 && in_true > 0) |
| 431 | { |
| 432 | ByteCount rev_in, rev_out; |
| 433 | TextPtr buf_rev = (TextPtr)alloc(in_true); |
| 434 | |
| 435 | if (buf_rev) |
| 436 | { |
| 437 | st = TECConvertText(cd->tecReverse, |
| 438 | buf_out, out_true, &rev_in, |
| 439 | buf_rev, in_true, &rev_out); |
| 440 | if (st != 0 || rev_in != out_true || rev_out != in_true |
| 441 | || memcmp(buf_rev, buf_in, rev_out) != 0) |
| 442 | { |
| 443 | #ifdef ICONVOSX_DEBUG |
| 444 | fprintf(stderr, " reverse conversion failed.\n"); |
| 445 | #endif |
| 446 | st = kTECUnmappableElementErr; |
| 447 | } |
| 448 | vim_free(buf_rev); |
| 449 | } |
| 450 | else |
| 451 | st = kTECUnmappableElementErr; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | else if (!buf_in && buf_out) |
| 456 | { |
| 457 | /* Flush all buffered strings to buffer, and reset status */ |
| 458 | if (!outbytesleft) |
| 459 | { |
| 460 | last_errno = EFAULT; |
| 461 | return -1; |
| 462 | } |
| 463 | out_len = *outbytesleft; |
| 464 | st = TECFlushText(cd->tec, |
| 465 | buf_out, out_len, &out_true); |
| 466 | } |
| 467 | else if (!buf_in && !buf_out) |
| 468 | { |
| 469 | /* Reset cd's status and cancel buffered strings */ |
| 470 | unsigned char tmp_out[256]; |
| 471 | |
| 472 | buf_out = tmp_out; |
| 473 | out_len = sizeof(tmp_out); |
| 474 | st = TECFlushText(cd->tec, |
| 475 | buf_out, out_len, &out_true); |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | last_errno = EFAULT; |
| 480 | return -1; |
| 481 | } |
| 482 | TRACE("st=%d, buf_in=%p, in_len=%d, in_true=%d\n" |
| 483 | " buf_out=%p, out_len=%d, out_true=%d\n", (int)st, |
| 484 | buf_in, (int)in_len, (int)in_true, |
| 485 | buf_out, (int)out_len, (int)out_true); |
| 486 | |
| 487 | switch (st) |
| 488 | { |
| 489 | case 0: |
| 490 | /* No error */ |
| 491 | if (inbytesleft) |
| 492 | *inbytesleft -= in_true; |
| 493 | if (outbytesleft) |
| 494 | *outbytesleft -= out_true; |
| 495 | if (inbuf && *inbuf) |
| 496 | *inbuf += in_true; |
| 497 | if (outbuf && *outbuf) |
| 498 | *outbuf += out_true; |
| 499 | last_errno = 0; |
| 500 | return 0; /* No error */ |
| 501 | case kTECUnmappableElementErr: |
| 502 | last_errno = EILSEQ; |
| 503 | case kTECIncompleteElementErr: |
| 504 | last_errno = EINVAL; |
| 505 | case kTECOutputBufferFullStatus: |
| 506 | last_errno = E2BIG; |
| 507 | return -1; |
| 508 | default: |
| 509 | TRACE("iconv(%p, %p, %p, %p, %p) failed. (%d)\n", |
| 510 | cd, inbuf, inbytesleft, outbuf, outbytesleft, (int)st); |
| 511 | last_errno = EFAULT; |
| 512 | return -1; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | int |
| 517 | iconv_close(iconv_t cd) |
| 518 | { |
| 519 | if (cd) |
| 520 | { |
| 521 | /* Free all elements of iconv_t */ |
| 522 | if (cd->tec) |
| 523 | TECDisposeConverter(cd->tec); |
| 524 | if (cd->tecReverse) |
| 525 | TECDisposeConverter(cd->tecReverse); |
| 526 | if (cd->sniff) |
| 527 | TECDisposeSniffer(cd->sniff); |
| 528 | vim_free(cd); |
| 529 | last_errno = 0; |
| 530 | return 0; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | last_errno = EINVAL; |
| 535 | return -1; |
| 536 | } |
| 537 | } |
| 538 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 539 | int * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | iconv_errno() |
| 541 | { |
| 542 | return &last_errno; |
| 543 | } |
| 544 | #endif /* MACOS_X_ICONVEMU */ |
| 545 | |
| 546 | #ifdef USE_MCH_GETTEXT |
| 547 | |
| 548 | #define GETTEXT_BUFNUM 64 |
| 549 | #define GETTEXT_BUFSIZE 256 |
| 550 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 551 | char * |
| 552 | mch_gettext(const char *msgid) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 553 | { |
| 554 | static char buf[GETTEXT_BUFNUM][GETTEXT_BUFSIZE]; |
| 555 | static int bufnum = 0; |
| 556 | const char *msg = NULL; |
| 557 | CFStringRef strkey = NULL, strmsg = NULL; |
| 558 | CFStringEncoding enc; |
| 559 | |
| 560 | if (!msgid) |
| 561 | goto MCH_GETTEXT_FINISH; |
| 562 | enc = CFStringGetSystemEncoding(); |
| 563 | TRACE("mch_gettext(%s)\n", msgid); |
| 564 | |
| 565 | strkey = CFStringCreateWithCString(NULL, msgid, enc); |
| 566 | if (!strkey) |
| 567 | { |
| 568 | TRACE(" Can't create a CFString for msgid.\n"); |
| 569 | goto MCH_GETTEXT_FINISH; |
| 570 | } |
| 571 | |
| 572 | strmsg = CFCopyLocalizedString(strkey, NULL); |
| 573 | if (!strmsg) |
| 574 | { |
| 575 | TRACE(" No localized strings for msgid.\n"); |
| 576 | goto MCH_GETTEXT_FINISH; |
| 577 | } |
| 578 | |
| 579 | msg = CFStringGetCStringPtr(strmsg, enc); |
| 580 | if (!msg) |
| 581 | { |
| 582 | /* This is as backup when CFStringGetCStringPtr was failed */ |
| 583 | CFStringGetCString(strmsg, buf[bufnum], GETTEXT_BUFSIZE, enc); |
| 584 | msg = buf[bufnum]; |
| 585 | if (++bufnum >= GETTEXT_BUFNUM) |
| 586 | bufnum = 0; |
| 587 | } |
| 588 | TRACE(" Localized to: %s\n", msg); |
| 589 | |
| 590 | MCH_GETTEXT_FINISH: |
| 591 | if (strkey) |
| 592 | CFRelease(strkey); |
| 593 | if (strmsg) |
| 594 | CFRelease(strmsg); |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 595 | return (char *)(msg ? msg : msgid); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 598 | char * |
| 599 | mch_bindtextdomain(const char *domain, const char *dirname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 600 | { |
| 601 | TRACE("mch_bindtextdomain(%s, %s)\n", domain, dirname); |
| 602 | return (char*)dirname; |
| 603 | } |
| 604 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 605 | char * |
| 606 | mch_textdomain(const char *domain) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 607 | { |
| 608 | TRACE("mch_textdomain(%s)\n", domain); |
| 609 | return (char*)domain; |
| 610 | } |
| 611 | #endif |