For some reason, i am getting output that i am not expecting and i don't know why. It's almost like the terminals I'm used to filter out some of the output and only display certain things to the user or something. Its pretty strange, i've included the code I've been using and the output of the code for both csh and bash. I know i could parse the data i need easily, but im wondering if anyone has a better way or at least an explanation of why the output I'm getting isn't what I'm seeing when i use csh and bash from the commandline.
This code is messy, but i wanted it all to compile with one call to g++ for testing.
GeSHi (cpp):
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
#include <fstream>
class liniocontroller
{
public:
liniocontroller();
void run();
int write(std::string);
void read();
virtual void readhandler(std::string);
private:
int master;
int slave;
std::string slavename;
};
liniocontroller::liniocontroller()
{
//open pseudo-terminal "master" multiplexer device, and save the file descriptor in master
master=open("/dev/ptmx",O_RDWR);
//open the pseudo-terminal "master"
if(grantpt(master)==0)
{
//unlock a pseudo-terminal master/slave pair
slave=unlockpt(master);
//save the "slave" end of the terminals device file name as slavename
slavename=ptsname(master);
}
}
void liniocontroller::run()
{
//create a commandline where shell program receives input from and outputs to slave end of terminal
std::string cmdline="bash >"+slavename+" <"+slavename;
//run that commandline, in a new thread so we still have control here
boost::thread runthread(boost::bind(system,cmdline.c_str()));
//start up a thread to read data that is sent back to the master end
boost::thread readthread(boost::bind(&liniocontroller::read,this));
}
void liniocontroller::read()
{
char buffer='\0';
std::string indata;
//read one charachter at a time and append it to a string while on the same line.
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(master,&rdfs);
struct timeval tv;
//Wait up to 1 second.
tv.tv_sec = 0;
tv.tv_usec = 0;
while(/*select(master,&rdfs,NULL,NULL,&tv) && */buffer != '\n')
{
::read(master,&buffer,1);
indata+=buffer;
}
//send data to the handler to be further processed
readhandler(indata);
//start over.
read();
}
void liniocontroller::readhandler(std::string indata)
{
std::ofstream fout("output2.txt",std::ios::app);
fout<<indata;
fout.close();
}
int liniocontroller::write(std::string outdata)
{
outdata+='\n';//stdin is line buffered, so add this so it knows to act on what you wrote.
return ::write(master,outdata.c_str(),outdata.size());
}
int main()
{
liniocontroller control;
control.run();
control.write("fortune");
std::cin.get();
return 0;
}
Created by GeSHI 1.0.7.18
output from csh -
http://www.fileupyours.com/view/172153/output.txtoutput from bash-
http://www.fileupyours.com/view/172153/output2.txt