コード例 #1
0
ファイル: myshell2.c プロジェクト: LainIwakura/Assignments
int exec_pipe(char *cmd)
{
  int num_pipes = 0;
  int fd[2];
  int bgfg = 0; // Never run background commands with pipes.
  char original_cmd[255];
  char *tok, prog1[256], prog2[256], *arg_list1[255], *arg_list2[255];

  strcpy(original_cmd, cmd);
  num_pipes = count_pipes(cmd);

  // Only support for singly piped commands.
  if (num_pipes > 1)
  {
    printf("The shell currently doesn't support multiple pipes! Sorry!\n");
    return 0;
  }
  else
  {
    // Get our two programs
    // They have arguments.
    tok = strtok(original_cmd, "|");
    strcpy(prog1, tok);
    tok = strtok(NULL, " |");
    strcpy(prog2, tok);

    // Process our arguments
    process_args(prog1, arg_list1, &bgfg);
    process_args(prog2, arg_list2, &bgfg);

    pipe(fd);
    // Execute prog1
    if (fork() > 0)
    {
      close(fd[0]);
      dup2(fd[1], 1);
      execvp(arg_list1[0], arg_list1);
      exit(22);
    }
    else
    {
      close(fd[1]);
      dup2(fd[0], 0);
      execvp(arg_list2[0], arg_list2);
      exit(22);
    }
  }

  return 1;
}
コード例 #2
0
ファイル: ns.c プロジェクト: nwam/noah-shell
/* main */
int main() {

    bool    exit_flag = false;
    int     oldest_history = HISTORY_SIZE - 1,
            status;
    char    *username,
            input[MAX_INPUT_SIZE],
            history[HISTORY_SIZE][MAX_INPUT_SIZE] = {'\0'},
                    *tokens[CMD_MAX];

    signal(SIGINT, handle_sigint);
    //shell loop
    while(!exit_flag) {

        //print the prompt and get the input
        printf("%s> " , get_username());
        fgets(input, MAX_INPUT_SIZE, stdin);
        format_input(input);

        //built-in exit function
        if(!strcmp(input, "exit")) exit_flag = true;

        //built-in history function
        else if(!strcmp(input, "history")) {
            int i;
            for(i = oldest_history+1; i!=oldest_history; i = (i+1)%HISTORY_SIZE) {
                if(history[i][0] != '\0') printf("%s\n", history[i]);
            }
        }

        //non-built-in function
        else if(strcmp(input, "")) {
            int     token_index,
                    num_tokens;
            pid_t   pid;
            char   *tokens[CMD_MAX];

            //get the token list and size
            num_tokens = make_tokenlist(input, tokens);

            //fork a child process
            pid = fork();

            //parent: wait for child to finish
            if(pid >  0) {
                pid = wait(&status);
            }

            //child: execute command
            if(pid == 0) {

                //redirect output
                token_index = contains_token(tokens, num_tokens, ">");
                if(token_index >= 0 && token_index+1 < num_tokens) {
                    int fd_o = open(tokens[token_index+1], O_CREAT|O_TRUNC|O_WRONLY, 0644);
                    if(dup2(fd_o, STDOUT_FILENO)<0) {
                        perror("dup2 error");
                        exit(EXIT_FAILURE);
                    }
                    close(fd_o);

                    //remove ">" and the output file from the list of arguments
                    tokens[token_index  ] = '\0';
                    tokens[token_index+1] = '\0';
                }

                //redirect input
                token_index = contains_token(tokens, num_tokens, "<");
                if(token_index >= 0 && token_index+1 < num_tokens) {
                    int fd_i = open(tokens[token_index+1], O_RDONLY);
                    dup2(fd_i, STDIN_FILENO);
                    close(fd_i);

                    //remove "<" and the input file from the list of arguments
                    tokens[token_index  ] = '\0';
                    tokens[token_index+1] = '\0';
                }

                //handle pipe
                int num_pipes = count_pipes(tokens, num_tokens);
                if(num_pipes > 0) {

                    //get the indices of the pipes to extract the commands
                    int pipe_indices[CMD_MAX];
                    get_pipe_indices(tokens, num_tokens, pipe_indices);

                    //extract all the commands
                    char *commands[CMD_MAX][CMD_MAX];
                    for(int i=0; i<=num_pipes; i++) {
                        extract_tokens(tokens, commands[i], pipe_indices[i]+1, pipe_indices[i+1]);
                    }

                    //connect and run all the pipe commands
                    int fds[2],
                        in = STDIN_FILENO;
                    for(int i=0; i<num_pipes; i++) {
                        pipe(fds);
                        execute_piped_process(in, fds[1], commands[i]);
                        close(fds[1]);
                        in = fds[0];
                    }

                    //execute the last command
                    if(in != STDIN_FILENO) dup2(in, STDIN_FILENO);
                    execvp(commands[num_pipes][0], commands[num_pipes]);
                }

                //execute command
                execvp(tokens[0], tokens);
                printf("%s: invalid command\n", tokens[0]);
                exit(EXIT_FAILURE);
            }
        }

        //update history
        if(strcmp(input, "") && status==0) {
            strcpy( history[oldest_history], input );
            oldest_history--;
            if(oldest_history < 0) oldest_history = HISTORY_SIZE - 1;
        }

    }

}
コード例 #3
0
ファイル: devdrv.c プロジェクト: jvesely/helenos
/** Create pipes for a device.
 *
 * This is more or less a wrapper that does following actions:
 * - allocate and initialize pipes
 * - map endpoints to the pipes based on the descriptions
 * - registers endpoints with the host controller
 *
 * @param[in] endpoints Endpoints description, NULL terminated.
 * @param[in] config_descr Configuration descriptor of active configuration.
 * @param[in] config_descr_size Size of @p config_descr in bytes.
 * @param[in] interface_no Interface to map from.
 * @param[in] interface_setting Interface setting (default is usually 0).
 * @param[out] pipes_ptr Where to store array of created pipes
 *	(not NULL terminated).
 * @param[out] pipes_count_ptr Where to store number of pipes
 *	(set to NULL if you wish to ignore the count).
 * @return Error code.
 */
