示例#1
0
// Returns a command object based on given input.
command_struct *get_command_struct(char *input_ptr)
{
	if(!*input_ptr) return 0;
	command_struct *malloc_ptr = calloc(sizeof(command_struct), 1);
	int num_args = get_num_args(input_ptr);
	if(num_args >= 1) malloc_ptr->args_malloc_ptr[0] = get_arg_malloc_ptr(input_ptr, 0);
	str_tolower(malloc_ptr->args_malloc_ptr[0]);
	if(num_args >= 2) malloc_ptr->args_malloc_ptr[1] = get_arg_malloc_ptr(input_ptr, 1);
	if(num_args >= 3) malloc_ptr->args_malloc_ptr[2] = get_long_arg_malloc_ptr(input_ptr, 2);
	return malloc_ptr;
}
示例#2
0
文件: commander.c 项目: elc1798/wish
int get_and_run_userin() {
    char buff[256];
    fgets(buff, sizeof(buff), stdin);

    // Buff will have space-separated command line arguments, then a newline,
    // then a terminating null. We can use strsep to get rid of the newline.
    // Since fgets will terminate on the first newline, we can be certain there
    // is no next command after it

    char *ptr_to_buff = buff;
    char *line = strsep(&ptr_to_buff, "\n");
    // Blank lines should just return 0, since a blank command doesn't 'crash'
    if (!(*line)) {
        return 0;
    }

    int num_commands = 1;

    int i; for (i = 0; line[i]; i++) {
        if (line[i] == ';') num_commands++;
    }
    char *commands[num_commands];
    for (i = 0; i < num_commands; i++) {
        commands[i] = strsep(&line, ";");
    }

    int retval;
    int counter; for (counter = 0; counter < num_commands; counter++) {
        if (!(commands[counter][0])) {
            retval = 0;
            continue;
        }
        // Get the number of arguments from get_num_args
        int num_args = get_num_args(commands[counter]);

        // Split the chunks by spaces and put into an array of char pointers of
        // size num_args + 1
        char *args[num_args + 1];
        for (i = 0; i < num_args; i++) {
            args[i] = strsep(&commands[counter], " ");
            if (!args[i][0]) i--;
        }
        if (!args[num_args - 1][0]) args[num_args - 1] = NULL;
        args[num_args] = NULL;

        char *command[num_args];
        int j;
        int redirected = 0;
        // Check for redirection or pipe symbols
        for (i = 0; i < num_args; i++) {
            if (!strcmp(args[i], ">")) {
                for (j = 0; j < i; j++) {
                    command[j] = args[j];
                }
                command[i] = NULL;
                retval = stdout_to_file_overwrite(command, args[num_args - 1]);
                redirected = 1;
            } else if (!strcmp(args[i], ">>")) {
                for (j = 0; j < i; j++) {
                    command[j] = args[j];
                } 
                command[i] = NULL;
                retval = stdout_to_file_append(command, args[num_args - 1]);
                redirected = 1;
            } else if (!strcmp(args[i], "2>")) {
                for (j = 0; j < i; j++) {
                    command[j] = args[j];
                } 
                command[i] = NULL;
                retval = stderr_to_file_overwrite(command, args[num_args - 1]);
                redirected = 1;
            } else if (!strcmp(args[i], "2>>")) {
                for (j = 0; j < i; j++) {
                    command[j] = args[j];
                } 
                command[i] = NULL;
                retval = stderr_to_file_append(command, args[num_args - 1]);
                redirected = 1;
            } else if (!strcmp(args[i], "2>1")) {
                for (j = 0; j < i; j++) {
                    command[j] = args[j];
                } 
                command[i] = NULL;
                retval = stderr_to_stdout(command);
                redirected = 1;
            } else if (!strcmp(args[i], "&>")) {
                retval = stdout_to_stderr(command);
                redirected = 1;
            } else if (!strcmp(args[i], "<")) {
                for (j = 0; j < i; j++){
                    command[j] = args[j];
                } 
                command[i] = NULL;
                retval = file_to_stdin(command, args[num_args - 1]);
                redirected = 1;
            } else if (!strcmp(args[i], "|")) {
                char *command1[i + 1];
                char *command2[num_args - i + 1];
                for (j = 0; j < i; j++){
                    command1[j] = args[j];
                } 
                command1[i] = NULL;

                for (j = i + 1; j <= num_args; j++){
                    command2[j - i - 1] = args[j];
                }
                retval = pipe_of_wish(command1, command2);
                redirected = 1;
            } 
        }

        if (!redirected) retval = execute(args[0], args);
    }
    return retval;
}