Exemple #1
0
int main( int argc, char** argv ) {
  command_t cmd;
  char line[MAXSTRLEN];
  char fullpath[MAXSTRLEN];
  int done = FALSE;

  while (!done) {
    printf(">> ");
    fgets(line, MAXSTRLEN, stdin);
    line[my_strlen(line)-1] = '\0'; // get rid of newline
    parse(line, &cmd);

    if (my_strequal(cmd.name, "exit")) {
      done = TRUE;
    }
    else if (is_builtin(&cmd)) {
      do_builtin(&cmd);
    }
    else if (find_fullpath(fullpath, &cmd)) {
      // NOTE: find_fullpath() is called again in execute
      execute(&cmd);
    }
    else if (line[0] != '\0') {   // non-blank line entered
      printf("invalid command\n");
    }
    
    cleanup(&cmd);
  }
  
  return 0;
} // end main function
// -----------------------------------
// Main function
// Arguments:	argc = number of arguments suppled by user
//				argv = array of argument values
//
//
int main( int argc, char** argv ) {
	int done;
	char input[255];
	command_t temp_command;
	char tempPath[255];

	//if statements for signal handling
	if (signal(SIGCHLD, sig_child_handler) == SIG_ERR) {
		perror("Unable to create new SIGCHLD signal handler!");
		exit(-1);
	}
	// //if statements for ctrl-c handling
	// if (signal(SIGINT, sig_int_handler) == SIG_ERR) {
	// 	perror("Unable to create new SIGCHLD signal handler!");
	// 	exit(-1);
	// }

	done = FALSE;
	while (!done) {
		printf("$");
		fflush(stdout);
		gets(input);
		parse(input, &temp_command);
		if (stringCompare(temp_command.name,"exit")==1)
		{
			done = TRUE;
		}
		else if (is_builtin(&temp_command))
		{
			do_builtin(&temp_command);
		}
		else if (find_fullpath(tempPath, &temp_command))
		{
			execute(&temp_command);
		}
		else {
			printf("Command not found.\n");
		}
		cleanup(&temp_command);
	}

	// printf( "CSCI 340 Homework Assignment 2 - Have Fun!\n" );

	return 0;

} // end main function
Exemple #3
0
void run_cmd(char *cmdLine, int mode)
{
	char *histCmd;
	int redirect_status;
	int loopback = 0;

start:
	histCmd = strdup(cmdLine);

	redirect_status = check_redirection(cmdLine);
	if (redirect_status == REDIR_ERROR) {

		if (mode == BATCH_MODE) {
			display_full_command(histCmd);
		}

		printError();
		free(cmdLine);
		free(histCmd);
		return;
	}

	char **shArgv = parseInput(cmdLine);

	if (mode == BATCH_MODE && shArgv[0] != NULL && loopback == 0) {
		display_full_command(histCmd);
	}

	int builtin = is_builtin(shArgv);
	if (builtin != -1 && redirect_status == REDIR_OK_REDIR) {
		printError();
		free(cmdLine);
		free(shArgv);
		free(histCmd);
		return;
	}

	if(builtin == 2) {
		int isValid = check_re_exec(shArgv);
		if(isValid) {
			free(cmdLine);
			free(shArgv);
			free(histCmd);

			cmdLine = strdup(re_exec_cmd);

			free(re_exec_cmd);

			loopback = 1;
			goto start;
		} else {
			printError();
			free(cmdLine);
			free(shArgv);
			free(histCmd);
			return;
		}
	}

	if(shArgv[0] != NULL) {

		add_cmd(histCmd);

		if(do_builtin(shArgv)) {
			free(histCmd);
			free(cmdLine);
			free(shArgv);
			return;
		} else {
			do_execute(shArgv,
						redirect_status == REDIR_OK_REDIR ?
							1 : 0, redir_file);
		}
	}
	free(cmdLine);
	free(shArgv);
	free(histCmd);
}