int usb_device_create_pipes(usb_device_t *usb_dev,
    const usb_endpoint_description_t **endpoints)
{
	assert(usb_dev);
	assert(usb_dev->descriptors.full_config);
	assert(usb_dev->pipes == NULL);
	assert(usb_dev->pipes_count == 0);

	size_t pipe_count = count_pipes(endpoints);
	if (pipe_count == 0) {
		return EOK;
	}

	usb_endpoint_mapping_t *pipes =
	    calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
	if (pipes == NULL) {
		return ENOMEM;
	}

	/* Now initialize. */
	for (size_t i = 0; i < pipe_count; i++) {
		pipes[i].description = endpoints[i];
		pipes[i].interface_no = usb_dev->interface_no;
		pipes[i].interface_setting =
		    usb_dev->alternate_interfaces.current;
	}

	/* Find the mapping from configuration descriptor. */
	int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
	    usb_dev->descriptors.full_config,
	    usb_dev->descriptors.full_config_size,
	    usb_dev->bus_session);
	if (rc != EOK) {
		free(pipes);
		return rc;
	}

	/* Register created pipes. */
	for (size_t i = 0; i < pipe_count; i++) {
		if (pipes[i].present) {
			rc = usb_pipe_register(&pipes[i].pipe,
			    pipes[i].descriptor->poll_interval);
			if (rc != EOK) {
				goto rollback_unregister_endpoints;
			}
		}
	}

	usb_dev->pipes = pipes;
	usb_dev->pipes_count = pipe_count;

	return EOK;

	/*
	 * Jump here if something went wrong after endpoints have
	 * been registered.
	 * This is also the target when the registration of
	 * endpoints fails.
	 */
rollback_unregister_endpoints:
	for (size_t i = 0; i < pipe_count; i++) {
		if (pipes[i].present) {
			usb_pipe_unregister(&pipes[i].pipe);
		}
	}

	free(pipes);
	return rc;
}