/* * ex_getline -- * Return a line from the file. * * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *)); */ int ex_getline(SCR *sp, FILE *fp, size_t *lenp) { EX_PRIVATE *exp; size_t off; int ch; char *p; exp = EXP(sp); for (errno = 0, off = 0, p = exp->ibp;;) { if (off >= exp->ibp_len) { BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1); p = exp->ibp + off; } if ((ch = getc(fp)) == EOF && !feof(fp)) { if (errno == EINTR) { errno = 0; clearerr(fp); continue; } return (1); } if (ch == EOF || ch == '\n') { if (ch == EOF && !off) return (1); *lenp = off; return (0); } *p++ = ch; ++off; } /* NOTREACHED */ }
static int int2raw(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, size_t *tolen, const char **dst) { int i; char **tostr = (char **)(void *)&cw->bp1; size_t *blen = &cw->blen1; BINC_RETC(NULL, *tostr, *blen, len); *tolen = len; for (i = 0; i < len; ++i) (*tostr)[i] = str[i]; *dst = cw->bp1; return 0; }
static int default_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, size_t *tolen, const char **pdst, const char *enc) { size_t i, j; int offset = 0; char **tostr = (char **)(void *)&cw->bp1; size_t *blen = &cw->blen1; mbstate_t mbs; size_t n; ssize_t nlen = len + MB_CUR_MAX; char *dst; size_t buflen; char buffer[CONV_BUFFER_SIZE]; iconv_t id = (iconv_t)-1; /* convert first len bytes of buffer and append it to cw->bp * len is adjusted => 0 * offset contains the offset in cw->bp and is adjusted * cw->bp is grown as required */ #ifdef USE_ICONV #define CONVERT2(len, cw, offset) \ do { \ const char *bp = buffer; \ while (len != 0) { \ size_t outleft = cw->blen1 - offset; \ char *obp = (char *)cw->bp1 + offset; \ if (cw->blen1 < offset + MB_CUR_MAX) { \ nlen += 256; \ BINC_RETC(NULL, cw->bp1, cw->blen1, nlen); \ } \ errno = 0; \ if (iconv(id, &bp, &len, &obp, &outleft) == (size_t)-1 && \ errno != E2BIG) \ HANDLE_ICONV_ERROR(obp, bp, outleft, len); \ offset = cw->blen1 - outleft; \ } \ } while (0) #else #define CONVERT2(len, cw, offset) #endif MEMSET(&mbs, 0, 1); BINC_RETC(NULL, *tostr, *blen, nlen); dst = *tostr; buflen = *blen; #ifdef USE_ICONV if (strcmp(nl_langinfo(CODESET), enc)) { id = iconv_open(enc, nl_langinfo(CODESET)); if (id == (iconv_t)-1) goto err; dst = buffer; buflen = CONV_BUFFER_SIZE; } #endif for (i = 0, j = 0; i < (size_t)len; ++i) { n = wcrtomb(dst+j, str[i], &mbs); if (n == (size_t)-1) HANDLE_MBR_ERROR(n, mbs, dst[j], str[i]); j += n; if (buflen < j + MB_CUR_MAX) { if (id != (iconv_t)-1) { CONVERT2(j, cw, offset); } else { nlen += 256; BINC_RETC(NULL, *tostr, *blen, nlen); dst = *tostr; buflen = *blen; } } } n = wcrtomb(dst+j, L'\0', &mbs); j += n - 1; /* don't count NUL at the end */ *tolen = j; if (id != (iconv_t)-1) { CONVERT2(j, cw, offset); *tolen = offset; } *pdst = cw->bp1; return 0; err: *tolen = j; *pdst = cw->bp1; return 1; }