Exemplo n.º 1
0
static void parse(char *line)
{
    char            *argv[ARGV_MAX];
    int             argc;
    char            *tmp;
    int             len;
    redirect_map_t  map;

    argc = 0;
    tmp = line;

    len = strlen(line);
    if (line[len - 1] == '\n')
        line[len - 1] = 0;

    if (parse_redirects(line, &map) < 0)
        return;

    for (;;) {
        /* Ignore leading whitespace.
        */
        while (*tmp && isspace(*tmp))
            tmp++;
        if (!*tmp)
            break;

        argv[argc++] = tmp;

        /* Token is everything up to trailing whitespace.
        */
        while (*tmp && !isspace(*tmp))
            tmp++;
        if (!*tmp)
            break;

        /* Null-terminate token.
        */
        *tmp++ = 0;
    }

    argv[argc] = NULL;

    if (!argc)
        return;

    execute(argc, argv, &map);
}
Exemplo n.º 2
0
Arquivo: c_cmd.cpp Projeto: pouba/os
int parse_cmd(char* cmd, run_params* par, run_params* parent_par, int wait) {
	//printf("*** %s ***\n", cmd);

	int pos, i;
	int return_val = 0;

	char* cmd_itself = (char*)malloc(sizeof(char) * MAX_CMD_LEN);
	char* input = (char*)malloc(sizeof(char) * MAX_PATH_LEN);
	char* output = (char*)malloc(sizeof(char) * MAX_PATH_LEN);
	char* err_output = (char*)malloc(sizeof(char) * MAX_PATH_LEN);
	int out_append = 0;
	for (i = 0; i < MAX_PATH_LEN; i++) {
		input[i] = '\0';
		output[i] = '\0';
		err_output[i] = '\0';
	}

	char** args = (char**)malloc(sizeof(char*) * MAX_ARGC);
	char* arg = (char*)malloc(sizeof(char) * MAX_PARAM_LEN);
	int argc = 0;

	pos = 0;
	int ret;
	ret = fill_string(cmd, &pos, cmd_itself);
	if (ret != 0) {
		return_val = 0; // empty command
		goto end;
	}

	while (1) {
		ret = fill_string(cmd, &pos, arg);
		if (ret != 0) break;

		ret = parse_redirects(cmd, &pos, arg, input, output, err_output, &out_append);
		if (ret == 0) {
			continue;
		}
		else if (ret == 1) {
			break;
		}
		else {
			if (argc >= MAX_ARGC) {
				return_val = 2;
				goto end;
			}

			args[argc] = arg;
			argc++;
			arg = (char*)malloc(sizeof(char) * MAX_PARAM_LEN);
		}
	}

	run_params* nParams = make_params(par, input, output, err_output, out_append, args, argc, cmd_itself);
	if (nParams == NULL) return 3;

	if (strcmp(cmd_itself, "exit") == 0) {
		if (par->secret_params == 2) {
			return 1;
		}
		if (par->secret_params == 1) {
			par->in->autoclose = 1;
			par->err->autoclose = 1;
			par->out->autoclose = 1;
			c_run((LPTHREAD_START_ROUTINE)(c_exit), nParams, wait);
		}
	}
	else if (strcmp(cmd_itself, "dir") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(dir), nParams, wait);
	}
	else if (strcmp(cmd_itself, "echo") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(echo), nParams, wait);
	}
	else if (strcmp(cmd_itself, "scan") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(scan), nParams, wait);
	}
	else if (strcmp(cmd_itself, "rand") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(random), nParams, wait);
	}
	else if (strcmp(cmd_itself, "cd") == 0) {
		cd(nParams, parent_par);
	}
	else if (strcmp(cmd_itself, "tree") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(tree), nParams, wait);
	}
	else if (strcmp(cmd_itself, "pipe") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(c_pipe), nParams, wait);
	}
	else if (strcmp(cmd_itself, "type") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(type), nParams, wait);
	}
	else if (strcmp(cmd_itself, "info") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(info), nParams, wait);
	}
	else if (strcmp(cmd_itself, "rm") == 0 || strcmp(cmd_itself, "rd") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(rm), nParams, wait);
	}
	else if (strcmp(cmd_itself, "sort") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(sort), nParams, wait);
	}
	else if (strcmp(cmd_itself, "mkdir") == 0 || strcmp(cmd_itself, "md") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(mkdir), nParams, wait);
	}
	else if (strcmp(cmd_itself, "freq") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(freq), nParams, wait);
	}
	else if (strcmp(cmd_itself, "wc") == 0) {
		c_run((LPTHREAD_START_ROUTINE)(wc), nParams, wait);
	}
	else if (strcmp(cmd_itself, "cmd") == 0) {
		nParams->secret_params = 2;
		c_run((LPTHREAD_START_ROUTINE)(c_cmd_run), nParams, wait);
	}
	else {
		c_run((LPTHREAD_START_ROUTINE)(c_non_existent), nParams, wait);
	}

end: 
	return return_val;
}