#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
extern "C" {
#include <pthread.h>
}
#include <memory.h>
#include <sys/stat.h>
#define FIFO "myfifo"
char g_cmdstr[128][128];
char g_cmddscstr[128][128];
char g_parastr[128][128];
QString g_Qcmd;
QString g_Qpara;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
strcpy(g_cmdstr[0],"apt-get");
strcpy(g_cmddscstr[0],"释放删除软件的空间");
strcpy(g_parastr[0],"autoremove");
strcpy(g_cmdstr[1],"apt-get");
strcpy(g_cmddscstr[1],"释放已经卸载软件包的安装文件");
strcpy(g_parastr[1],"autoclean");
strcpy(g_cmdstr[2],"apt-get");
strcpy(g_cmddscstr[2],"删除安装的软件的备份");
strcpy(g_parastr[2],"clean");
ui->comboBox->addItem(g_cmddscstr[0]);
ui->comboBox->addItem(g_cmddscstr[1]);
ui->comboBox->addItem(g_cmddscstr[2]);
int i=3;
strcpy(g_cmdstr[i],"df");
strcpy(g_cmddscstr[i],"查看硬盘空间");
strcpy(g_parastr[i],"-h");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"free");
strcpy(g_cmddscstr[i],"查看memory");
strcpy(g_parastr[i],"-m");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"uname");
strcpy(g_cmddscstr[i],"查看 version");
strcpy(g_parastr[i],"-r");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ps");
strcpy(g_cmddscstr[i],"查看all process aux");
strcpy(g_parastr[i],"aux");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ps");
strcpy(g_cmddscstr[i],"查看all process");
strcpy(g_parastr[i],"-ef");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ps");
strcpy(g_cmddscstr[i],"查看term process");
strcpy(g_parastr[i],"-s");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ls");
strcpy(g_cmddscstr[i],"查看目录下文件");
strcpy(g_parastr[i],"");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ls");
strcpy(g_cmddscstr[i],"查看目录下隐藏文件");
strcpy(g_parastr[i],"-a");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"ls");
strcpy(g_cmddscstr[i],"查看目录下文件详细情况");
strcpy(g_parastr[i],"-l");
ui->comboBox->addItem(g_cmddscstr[i]);
i++;
strcpy(g_cmdstr[i],"man");
strcpy(g_cmddscstr[i],"查看命令帮助");
strcpy(g_parastr[i],"ls");
ui->comboBox->addItem(g_cmddscstr[i]);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbtn_run_clicked()
{
ui->textEdit->clear();
//runfork(); //ok
runsystem(); //ok2
}
char* MainWindow::cmd_system(const char* command)
{
char* result = "";
FILE *fpRead;
fpRead = popen(command, "r");
char buf[1024];
memset(buf,'\0',sizeof(buf));
while(fgets(buf,1024-1,fpRead)!=NULL)
{
//buf[strlen(buf)-1]=0; //去掉0x0a
if(buf[strlen(buf)-1]==0x0a) buf[strlen(buf)-1]=0; //去掉0x0a
ui->textEdit->append(buf);
result = buf;
}
if(fpRead!=NULL)
pclose(fpRead);
return result;
}
void MainWindow::runsystem()
{
pid_t pc,pr;
char cmdbuf[200],parabuf[200],tmp[512];
//ui->ledt_cmd.text();
QString cmdstr= ui->ledt_cmd->text();
QString parastr=ui->ledt_para->text();
int apipe[2],i,len;
char buf[BUFSIZ]; //ctrl+BUFSIZ find BUFSIZ=8192
memset(buf,0,BUFSIZ);
for(i=0;i<200;i++)
{
cmdbuf[i]=0;
parabuf[i]=0;
}
strcpy(cmdbuf ,cmdstr.toLocal8Bit().data() );
strcpy(parabuf ,parastr.toLocal8Bit().data() );
memset(tmp,0,512);
strcpy(tmp,cmdbuf);
strcat(tmp," ");
strcat(tmp,parabuf);
ui->textEdit->append(tmp);
cmd_system(tmp);
}
void MainWindow::runfork()
{
pid_t pc,pr;
char cmdbuf[200],parabuf[200],tmp[512];
//ui->ledt_cmd.text();
QString cmdstr= ui->ledt_cmd->text();
QString parastr=ui->ledt_para->text();
int apipe[2],i,len;
char buf[BUFSIZ]; //ctrl+BUFSIZ find BUFSIZ=8192
memset(buf,0,BUFSIZ);
for(i=0;i<200;i++)
{
cmdbuf[i]=0;
parabuf[i]=0;
}
strcpy(cmdbuf ,cmdstr.toLocal8Bit().data() );
strcpy(parabuf ,parastr.toLocal8Bit().data() );
//if(parabuf[0]=='\0')
// parabuf[0]=0;
if(pipe(apipe) == -1){ // 创建一个管道:apipe[0]读,apipe[1]写
// 数据流向:读 <--- 写, 即apipe[1]流向apipe[0]
perror("pipe"); // 如果创建失败,输出错误原因,退出
exit(0);
}
memset(tmp,0,512);
strcpy(tmp,">");
strcat(tmp,cmdbuf);
strcat(tmp," ");
strcat(tmp,parabuf);
ui->textEdit->append(tmp);
pc = fork() ;
//if ( fork() == 0 ) {
if ( pc == 0 ) {
::close(1);
dup2( apipe[1], 1 );
::close( apipe[0] );
if(parabuf[0]=='\0')
execlp( cmdbuf, cmdbuf, NULL, NULL ); //ok //can run "apt-get clean",and not die. can run "ls"
else
execlp( cmdbuf, cmdbuf, parabuf, NULL ); //ok
exit(0);
} else {
::close(0);
dup2( apipe[0], 0 );
::close( apipe[1] );
//wait child exit!!!
int flags = fcntl(apipe[0],F_GETFL);
fcntl(apipe[0],F_SETFL,flags|O_NONBLOCK); //非阻塞设置
while(1)
{
//一次读256字节,不判断回车和换行
//mabao i=read(apipe[0],buf,256); //从apipe[0]中读取数据 ok good
//一次读1字节,判断回车和换行
char tmpbuf;
int t=0;
i=0;
t=read(apipe[0],&tmpbuf,1); //从apipe[0]中读取数据 ok good
while(t>0)
{
buf[i]=tmpbuf;
i=i+t;
t=read(apipe[0],&tmpbuf,1);
//if(tmpbuf==0x0d) break;
if(tmpbuf==0x0a) break; //ok
}
if(i>0){
ui->textEdit->append(QString::fromLocal8Bit(buf,i)); //ok
continue;
}
//sleep(1);
//do {// 使用了WNOHANG参数,waitpid不会在这里等待
pr=waitpid(pc,NULL,WNOHANG);
if (pr==0)
{
//printf("No child exit\n");
//mabao sleep(1);
usleep(10);
}
//}while (pr==0);
if(pr != 0){
if (pr==pc)
printf("successfully get child %d\n",pr);
else
printf("wait child error\n");
break;
}
}//end while
ui->textEdit->append(">");
}
//return 0;
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
ui->ledt_cmd->setText(QString::fromLocal8Bit(g_cmdstr[index],strlen(g_cmdstr[index])));
ui->ledt_para->setText(QString::fromLocal8Bit(g_parastr[index],strlen(g_parastr[index])));
}