#include <mksh/misc.h>
#include <mksh/read.h>
#include <sys/uio.h>
#include <unistd.h>
#include <libintl.h>
#define STRING_LEN_TO_CONVERT (8*1024)
Boolean make_state_locked;
Source
get_next_block_fn(Source source)
{
off_t to_read;
int length;
size_t num_wc_chars;
char ch_save;
char *ptr;
if (source == NULL) {
return NULL;
}
if ((source->fd < 0) ||
((source->bytes_left_in_file <= 0) &&
(source->inp_buf_ptr >= source->inp_buf_end))) {
if (source->fd > 2) {
(void) close(source->fd);
if (make_state_lockfile != NULL) {
(void) unlink(make_state_lockfile);
retmem_mb(make_state_lockfile);
make_state_lockfile = NULL;
make_state_locked = false;
}
}
if (source->string.free_after_use &&
(source->string.buffer.start != NULL)) {
retmem(source->string.buffer.start);
source->string.buffer.start = NULL;
}
if (source->inp_buf != NULL) {
retmem_mb(source->inp_buf);
source->inp_buf = NULL;
}
source = source->previous;
if (source != NULL) {
source->error_converting = false;
}
return source;
}
if (source->bytes_left_in_file > 0) {
to_read = source->bytes_left_in_file;
source->inp_buf_ptr = source->inp_buf = getmem(to_read + 1);
source->inp_buf_end = source->inp_buf + to_read;
length = read(source->fd, source->inp_buf, (unsigned int) to_read);
if (length != to_read) {
WCSTOMBS(mbs_buffer, file_being_read);
if (length == 0) {
fatal_mksh(gettext("Error reading `%s': Premature EOF"),
mbs_buffer);
} else {
fatal_mksh(gettext("Error reading `%s': %s"),
mbs_buffer,
errmsg(errno));
}
}
*source->inp_buf_end = nul_char;
source->bytes_left_in_file = 0;
}
ptr = source->inp_buf_ptr + STRING_LEN_TO_CONVERT;
if (ptr > source->inp_buf_end) {
ptr = source->inp_buf_end;
}
for (num_wc_chars = 0; ptr > source->inp_buf_ptr; ptr--) {
ch_save = *ptr;
*ptr = nul_char;
num_wc_chars = mbstowcs(source->string.text.end,
source->inp_buf_ptr,
STRING_LEN_TO_CONVERT);
*ptr = ch_save;
if (num_wc_chars != (size_t)-1) {
break;
}
}
if ((int) num_wc_chars == (size_t)-1) {
source->error_converting = true;
return source;
}
source->error_converting = false;
source->inp_buf_ptr = ptr;
source->string.text.end += num_wc_chars;
*source->string.text.end = 0;
if (source->inp_buf_ptr >= source->inp_buf_end) {
if (*(source->string.text.end - 1) != (int) newline_char) {
WCSTOMBS(mbs_buffer, file_being_read);
warning_mksh(gettext("newline is not last character in file %s"),
mbs_buffer);
*source->string.text.end++ = (int) newline_char;
*source->string.text.end = (int) nul_char;
*source->string.buffer.end++;
}
if (source->inp_buf != NULL) {
retmem_mb(source->inp_buf);
source->inp_buf = NULL;
}
}
return source;
}