Пример #1
0
action parse_command_line(const unsigned char * command_line, int * const num_arg, unsigned char ** const string_arg, const int exec_only_options) {

	int h;
	action a;
	const unsigned char *p = command_line;

	if (num_arg) *num_arg = -1;
	if (string_arg) *string_arg = NULL;

	if (!command_line) return NOP_A;

	if (!*p) return NOP_A;

	while(isasciispace(*p)) p++;

	command_line = p;

	if (!isalpha(*p)) return NOP_A;

	while(*p && !isasciispace(*p)) p++;

	h = hash_cmd(command_line, p - command_line);

	if ((a = hash_table[h]) && !cmdcmp(commands[--a].name, command_line)
		|| (a = short_hash_table[h]) && !cmdcmp(commands[--a].short_name, command_line)) {

		while(isasciispace(*p)) p++;

		if (!(*p && (commands[a].flags & NO_ARGS))) {
			if (!*p || (commands[a].flags & ARG_IS_STRING) || isxdigit(*p) || *p == 'x' || *p =='X') {
				if ((commands[a].flags & IS_OPTION) || !exec_only_options) {
					if (*p) {
						if ((commands[a].flags & ARG_IS_STRING) && string_arg) {
							int len = strlen(p);

							if (len > 1 && *p == '"' && p[len - 1] == '"') {
								p++;
								len -= 2;
							}

							if (len == 0 && !(commands[a].flags & EMPTY_STRING_OK)) return -STRING_IS_EMPTY;

							if (!(*string_arg = malloc(len + 1))) return -OUT_OF_MEMORY;
							memcpy(*string_arg, p, len);
							(*string_arg)[len] = 0;
						}
						else if (num_arg) {
							char *q;
							*num_arg = strtol(p, &q, 0);
							if (*q && !isasciispace(*q)) return -NOT_A_NUMBER;
						}
					}
					return a;
				}
				D(fprintf(stderr,"parse_command error: Can execute only options.\n");)
				return -CAN_EXECUTE_ONLY_OPTIONS;
			}
			D(fprintf(stderr,"parse_command error: Has numeric argument.\n");)
			return -HAS_NUMERIC_ARGUMENT;
		}
Пример #2
0
struct servo *servo_open(const char *device)
{
    /* configure serial port to drive motor */
    char cmd[256] = "";
    strcat(cmd, "stty -F ");
    strcat(cmd, device);
    strcat(cmd, " 9600 ignbrk -icrnl -opost -onlcr -isig -icanon "
           "-iexten -echo -echoe -echok -echoctl -echoke min 1 time 5");
    system(cmd);

    FILE *file;
    if(!(file = fopen(device, "rw"))) {
        printf("failed to open '%s' to control servo\n", device);
        return NULL;
    }

    struct servo *servo = malloc(sizeof *servo);
    if(!servo)
        return NULL;

    servo->motor_count = servo->button_count = servo->buzzer = 0;
    servo->file = file;

    if(fcntl(fileno(servo->file), F_SETFL, O_NONBLOCK) == -1) {
        printf("failed to set '%s' non blocking\n", device);
        exit(1);
    }
    setlinebuf(servo->file);

    fprintf(servo->file, "!GETCAP\r\n");

    /* wait for it */
    struct timespec ts = {0, 1e8};
    nanosleep(&ts, NULL);

    /* read capabilities */
    char s[128], *e, *f;
    while(fgets(s, sizeof s, servo->file))
        if(cmdcmp("!CAP", s, &e))
            while(*e) {
                while(*e == ' ') e++;
                if(cmdcmp("MOTORS=", e, &f))
                    servo->motor_count = strtol(f, &e, 10);
                else if(cmdcmp("BUTTONS=", e, &f))
                    servo->button_count = strtol(f, &e, 10);
                else if(cmdcmp("BUZZER", e, &f))
                    servo->buzzer = 1, e = f;
                while(*e != ' ') e++;
            }

    return servo;
}
Пример #3
0
int exec_cmd(char *arg)
{
      int n;
      cmd_t *c;

      arg = getarg(arg, &n);
      if ( ! arg ) 
      {
	    msg_send(CMD_UNKNOWN);
	    return 0;
      }

      c = cmds;
      while ( c->command )
      {
	    if ( ! cmdcmp(arg, c->command) ) 
	    {
		  if ( c->handler )
		  {
			arg = getarg(arg+n, &n);
			return c->handler(c, arg);
		  }
		  else
		  {
			error("FIXME: no c->handler!\n");
			exit(1);
		  }
	    }
	    c++;
      }

      msg_send(CMD_UNKNOWN);

      return 0;
}
Пример #4
0
int cmd_chat(int cn,char *text)
{
	int n,len;
	char buf[80];

	for (n=0; n<33; n++) {
		if (!cname[n].name) continue;
		
		sprintf(buf,"c%d",n);
		if ((len=cmdcmp(text,buf))) {
			return write_chat(cn,n,text+len);
		}
		if ((len=cmdcmp(text,cname[n].name))) {
			return write_chat(cn,n,text+len);
		}
	}
	return 0;
}
Пример #5
0
const char *servo_read_status(struct servo *servo)
{
    if(!servo)
        return NULL;

    static char laststatus[256] = "";
    static int laststatuspos = 0;
    char *e;

    int c;
    while((c = fgetc(servo->file)) != EOF) {
        laststatus[laststatuspos++] = c;
        if(c == '\n') {
            laststatus[laststatuspos-1] = '\0';
            laststatuspos = 0;
            if(cmdcmp("!STATUS", laststatus, &e)) {
                return e;
            }
        }
    }

    return NULL;
}
Пример #6
0
int main(int argc, char* argv[]) {
	string homedir;
	for (int i = 1; i < argc; i++) {
		if (cmdcmp(argv[i], "-h") == 0) {
			print_usage();
			exit(EXIT_SUCCESS);
		} else if (cmdcmp(argv[i], "-i") == 0) {
			print_license();
			exit(EXIT_SUCCESS);
		} else if (cmdcmp(argv[i], "-homedir") == 0 && i + 1 < argc) {
			homedir = argv[++i];
		} else {
			fprintf(stderr, "Illegal argument \"%s\"\n", argv[i]);
			print_usage();
			exit(EXIT_FAILURE);
		}
	}

	if (!settings_.init(homedir)) {
		fprintf(stderr, "The homedir[%s] is not exist\n", homedir.c_str());
		return -1;
	}
	string reason = settings_.load_conf();
	if (reason != "") {
		fprintf(stderr, "Failed to load configuration file, %s\n", reason.c_str());
		return -1;
	}
	log_init("xixibase_%N.log", 20 * 1024 * 1024);
	set_log_level(settings_.log_level);

	LOG_INFO(VERSION" start.");

	printf_system_info();

	settings_.print();

	try {
#if defined(_WIN32)
		SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
#else
		signal(SIGINT, sigproc);
		signal(SIGHUP, sigproc);
		signal(SIGQUIT, sigproc);
		signal(SIGTERM, sigproc);
		signal(SIGPIPE, sigpipeproc);
#endif

		svr_ = new Server(settings_.pool_size, settings_.num_threads);

		if (svr_->start()) {
			svr_->run();
		}

		LOG_INFO("destory instance");
		delete svr_;
		svr_ = NULL;
	} catch (std::exception& e) {
		LOG_FATAL("Exception: " << e.what());
	}
	LOG_INFO(VERSION" stop.");

	return 0;
}