Exemple #1
0
int main(int argc, char **argv)
{
	int opt;
	int len, rc;
	struct sigaction sigact;
	char *rest;
	int oflags = 0;
	int no_tag;

	memset(&sigact, 0, sizeof(sigact));
	sigact.sa_handler = sighandler;

	if (sigaction(SIGALRM, &sigact, NULL) == -1)
		return 1;

	if (sigaction(SIGPIPE, &sigact, NULL) == -1)
		return 1;

	input = stdin;
	output = stdout;
	/* now parsing options with getopt */
	while ((opt = getopt(argc, argv, options)) != EOF) {
		switch (opt) {
		case 'c':
			rc = chdir(optarg);
			if (rc == -1) {
				fprintf(stderr,
					"Can not change dir to %s errno = %d \"%s\"\n",
					optarg, errno, strerror(errno));
				exit(1);
			}
			break;

		case 'q':
			quiet = true;
			break;

		case 'd':
			duperrors = true;
			break;

		case 's':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -s\n");

			oflags |= 1;
			script = true;
			strncpy(server, optarg, MAXSTR);
			break;

		case 'x':
			if ((oflags & 7) != 0)
				show_usage(1,
					   "Can not combine -x and -s/-p/-n\n");

			oflags |= 8;
			script = true;
			input = fopen(optarg, "r");

			if (input == NULL)
				fatal("Could not open %s\n", optarg);
			break;

		case 'n':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -n\n");

			oflags |= 2;
			strncpy(name, optarg, MAXSTR);
			break;

		case 'p':
			if (oflags > 7)
				show_usage(1, "Can not combine -x and -p\n");

			oflags |= 4;
			strncpy(portstr, optarg, MAXSTR);
			port = atoi(optarg);
			break;

		case '?':
		case 'h':
		default:
			/* display the help */
			show_usage(0, "Help\n");
		}
	}

	if (oflags > 0 && oflags < 7)
		show_usage(1, "Must specify -s, -p, and -n together\n");

	if (oflags == 7)
		openserver();

	while (1) {
		len = readln(input, line, MAXSTR * 2);
		if (len < 0) {
			if (script)
				fatal("End of file on input\n");
			else
				break;
		} else {
			struct response resp;

			lno++;
			memset(&resp, 0, sizeof(resp));

			rest = SkipWhite(line, REQUIRES_MORE, "Invalid line");

			/* Skip totally blank line */
			if (rest == NULL)
				continue;

			if (script && !quiet)
				fprintf(stdout, "%s\n", rest);

			/* If line doesn't start with a tag, that's ok */
			no_tag = (!isdigit(*rest) && (*rest != '$'));

			/* Parse request into response structure */
			rest = parse_request(rest, &resp, no_tag);

			if (rest == NULL) {
				resp.r_status = STATUS_ERRNO;
				resp.r_errno = errno;
			} else {
				/* Make sure default status is ok */
				resp.r_status = STATUS_OK;

				if (*rest != '\0' && *rest != '#')
					fprintf_stderr(
					     "Command line not consumed, rest=\"%s\"\n",
					     rest);

				switch (resp.r_cmd) {
				case CMD_OPEN:
					do_open(&resp);
					break;
				case CMD_CLOSE:
					do_close(&resp);
					break;
				case CMD_LOCKW:
					do_lock(&resp);
					break;
				case CMD_LOCK:
					do_lock(&resp);
					break;
				case CMD_UNLOCK:
					do_unlock(&resp);
					break;
				case CMD_TEST:
					do_test(&resp);
					break;
				case CMD_LIST:
					do_list(&resp);
					break;
				case CMD_HOP:
					do_hop(&resp);
					break;
				case CMD_UNHOP:
					do_unhop(&resp);
					break;
				case CMD_SEEK:
					do_seek(&resp);
					break;
				case CMD_READ:
					do_read(&resp);
					break;
				case CMD_WRITE:
					do_write(&resp);
					break;
				case CMD_ALARM:
					do_alarm(&resp);
					break;

				case CMD_HELLO:
				case CMD_COMMENT:
				case CMD_QUIT:
					resp.r_status = STATUS_OK;
					break;

				case NUM_COMMANDS:
					fprintf_stderr("Invalid command %s\n",
						       line);
					continue;
				}
			}

			respond(&resp);

			if (resp.r_cmd == CMD_QUIT)
				exit(0);
		}
	}

	return 0;
}
void process_cli_data()
{
    int ret;
    ret = read(cli.in, buf, RXBUF);
    buf[ret]=0;
    if(buf[ret-1] == '\n')
        buf[ret-1] = 0;
    int done = 0;
    if ( !strncasecmp((char *)buf, "help", 4) )
    {
        print_help();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "fpscan", 6) )
    {
        do_fpscan();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "callscan", 8) )
    {
        do_callscan();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "autorec", 7) )
    {
        do_autorec();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "ppscan", 6) )
    {
        do_ppscan_str(&buf[6]);
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "chan", 4) )
    {
        do_chan(&buf[4]);
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "slot", 4) )
    {
        do_slot(&buf[4]);
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "jam", 3) )
    {
        do_jam();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "ignore", 6) )
    {
        do_ignore_str(&buf[6]);
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "dump", 4) )
    {
        do_dump();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "hop", 3) )
    {
        do_hop();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "audio", 5) )
    {
        do_audio();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "direction", 9) )
    {
        do_direction();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "wav", 3) )
    {
        do_wav();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "ima", 3) )
    {
        do_ima();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "verb", 4) )
    {
        do_verb();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "stop", 4) )
    {
        do_stop();
        done = 1;
    }
    if ( !strncasecmp((char *)buf, "quit", 4) )
        do_quit();

    if(!done)
        LOG("!!! no such command %s\n", buf);

}