Skip to content

Instantly share code, notes, and snippets.

@shawndooley
Last active May 28, 2020 12:51
Show Gist options
  • Save shawndooley/2da16472d29eb4b247897c21fc58f4a7 to your computer and use it in GitHub Desktop.
Save shawndooley/2da16472d29eb4b247897c21fc58f4a7 to your computer and use it in GitHub Desktop.
Boost ASIO PTY example
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <pty.h>
#include <string>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
void do_async_read();
constexpr char linkname[] = "/tmp/OUT";
boost::asio::io_service ioservice;
boost::asio::posix::basic_stream_descriptor<> stream(ioservice);
boost::asio::streambuf stream_buffer_;
void data_send(void) { std::cout << "omg sent" << std::endl; }
void handle_read(boost::system::error_code ec, size_t bytes_transferred) {
std::istream istream(&stream_buffer_);
std::string str;
std::getline(istream, str, '\r');
std::cout << str << std::endl;
do_async_read();
}
void do_async_read() {
boost::asio::async_read_until(
stream, stream_buffer_, '\r',
boost::bind(&handle_read, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred()));
}
void pty_test() {
int master;
int slave;
char ptyname[64];
openpty(&master, &slave, ptyname, NULL, NULL);
std::cout << ptyname << std::endl;
unlink(linkname);
symlink(ptyname, linkname);
stream.assign(master);
do_async_read();
ioservice.run();
;
}
int main(int argc, const char *argv[]) { pty_test(); }
@shawndooley
Copy link
Author

Not well organized, but a working example I can reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment