Exemplo n.º 1
0
void console_execute_buffer()
{
	const char *argv[MAX_COMMAND_ARGS];
	int argc;
	
	// Split arguments
	char *line = command_buffer;
	for (argc = 0; ; ++argc)
	{
		// Skip leading whitespace
		while (1)
		{
			if (*line == 0)
			{
				goto split_done;
			}
			if (*line != ' ')
			{
				break;
			}
			++line;
		}
		
		if (argc == MAX_COMMAND_ARGS)
		{
			printf("*** Too many arguments\n");
			return;
		}
		
		// Store the beginning of this word
		argv[argc] = line;
		
		// Find the end of this word
		while (*line != ' ')
		{
			if (*line == 0)
			{
				// End of the last word
				++argc;
				goto split_done;
			}
			++line;
		}
		*line++ = 0;
	}
split_done:
	
	if (argc)
	{
		subcommand(argc, argv, (void *)commands);
	}
}
Exemplo n.º 2
0
	CmdResult Handle (const std::vector<std::string> &parameters, User *user)
	{
		std::string subcommand(parameters[0].length(), ' ');
		std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);

		if (subcommand == "REQ")
		{
			if (parameters.size() < 2)
				return CMD_FAILURE;

			CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);

			// tokenize the input into a nice list of requested caps
			std::string cap_;
			irc::spacesepstream cap_stream(parameters[1]);

			while (cap_stream.GetToken(cap_))
			{
				std::transform(cap_.begin(), cap_.end(), cap_.begin(), ::tolower);
				Data.wanted.push_back(cap_);
			}

			reghold.set(user, 1);
			FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));

			if (Data.ack.size() > 0)
			{
				std::string AckResult = irc::stringjoiner(Data.ack);
				user->WriteCommand("CAP", "ACK :" + AckResult);
			}

			if (Data.wanted.size() > 0)
			{
				std::string NakResult = irc::stringjoiner(Data.wanted);
				user->WriteCommand("CAP", "NAK :" + NakResult);
			}
		}
		else if (subcommand == "END")
		{
			reghold.set(user, 0);
		}
		else if ((subcommand == "LS") || (subcommand == "LIST"))
		{
			CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST);

			reghold.set(user, 1);
			FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));

			std::string Result = irc::stringjoiner(Data.wanted);
			user->WriteCommand("CAP", subcommand + " :" + Result);
		}
		else if (subcommand == "CLEAR")
		{
			CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);

			reghold.set(user, 1);
			FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));

			std::string Result = irc::stringjoiner(Data.ack);
			user->WriteCommand("CAP", "ACK :" + Result);
		}
		else
		{
			user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
			return CMD_FAILURE;
		}

		return CMD_SUCCESS;
	}
Exemplo n.º 3
0
/*
 * Read commands until we are told to stop.
 */
