Exemplo n.º 1
0
void executeWithPipe(char** commandArgs) {
	char* firstCommand[MAX_COMMAND_LENGTH];
	char* secondCommand[MAX_COMMAND_LENGTH];

	int i = 0;
	while (0 != strcmp(commandArgs[i], "|")) {
		firstCommand[i] = commandArgs[i];
		i++;
	}
	i++;
	int j = i;
	while (commandArgs[i] != NULL) {
		secondCommand[i-j] = commandArgs[i];
		i++;
	}
	#ifdef DEBUG
		int test = 0;
		while (firstCommand[test] != NULL) {
			printf("%s\n", firstCommand[test]);
			test++;
		}
		test = 0;
		while (secondCommand[test] != NULL) {
			printf("%s\n", secondCommand[test]);
			test++;
		}
	#endif

	char* pathArray[MAX_PATH_LENGTH];
	int pathLength = getPathArray(pathArray);
	int background = 0;

	for (i = 0; i < pathLength; i++) {
		char commandDir[100];
		struct stat lstatBuff;
		sprintf(commandDir, "%s/%s", pathArray[i], commandArgs[0]);
		if (lstat(pathArray[i], &lstatBuff) == 0) {

			#ifdef DEBUG
				printf("%s\n", pathArray[i]);
			#endif

			int childPID = fork();

			if (childPID == 0) {

				int p[2];
				int rc = pipe(p);

				if ( rc < 0 ) {
				    perror( "pipe() failed" );
				    return;
				}

				int childPID2 = fork();
				if (childPID2 == 0) {
					close(p[0]);
					dup2(p[1], 1);
					execvp(firstCommand[0], firstCommand);
				}
				else {
					close(p[1]);
					dup2(p[0], 0);
					execvp(secondCommand[0], secondCommand);
				}
				
			}
			else if (background) {
				signal(SIGCHLD, &deleteConnec);
				waitpid(-1, NULL, WNOHANG);
			}
			else {
				pid_t tempPID = waitpid(childPID, NULL, 0);
				printf("Process %d terminated.\n", tempPID);
			}
			break;
		}
	}

	


}
void drawGameScreen() {
    clear();
    //draw exterior of game
    drawBox(10, 3, 44, 34, 0);
    
    //draw path walls
    drawBox(10, 7, 11, 3, 0);
    drawBox(18, 7, 4, 14, 0);
    drawBox(18, 18, 16, 3, 0);
    drawBox(31, 7, 4, 14, 0);
    drawBox(31, 7, 18, 3, 0);
    drawBox(46, 7, 4, 24, 0);
    drawBox(19, 28, 30, 3, 0);
    drawBox(19, 28, 4, 9, 0);

    //fix up the corners
    clearPath();
    fixCorners();

    //add beginning and ending chars
    mvaddch(8, 8, '>');
    mvaddch(37, 21, ACS_DARROW);

    //draw and fill the purchase portion of the screen
    attron(COLOR_PAIR(0));
    drawPurchaseArea();
    basicSetupScreen(1);
    updateScore(0);
    attroff(COLOR_PAIR(0));
    drawTowerExplain();

    //load arrays for drawing
    TowerArray* theTowerList = getTowerArray("assets/towersLevel1.txt");
    Path* thePath = getPathArray("assets/path.txt");
    UnitListHeader * unitList = malloc(sizeof(UnitListHeader));
    int moneyAmount = STARTINGMONEY;
    initializeList(unitList);
    drawUnitTypes(unitList);
    int theScore = 0;

    drawTowers(theTowerList);

    //go to entering unit purchases (gives control back to user)
    selectUnitsInterface(thePath,unitList,&moneyAmount,theTowerList,&theScore);

    //get lowest score of highscores
    ScoreHead scores;
    scores.size = 0;
    scores.first = NULL;
    ScoreNode * currScore = NULL;
    readScores(&scores);
    currScore = scores.first;
    int numScores = 1;
    int highestScore = 0;

    while ((currScore->next != NULL) && (numScores < 9)) { 
        numScores += 1;
        currScore = currScore->next;
    }

    highestScore = -1;
    if (numScores > 8 && currScore != NULL) {
        highestScore = currScore->score;
    }

    char * enterName;
    if (theScore > highestScore) {
        enterName = drawScore(theScore,1);
        if (enterName != NULL) {
            addScore(&scores, enterName, theScore);
            writeScores(&scores);
        }
    } else {
        drawScore(theScore,0);
    }

    if (scores.size > 0) destroyScores(&scores);

}
Exemplo n.º 3
0
void executeCommand(char* command) {
	#ifdef DEBUG
		printf("Inside executeCommand\n");
		printf("%s\n", command);
	#endif

	
	
	int i = 0;

	

	char* commandArgs[MAX_COMMAND_LENGTH];
	commandArgs[0] = strtok(command, " ");
	//int p = 0;
	int background = 0;
	
	i = 0;

	while (commandArgs[i] != NULL) {
		#ifdef DEBUG
			printf("%s %d\n", commandArgs[i], i);
		#endif
		i++;
		commandArgs[i] = strtok(NULL, " ");
	}
	int commandArgsLen = i;
	for (i = 0; i < commandArgsLen; i++) {
		if (0 == strcmp(commandArgs[i], "&")) {
			//printf("%s\n", commandArgs[i]);
			background = 1;
		}
		if (0 == (strcmp(commandArgs[i], "|"))) {
			executeWithPipe(commandArgs);
			return;
		}
	}

	char* pathArray[MAX_PATH_LENGTH];
	int pathLength = getPathArray(pathArray);

	#ifdef DEBUG
		for (i = 0; i < pathLength; i++) {
			printf("%s\n", pathArray[i]);
		}
	#endif

	#ifdef DEBUG
		printf("%s", command);
		printf("------\n");
	#endif

	if (0 == strcmp(commandArgs[0], "cd")) {
		chdir(commandArgs[1]);
		return;
	}
	if (0 == strcmp(commandArgs[0], "echo")) {
		if ('$' == commandArgs[1][0]) {
			char* enviroVar = getenv(++commandArgs[1]);
			printf("%s\n", enviroVar);
		}
		else {
			printf("%s\n", commandArgs[1]);
		}
		return;
	}
	for (i = 0; i < pathLength; i++) {
		char commandDir[100];
		struct stat lstatBuff;
		sprintf(commandDir, "%s/%s", pathArray[i], commandArgs[0]);
		if (lstat(pathArray[i], &lstatBuff) == 0) {

			#ifdef DEBUG
				printf("%s\n", pathArray[i]);
			#endif

			int childPID = fork();

			if (childPID == 0) {
				execvp(commandArgs[0], commandArgs);
			}
			else if (background) {
				signal(SIGCHLD, &deleteConnec);
				printf("[process running in background with pid %d]\n", childPID);
				waitpid(-1, NULL, WNOHANG);
			}
			else {
				
				pid_t tempPID = waitpid(childPID, NULL, 0);
				printf("Process %d terminated.\n", tempPID);
			}
			return;
		}
	}

	printf("ERROR: command '%s' not found\n", commandArgs[0]);
	
	
}