//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
size_t line_start = 0;
for (size_t i = 0; i < nbytes; i++) {
if (buffer[i] == '\n') {
line_callback(index, line);
line.clear();
accum.clear();
line_start = i + 1;
} else if (buffer[i] == '\r') {
// Do nothing.
} else {
line.append(1, buffer[i]);
}
}
accum.append(&buffer[line_start]);
return nbytes;
}
void
impl::muxer::mux(volatile const bool& terminate)
{
tools::auto_array< struct pollfd > poll_fds(new struct pollfd[m_nfds]);
for (size_t i = 0; i < m_nfds; i++) {
poll_fds[i].fd = m_fds[i];
poll_fds[i].events = POLLIN;
}
size_t nactive = m_nfds;
while (nactive > 0 && !terminate) {
int ret;
while (!terminate && (ret = safe_poll(poll_fds.get(), 2, 250)) == 0) {}
for (size_t i = 0; !terminate && i < m_nfds; i++) {
if (poll_fds[i].events == 0)
continue;
if (poll_fds[i].revents & POLLHUP) {
// Any data still available at this point will be processed by
// a call to the flush method.
poll_fds[i].events = 0;