static void docommands(void)
{
    char *cp;
    int len;
    NUM num1;
    NUM num2;
    BOOL have1;
    BOOL have2;

    while (TRUE) {
	intflag = FALSE;
	printf(": ");
	fflush(stdout);

	if (fgets(buf, sizeof(buf), stdin) == NULL)
	    return;

	len = strlen(buf);
	if (len == 0)
	    return;

	cp = &buf[len - 1];
	if (*cp != '\n') {
	    fprintf(stderr, "Command line too long\n");
	    do {
		len = fgetc(stdin);
	    } while ((len != EOF) && (len != '\n'));

	    continue;
	}
	while ((cp > buf) && isblank(cp[-1]))
	    cp--;
	*cp = '\0';

	cp = buf;
	while (isblank(*cp))
	    *cp++;

	have1 = FALSE;
	have2 = FALSE;

	if ((curnum == 0) && (lastnum > 0)) {
	    curnum = 1;
	    curline = lines.next;
	}
	if (!getnum(&cp, &have1, &num1))
	    continue;

	while (isblank(*cp))
	    cp++;

	if (*cp == ',') {
	    cp++;
	    if (!getnum(&cp, &have2, &num2))
		continue;

	    if (!have1)
		num1 = 1;

	    if (!have2)
		num2 = lastnum;

	    have1 = TRUE;
	    have2 = TRUE;
	}
	if (!have1)
	    num1 = curnum;

	if (!have2)
	    num2 = num1;

	switch (*cp++) {
	case 'a':
	    addlines(num1 + 1);
	    break;

	case 'c':
	    deletelines(num1, num2);
	    addlines(num1);
	    break;

	case 'd':
	    deletelines(num1, num2);
	    break;

	case 'f':
	    if (*cp && !isblank(*cp)) {
		fprintf(stderr, "Bad file command\n");
		break;
	    }
	    while (isblank(*cp))
		cp++;
	    if (*cp == '\0') {
		if (filename)
		    printf("\"%s\"\n", filename);
		else
		    printf("No filename\n");
		break;
	    }
	    cp = strdup(cp);
	    if (cp == NULL) {
		fprintf(stderr, "No memory for filename\n");
		break;
	    }
	    if (filename)
		free(filename);
	    filename = cp;
	    break;

	case 'i':
	    addlines(num1);
	    break;

	case 'k':
	    while (isblank(*cp))
		cp++;

	    if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
		fprintf(stderr, "Bad mark name\n");
		break;
	    }
	    marks[*cp - 'a'] = num2;
	    break;

	case 'l':
	    printlines(num1, num2, TRUE);
	    break;

	case 'p':
	    printlines(num1, num2, FALSE);
	    break;

	case 'q':
	    while (isblank(*cp))
		cp++;
	    if (have1 || *cp) {
		fprintf(stderr, "Bad quit command\n");
		break;
	    }
	    if (!dirty)
		return;

	    printf("Really quit? ");
	    fflush(stdout);

	    buf[0] = '\0';
	    fgets(buf, sizeof(buf), stdin);
	    cp = buf;
	    while (isblank(*cp))
		cp++;
	    if ((*cp == 'y') || (*cp == 'Y'))
		return;
	    break;

	case 'r':
	    if (*cp && !isblank(*cp)) {
		fprintf(stderr, "Bad read command\n");
		break;
	    }
	    while (isblank(*cp))
		cp++;
	    if (*cp == '\0') {
		fprintf(stderr, "No filename\n");
		break;
	    }
	    if (!have1)
		num1 = lastnum;

	    if (readlines(cp, num1 + 1))
		break;

	    if (filename == NULL)
		filename = strdup(cp);
	    break;

	case 's':
	    subcommand(cp, num1, num2);
	    break;

	case 'w':
	    if (*cp && !isblank(*cp)) {
		fprintf(stderr, "Bad write command\n");
		break;
	    }
	    while (isblank(*cp))
		cp++;

	    if (!have1) {
		num1 = 1;
		num2 = lastnum;
	    }
	    if (*cp == '\0')
		cp = filename;
	    if (cp == NULL) {
		fprintf(stderr, "No file name specified\n");
		break;
	    }
	    writelines(cp, num1, num2);
	    break;

	case 'z':
	    switch (*cp) {
	    case '-':
		printlines(curnum - 21, curnum, FALSE);
		break;
	    case '.':
		printlines(curnum - 11, curnum + 10, FALSE);
		break;
	    default:
		printlines(curnum, curnum + 21, FALSE);
		break;
	    }
	    break;

	case '.':
	    if (have1) {
		fprintf(stderr, "No arguments allowed\n");
		break;
	    }
	    printlines(curnum, curnum, FALSE);
	    break;

	case '-':
	    if (setcurnum(curnum - 1))
		printlines(curnum, curnum, FALSE);
	    break;

	case '=':
	    printf("%d\n", num1);
	    break;

	case '\0':
	    if (have1) {
		printlines(num2, num2, FALSE);
		break;
	    }
	    if (setcurnum(curnum + 1))
		printlines(curnum, curnum, FALSE);
	    break;

	default:
	    fprintf(stderr, "Unimplemented command\n");
	    break;
	}
    }
}