Exemple #1
0
int main(int argc, char *argv[])
{
	int opt_c;
	char buf[FILENAME_MAX];
	int rc = 1;

	me = strrchr(argv[0], '/');
	if (me == NULL ) {
		me = argv[0];
	} else {
		me++;
	}
	my_log_init(me);

	my_conf = my_conf_create();
	if (!my_conf) {
		goto _MY_ERR_conf_create;
	}

	while ((opt_c = getopt(argc, argv, "C:L:Vh?")) != -1)
		switch (opt_c) {
		case 'C':
			my_conf->cfg_file = optarg;
			break;
		case 'L':
			my_conf->log_file = optarg;
			break;
		case 'P':
			my_conf->pid_file = optarg;
			break;
		case 'V':
			my_show_version();
			rc = 0;
			goto _MY_EXIT;
		case '?':
		case 'h':
			my_show_usage();
			rc = 0;
			goto _MY_EXIT;
		default:
			MY_ERROR("unknow argument '-%c'", (int)opt_c);
			my_show_usage();
			rc = 1;
			goto _MY_EXIT;
	}

	my_conf->log_level = -1;

	if (my_conf->cfg_file == NULL) {
		snprintf(buf, sizeof(buf), "%s/%s.conf", MY_CFG_DIR, me);
		my_conf->cfg_file = strdup(buf);
	}

	if (my_conf_parse(my_conf) != 0) {
		goto _MY_ERR_conf_parse;
	}

	if (my_conf->log_file == NULL) {
#ifdef MY_DEBUGGING
		my_conf->log_file = "stderr";
#else
		my_conf->log_file = "syslog";
#endif
	}

	if (my_conf->log_level < 0) {
#ifdef MY_DEBUGGING
		my_conf->log_level = MY_LOG_DEBUG;
#else
		my_conf->log_level = MY_LOG_NOTICE;
#endif
	}

	if (my_conf->pid_file == NULL) {
		snprintf(buf, sizeof(buf), "%s/%s.pid", MY_RUN_DIR, me);
		my_conf->pid_file = strdup(buf);
	}

	if (my_log_open(my_conf->log_file, my_conf->log_level) != 0) {
		goto _MY_ERR_log_open;
	}

	my_log(MY_LOG_NOTICE, "started");

	my_core = my_core_create();
	if (!my_core) {
		goto _MY_ERR_core_create;
	}

	if (my_core_init(my_core, my_conf) != 0) {
		goto _MY_ERR_core_init;
	}

	signal(SIGINT, my_sig_handler);
	signal(SIGTERM, my_sig_handler);

#ifdef MY_DEBUGGING
	my_conf_dump(my_conf);
	my_core_dump(my_core);
#endif

	my_core_loop(my_core);
	my_core_destroy(my_core);

	my_log(MY_LOG_NOTICE, "ended");

	my_log_close();

	return 0;

_MY_ERR_core_init:
	my_core_destroy(my_core);
_MY_ERR_core_create:
	my_log_close();
_MY_ERR_log_open:
_MY_ERR_conf_parse:
_MY_EXIT:
	my_conf_destroy(my_conf);
_MY_ERR_conf_create:
	return rc;
}
static void parse_option() {

	const char * file_name;
	FILE * file;
	char line[256];
	char * name, * value;

	file_name = option_get_string("OptionFile");

	file = fopen(file_name,"r");
	if (file == NULL)
		my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno));

	// PolyGlot options (assumed first)

	//read the ini file,and store the name/value pairs
	while (true) {

		if (!my_file_read_line(file,line,sizeof(line))) {
			my_fatal("parse_option(): missing [Engine] section\n");
		}

		if (my_string_case_equal(line,"[engine]")) break;

		if (parse_line(line,&name,&value)) option_set(name,value);

	}
	if (option_get_bool("Log")) { my_log_open(option_get_string("LogFile"));}

	while (my_file_read_line(file,line,sizeof(line))) {

		if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line);

		if (parse_line(line,&name,&value)) {
			uci_option_store(name,value);
		}
	}
	fclose(file);
	//read the optional global.ini file
	if(!option_get_bool("NoGlobals")){
		file=fopen("globals.ini","r");
		//override settings,if any
		if(file!=NULL){
			while (true) {
				if (!my_file_read_line(file,line,sizeof(line))) {
					my_fatal("parse_option(): missing [Engine] section\n");
				}

				if (my_string_case_equal(line,"[engine]")) break;

				if (parse_line(line,&name,&value)){
					if(!my_string_case_equal(name,"LogFile"))
						option_set(name,value);
				}
			}

			if (option_get_bool("Log")) {
				my_log_open(option_get_string("LogFile"));
			}
			else my_log_close(); //close it 

			while (my_file_read_line(file,line,sizeof(line))) {

				if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line);

				if (parse_line(line,&name,&value)) {
					uci_option_store(name,value);
				}
			}
			fclose(file);
		}
	}
	my_log("POLYGLOT %s *** START ***\n",Version);
	my_log("POLYGLOT INI file \"%s\"\n",file_name);
	//do the dump:
	engine_open(Engine);
	Init = true; 
	uci_open(Uci,Engine);
	uci_option_t *next;
	init_uci_list(&next);
	while(next!=NULL){
		if(next->var==NULL) break;
		uci_send_option(Uci,next->var,"%s",next->val);
		if(my_string_case_equal(next->var,"MultiPV") && atoi(next->val)>1)  Uci->multipv_mode=true;
		next=next->next;
	}
	uci_send_isready(Uci);

	if (my_string_equal(option_get_string("EngineName"),"<empty>")) {
		option_set("EngineName",Uci->name);
	}
}