当前位置: 首页>>代码示例>>C++>>正文


C++ Parse::pipeThese方法代码示例

本文整理汇总了C++中Parse::pipeThese方法的典型用法代码示例。如果您正苦于以下问题:C++ Parse::pipeThese方法的具体用法?C++ Parse::pipeThese怎么用?C++ Parse::pipeThese使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Parse的用法示例。


在下文中一共展示了Parse::pipeThese方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
	cout << "\nRunning Daniel's Shell\n" << "Created by dyc8av\n" << endl;
	//while there is still input from the user
	while(true) {

		Parse p; // create parse in order to call methods from
		string input = ""; // where raw input is stored
		char* cinput = NULL;
		vector<pid_t> spid;
		int status;
		vector<string> sTokens; //where tokens are stored unorganized
		int nTokens = 0;
		getline(cin,input);

// -------------------- parsing input --------------------------

		if(cin.eof()) { // break if end-of-file
			exit(0);
		}
		else if(input == "exit") { // break if "exit"
			exit(0);
	}
		else if(input.length() > 100) { // reload while loop if over 100 characters
			cout << "Your input is over 100 characters." << endl;
			continue;
		}
		else if(!p.legalChar(input)) { // reload while loop of invalid characters
			cout << "Your input contains invalid characters." << endl;
			continue;
		}
		else if (!p.checkPipe(input)) { // check pipe location is valid
			cout << "Your input ends in a pipe!" << endl;
			continue;
		}
		else if (!p.checkIn(input)) { // check if < location is valid
			cout << "Your input ends in an operator!" << endl;
			continue;
		}
		else if (!p.checkOut(input)) { // check if > location is valid
			cout << "Your input ends in an operator!" << endl;
			continue;
		}
		else {

		//parse the input line into appropiate tokens
			cinput = new char[input.size() +1];
			strcpy(cinput,input.c_str());
			char* tokens = strtok(cinput," ");
		while(tokens != NULL) { //read and split into tokens 
			sTokens.push_back(strdup(tokens));
			nTokens++;
			tokens = strtok(NULL," ");
		}

// ---------------------- end parsing -------------------------------

		//create vector of list of inputs to be execv()
		//combine into token groups with operators
		vector< vector<string> > tokenVec = p.tokenGroup(sTokens,nTokens);

		//execv the commands
		for(int i = 0; i < tokenVec.size(); i++) {
			// if the command is a pipe
			if(tokenVec[i][0] == "|") {
				const char **lastVec1 = p.changev(tokenVec[i-1]);
				const char **lastVec2 = p.changev(tokenVec[i+1]);
				p.pipeThese((char **) lastVec1,(char **) lastVec2);
			}
			// if direction in
			else if(tokenVec[i][0] == "<") {
				const char **execVec = p.changev(tokenVec[i-1]);
				const char **fileVec = p.changev(tokenVec[i+1]);
				p.redirectIN((char **) execVec, fileVec[0]);
			}
			// if direction out
			else if(tokenVec[i][0] == ">") {
				const char **execVec = p.changev(tokenVec[i-1]);
				const char **fileVec = p.changev(tokenVec[i+1]);
				p.redirectOUT((char **) execVec, fileVec[0]);

			}
			else {
				if(i < tokenVec.size()-1) {
					// check if next token is a command, then wait to execv() that command
					if(tokenVec[i+1][0] == "|" || tokenVec[i+1][0] == ">" || tokenVec[i+1][0] == "<" ) {
						continue;
					}
					else {
						pid_t pid;
						pid = fork(); // fork the process
						if(pid == 0) {// child
						//convert vector<string> to appropiate format
							const char **lastVec = p.changev(tokenVec[i]);
							execv(lastVec[0], (char **) lastVec);
						}
						else {
							spid.push_back(pid);
						}
					}
//.........这里部分代码省略.........
开发者ID:danielycheng,项目名称:Custom-UNIX-Shell,代码行数:101,代码来源:main.cpp


注:本文中的Parse::pipeThese方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。