Exemple #1
0
const Csocket& Csocket::open(int t, bool _blocking)
{
	start_up();
	*this = socket(AF_INET, t, 0);
	if (*this != INVALID_SOCKET && !_blocking && blocking(false))
		close();
	return *this;
}
Exemple #2
0
int main(int argc,const char* argv[]){
	if(argc!=3){
		printf("usage:%s [ip] [port]\n",argv[0]);
		return 0;
	}
	int listen_sock = start_up(argv[1],argv[2]);
	fds[0] = listen_sock;
	init_fds(fds,SIZE);	
	while(1){
		int max_fd = -1;
		fd_set rdfds;
		init_rdfds(fds,SIZE,&rdfds,&max_fd);
		switch(select(max_fd+1,&rdfds,NULL,NULL,NULL)){
			case 0:
				printf("time out!\n");
				break;
			case -1:
				perror("select");
				break;
			default:{
				//遍历文件描述符表,找已就绪的文件描述符
				for(int i=0; i<SIZE; ++i){
					if(i==0 && FD_ISSET(fds[i],&rdfds)){
						//监听套接字已就绪
						struct sockaddr_in client;
						socklen_t size = sizeof(client);
						int client_sock = accept(fds[0],(struct sockaddr *)&client,&size);
						if(client_sock < 0){
							perror("accept");
							break;
						}
						int index = put_fd_in_fds(fds,SIZE,client_sock);
						printf("%s:%d connected, fd in %d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),index);
					}else if(i!=0 && FD_ISSET(fds[i],&rdfds)){
						//其它读事件已就绪(假设数据量小于1024)
						char buff[1024] = {0};
						ssize_t s = read(fds[i],buff,sizeof(buff)-1);
						if(s == 0){
							printf("someone is disconneced!\n");
							fds[i] = -1;
							close(fds[i]);
							break;
						}else if(s < 0){
							perror("read");
							break;
						}
						printf("client#%s",buff);
					}
				}
			}
		}
	}	
	return 0;
}
int DLL InitialiseEncoderJob(
							 EncoderJob &jobSpec,			// Allocated EncoderJob object to fill
							 int Width,
							 int Height,
							 const char* BaseDirectory,		// Output location in filesystem
							 int FrameRate,					// Encode frame rate (must be the same as capture frame rate)
							 int Bitrate,					// Video bitrate
							 double SegmentDuration			// Target duration for file chunks (might get missed)
							 )
{

	fwStaticInit(); // start up FrameWave library.

	jobSpec.IsValid = false;
	int l = strlen(BaseDirectory);
	if (l >= 1024) return 1;
	strcpy(jobSpec.BaseDirectory, BaseDirectory);

	printf("CPUID: %d\n", fwGetCpuType());

	jobSpec.Width = Width;
	jobSpec.Height = Height;

	jobSpec.SegmentNumber = 1;
	jobSpec.FrameRate = FrameRate;

	int saneBitrate = Bitrate;
	if (saneBitrate > 1900000) saneBitrate = 1900000;
	if (saneBitrate < 60000) saneBitrate = 60000;
	jobSpec.Bitrate = saneBitrate;

	jobSpec.SegmentDuration = SegmentDuration;
	jobSpec.FrameCount = 1;
	jobSpec.SplitNextKey = 0;
	jobSpec.a_pts = 0;
	jobSpec.v_pts = 0;

	// Set-up FFmpeg:
	av_register_all(); // Must be called from 32-bit code.
	jobSpec.fmt = av_guess_format("mpegts", NULL, NULL);
	if (!jobSpec.fmt) {
		fprintf(stderr, "Could not find suitable output format\n");
		return 2;
	}

	return start_up(jobSpec);
}
Exemple #4
0
int main(int argc,char **argv)
{
	//服务器socket号
	int server_sock;
	int client_sock;
	//端口号
	int port;
	int client_addr_len;
	struct sockaddr_in client_addr;
	pthread_t thread_id;
	if(argc!=2){
		perror("please run it like: ./tinyweb port");
		exit(EXIT_FAILURE);
	}
	
	//把字符串类型端口号转为整型
	port = atoi(argv[1]);
	
	//初始化服务器socket号
	server_sock=start_up(port);
	client_addr_len=sizeof(client_addr);
	
	//不断循环,不断接收客户端的链接请求
	while(1){
		//accept函数返回值,初始化客户socket号,以后获取客户端的内容和向客户端发送信息都
		//通过这个端口号
		client_sock = accept(server_sock,(struct sockaddr *)&client_addr,&client_addr_len);

		if(client_sock == -1){
			error_die("accept error\n");
		}
		
		//创建新的线程来为客户端服务,主要pthread_create这个函数的参数
		if(pthread_create(&thread_id,NULL,(void *)deal_request,(void *)&client_sock)!=0)
			error_die("pthread_create error\n");
	}
	
	close(server_sock);
	return 0;
}
Exemple #5
0
int start_chess() {
	read_rcfile();
	initialize_zobrist();
	
	Variant = Normal;
	//Variant = Crazyhouse;
	
	memcpy(material, std_material, sizeof(std_material));
	//memcpy(material, zh_material, sizeof(zh_material));
	
	if (!init_book())
		printf("No .OPN opening book found.\n");
	
	lrn_standard = learn_open("standard", "standard");
	lrn_zh	   = learn_open("crazyhouse", "bug");
	lrn_suicide  = learn_open("suicide", "suicide");
	lrn_losers   = learn_open("losers", "losers");
	
	start_up ();
	init_game ();
	
	initialize_hash();
	clear_tt();
	
	init_egtb();
	
	if (init_segtb())
		SEGTB = TRUE_C;
	else
		SEGTB = FALSE_C;
	
	EGTBProbes = 0;
	EGTBHits = 0;
	ECacheProbes = 0;
	ECacheHits = 0;
	TTProbes = 0;
	TTStores = 0;
	TTHits = 0;
	bookidx = 0;
	total_moves = 0;
	ply = 0;
	braindeadinterface = 0;
	moves_to_tc = 40;
	min_per_game = 5;
	time_left = 30000;
	my_rating = opp_rating = 2000;
	maxdepth = 40;
	must_go = 1;
	tradefreely = 1;
	automode = 0;
	
	xb_mode = FALSE_C;
	force_mode = FALSE_C;
	comp_color = 0;
	show_board = TRUE_C;
	is_pondering = FALSE_C;
	allow_pondering = TRUE_C;
	is_analyzing = FALSE_C;
	is_edit_mode = FALSE_C;
	have_partner = FALSE_C;
	must_sit = FALSE_C;
	go_fast = FALSE_C;
	fixed_time = FALSE_C;
	phase = Opening;
	root_to_move = WHITE;
	kibitzed = FALSE_C;
	confirm_moves = FALSE_C;
	
	move_number = 0;
	memset(game_history, 0, sizeof(game_history));
	memset(game_history_x, 0, sizeof(game_history_x));
	
	hash_history[move_number] = hash;
	
	setbuf (stdout, NULL);
	setbuf (stdin, NULL);
	
	depth = 4;
	return 0;
}
Exemple #6
0
int main (int argc, char *argv[]) {

  char input[STR_BUFF], *p, output[STR_BUFF];
  move_s move, comp_move;
  int depth = 4, comp_color;
  bool force_mode, show_board;
  double nps, elapsed;
  clock_t cpu_start = 0, cpu_end = 0;

  parse_cmdline (argc, argv);
  start_up ();
  init_hash_values ();
  init_hash_tables ();
  init_game ();
  init_book ();
  xb_mode = FALSE;
  force_mode = FALSE;
  comp_color = 0;
  show_board = TRUE;
  
  setbuf (stdout, NULL);
  setbuf (stdin, NULL);

  /* keep looping for input, and responding to it: */
  while (TRUE) {

    /* case where it's the computer's turn to move: */
    if (comp_color == white_to_move && !force_mode) {
      nodes = 0;
      qnodes = 0;
      ply = 0;

      start_time = rtime ();
      cpu_start = clock ();
      comp_move = think ();
      cpu_end = clock ();

      /* check for a game end: */
      if (((comp_color == 1 && result != white_is_mated) ||
	  (comp_color == 0 && result != black_is_mated)) &&
	  result != stalemate && result != draw_by_fifty &&
	  result != draw_by_rep) {
	
	comp_to_coord (comp_move, output);

	make (&comp_move, 0);

	/* check to see if we draw by rep/fifty after our move: */
	if (is_draw ()) {
	  result = draw_by_rep;
	}
	else if (fifty > 100) {
	  result = draw_by_fifty;
	}

	reset_piece_square ();
	/* check to see if we mate our opponent with our current move: */
	if (!result) {
	  if (xb_mode) {
	    printf ("move %s\n", output);
	  }
	  else {
	    printf ("\n%s\n", output);
	  }
	}
	else {
	  if (xb_mode) {
	    printf ("move %s\n", output);
	  }
	  else {
	    printf ("\n%s\n", output);
	  }
	  if (result == white_is_mated) {
	    printf ("0-1 {Black Mates}\n");
	  }
	  else if (result == black_is_mated) {
	    printf ("1-0 {White Mates}\n");
	  }
	  else if (result == draw_by_fifty) {
	    printf ("1/2-1/2 {Fifty move rule}\n");
	  }
	  else if (result == draw_by_rep) {
	    printf ("1/2-1/2 {3 fold repetition}\n");
	  }
	  else {
	    printf ("1/2-1/2 {Draw}\n");
	  }
	}
      }
      /* we have been mated or there is a draw: */
      else {
	if (result == white_is_mated) {
	  printf ("0-1 {Black Mates}\n");
	}
	else if (result == black_is_mated) {
	  printf ("1-0 {White Mates}\n");
	}
	else if (result == stalemate) {
	  printf ("1/2-1/2 {Stalemate}\n");
	}
	else if (result == draw_by_fifty) {
	  printf ("1/2-1/2 {Fifty move rule}\n");
	}
	else if (result == draw_by_rep) {
	  printf ("1/2-1/2 {3 fold repetition}\n");
	}
	else {
	  printf ("1/2-1/2 {Draw}\n");
	}
      }

    }

    /* get our input: */
    if (!xb_mode) {
      if (show_board && strcmp (input, "help")) {
	printf ("\n");
	display_board (stdout, 1-comp_color);
      }
      printf ("Faile> ");
      rinput (input, STR_BUFF, stdin);
    }
    else {
      rinput (input, STR_BUFF, stdin);
    }

    /* check to see if we have a move.  If it's legal, play it. */
    if (is_valid_comp (pgn_to_comp (input))) {
      /* good SAN input style move */
      move = pgn_to_comp (input);
      make (&move, 0);
      reset_piece_square ();
      if (show_board) {
	printf ("\n");
	display_board (stdout, 1-comp_color);
      }
    }
    else if (is_move (&input[0])) {
      /* good coordinate style input move */
      if (verify_coord (input, &move)) {
	make (&move, 0);
	reset_piece_square ();
	if (show_board) {
	  printf ("\n");
	  display_board (stdout, 1-comp_color);
	}
      }
      else {
	printf ("Illegal move: %s\n", input);
      }
    }
    else {

      /* make everything lower case for convenience: */
      for (p = input; *p; p++) *p = tolower (*p);

      /* command parsing: */
      if (!strcmp (input, "quit") || !strcmp (input, "exit")) {
	shut_down (EXIT_SUCCESS);
      }
      else if (!strcmp (input, "diagram") || !strcmp (input, "d")) {
	toggle_bool (&show_board);
      }
      else if (!strncmp (input, "perft", 5)) {
	sscanf (input+6, "%d", &depth);
	raw_nodes = 0;
	perft (depth);
	printf ("Raw nodes for depth %d: %ld\n", depth, raw_nodes);
      }
      else if (!strcmp (input, "new")) {
	init_game ();
	/* refresh our hash tables: */
	refresh_hash ();
	force_mode = FALSE;
	comp_color = 0;
      }
      else if (!strcmp (input, "xboard")) {
	xb_mode = TRUE;
	toggle_bool (&show_board);
	signal (SIGINT, SIG_IGN);
	printf ("\n");
      }
      else if (!strcmp (input, "nodes")) {
	printf ("Number of nodes: %li (%0.2f%% qnodes)\n", nodes,
		(float) ((float) qnodes / (float) nodes * 100.0));
      }
      else if (!strcmp (input, "nps")) {
	elapsed = (cpu_end-cpu_start)/(double) CLOCKS_PER_SEC;
	nps = (float) nodes/(float) elapsed;
	if (!elapsed)
	  printf ("NPS: N/A\n");
	else
	  printf ("NPS: %ld\n", (long int) nps);
      }
      else if (!strcmp (input, "post")) {
	toggle_bool (&post);
	if (xb_mode)
	  post = TRUE;
      }
      else if (!strcmp (input, "nopost")) {
	post = FALSE;
      }
      else if (!strcmp (input, "random")) {
	continue;
      }
      else if (!strcmp (input, "hard")) {
	continue;
      }
      else if (!strcmp (input, "easy")) {
	continue;
      }
      else if (!strcmp (input, "?")) {
	continue;
      }
      else if (!strcmp (input, "white")) {
	white_to_move = 1;
	comp_color = 0;
      }
      else if (!strcmp (input, "black")) {
	white_to_move = 0;
	comp_color = 1;
      }
      else if (!strcmp (input, "force")) {
	force_mode = TRUE;
      }
      else if (!strcmp (input, "go")) {
	comp_color = white_to_move;
	force_mode = FALSE;
      }
      else if (!strncmp (input, "time", 4)) {
	sscanf (input+5, "%ld", &time_left);
      }
      else if (!strncmp (input, "otim", 4)) {
	sscanf (input+5, "%ld", &opp_time);
      }
      else if (!strncmp (input, "level", 5)) {
	/* extract the time controls: */
	sscanf (input+6, "%ld %ld %ld", &moves_to_tc, &min_per_game, &inc);
	time_left = min_per_game*6000;
	opp_time = time_left;
	inc *= 100;
      }
      else if (!strncmp (input, "result", 6)) {
	ics_game_end ();
	init_game ();
	force_mode = FALSE;
	comp_color = 0;
      }
      else if (!strcmp (input, "help")) {
	printf ("\n%s\n\n", divider);
	printf ("diagram/d: toggle diagram display\n");
	printf ("exit/quit: terminate Faile\n");
	printf ("go:        make Faile play the side to move\n");
	printf ("new:       start a new game\n");
	printf ("level <x>: the xboard style command to set time\n");
	printf ("  <x> should be in the form: <a> <b> <c> where:\n");
	printf ("  a -> moves to TC (0 if using an ICS style TC)\n");
	printf ("  b -> minutes per game\n");
	printf ("  c -> increment in seconds\n");
	printf ("nodes:     outputs the number of nodes searched\n");
	printf ("nps:       outputs Faile's NPS in search\n");
	printf ("perft <x>: compute raw nodes to depth x\n");
	printf ("post:      toggles thinking output\n");
	printf ("xboard:    put Faile into xboard mode\n");
	printf ("\n%s\n\n", divider);
      }
      else if (!xb_mode) {
	printf ("Illegal move: %s\n", input);
      }

    }

  }

  return 0;

}
Exemple #7
0
int main(int argc, char *argv[]){

    int sockfd, newfd;
    struct sockaddr_storage their_addr; // client's address info
    socklen_t sin_size;
    char s[INET6_ADDRSTRLEN];

    // command line must have port number as argument
    if(argc != 2){
        fprintf(stderr, "Please run program with port number as an argument\n");
        exit(1);
    }
   
    sockfd = start_up(sockfd, argv[1]);

    if(listen(sockfd, 10) == -1){// listen for up to 10 connections
	perror("listen");
    }

    printf("server is listening for connections...\n");

    signal(SIGINT, sig_handler);// checks for SIGINT throughout application
       
    while(1){
        sin_size = sizeof their_addr;
        newfd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size);

        if(newfd == -1){
            perror("accept");
        }

        inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr*)&their_addr), s, sizeof(s));
        printf("server: got connection from %s\n", s);

	if(!fork()){
	    close(sockfd);// child process closes listener
	    
            printf("Enter a handle name 10 characters or less: ");
            scanf("%s", myHandle);
  	
            // make sure handle is 10 characters or less
            while(strlen(myHandle) > 10){

                printf("Handle name must be 10 characters or less- try again: ");
                while((c = getchar()) != '\n' && c != EOF);
                scanf("%s", myHandle);
            }

            strcat(myHandle, "> ");
            printf("Close the connection during chat by typing: \\quit\nPlease wait for a message from the client.\n");
	
	    while(1){

	        //bzero(clientMessage, 500);
	        getMessage(newfd);
	        strcpy(copyHandle, myHandle);
                printf("%s", myHandle);
	        sendMessage(newfd);
 
            }
    	    close(newfd);
	}
        //close(newfd); not sure about needing to close sockets, left as a comment for the future
    }
   // close(sockfd);
    return 0;
}
Exemple #8
0
int main()
{
	int listen_sock=start_up();
    struct sockaddr_in remote;
	socklen_t len=sizeof(remote);

	fd_set _read_set;
	int read_fd=0;
	int write_fd=1;
	int array[MAX_NUM];
	int max_fd=listen_sock;
	int i=0;
	for(;i<MAX_NUM;i++)
	{
		array[i]=-1;
	}
	array[0]=listen_sock;

	while(1)
	{
		FD_ZERO(&_read_set);
		for(i=0;i<MAX_NUM;i++)
		{
			if(array[i]>-1)
			{
				FD_SET(array[ i],&_read_set);
				if(array[i]>max_fd)
				{
					max_fd=array[i];
				}
			}
		}
		switch(select(max_fd+1,&_read_set,NULL,NULL,NULL))
		{
			case 0:
				printf("time  out...");
				break;
			case -1:
				printf("failed...");
				break;
			default:
				for(i=0; i<MAX_NUM;i++)
				{
					if(array[ i]!=-1&&array[i]==listen_sock&&FD_ISSET(array[i],&_read_set))
					{
						int new_sock=accept(array[i],(struct sockaddr*)&remote,&len);
						if(new_sock<0)
						{
							perror("accept");  
							exit(2);
						}
						//get new request
						int j=0;
						for(;j<MAX_NUM;j++)
						{
							if (array[j]==-1) 
							{
							 	array[j]=new_sock;
								if(array[j]>max_fd)
								{
									max_fd=array[j];
								}
								break;
							}
							if(j+1==MAX_NUM)
							{
								printf("buf has full...\n");
								close(new_sock);
							}
						}
					}
					else if(array[i]!=-1&&array[i]>-1&&FD_ISSET(array[i],&_read_set))
					{// get data sock
						char buf[1024];
						memset(buf,'\0',sizeof(buf)-1);
						int len=read(array[i],buf,sizeof(buf)-1);
						if(len<0)
						{
							perror("read");
							exit(3);

						}
						else if(len==0)
						{ 
							printf("client close...\n");
							close(array[i]);
							array[i]=-1;
							break;
						}
						else
						{
							buf[len]='\0';
							printf("client say:%s\n",buf);
						}
					}
				}
		}
	}
	close(listen_sock);
	return 0;
}
int main()
{
	int sock = start_up();
	printf("server socket is: %d\n",sock);
	
	//create fd
	//int epoll_create(int size);
	int epoll_fd = epoll_create(256);
	if(epoll_fd < 0)
	{
		perror("epoll_fd");
		exit(1);
	}
	printf("epoll_fd : %d\n",epoll_fd);

	struct sockaddr_in client;
	socklen_t client_len = sizeof(client);

	struct epoll_event _ev;
	_ev.events = EPOLLIN;
	_ev.data.fd = sock;

	// int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
	if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&_ev) < 0)
	{
		perror("epoll_ctl");
		close(epoll_fd);
	}

	//int timeout = 5000; //5 second
	char buf[1024 * 5];

	struct epoll_event _ev_out[_MAX_NUM_];
	int ready_num = -1;
	
	while(1)
	{
//int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
		switch(ready_num = epoll_wait(epoll_fd,_ev_out,_MAX_NUM_,-1))
		{
			case 0://timeout
				printf("epoll timeout..\n");
				break;
			case -1://error
				perror("epoll_wait");
				break;
			default:
				{
					printf("ready_num : %d\n",ready_num);
					int i = 0;
					for(;i < ready_num;i++)
					{
						int fd = _ev_out[i].data.fd;
						if((fd == sock) && (_ev_out[i].events & EPOLLIN))
						{
							int new_sock = accept(fd,(struct sockaddr*)&client,&client_len);
							if(new_sock < 0)
							{
								perror("accept");
								continue;
							}

							printf("get a connect...\n");
							set_nonblock(new_sock);
							_ev.events = EPOLLIN | EPOLLET;
							_ev.data.fd = new_sock;

							if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,new_sock,&_ev) < 0)
							{
								perror("epoll_ctl");
								close(new_sock);
								continue;
							}
							printf("Insert new_fd success,new_sock is : %d\n",new_sock);
							continue;
						}
						if(_ev_out[i].events & EPOLLIN)
						{
							memset(buf,'\0',sizeof(buf));
							if(read_data(fd,buf,sizeof(buf)) == 0)
							{
								printf("close connect..\n");
								//delete fd
								epoll_ctl(epoll_fd,EPOLL_CTL_DEL,fd,NULL);
							}
							printf("%s\n",buf);
							memset(buf,'\0',sizeof(buf));
							sprintf(buf,"HTTP/1.0 200 OK\r\n\r\n");
							if(send(fd,buf,strlen(buf),0) == -1)
							{
								perror("send");
								exit(1);
							}
							sprintf(buf,"<html><head></head><body><p><h1>hello kkw</h1></p></body></html>\n");
							send(fd,buf,strlen(buf),0);
						}
					}
				}
				break;
		}
	}
	close(sock);
	close(epoll_fd);
	return 0;
}
Exemple #10
0
int main (int argc, char *argv[]) {

  char input[STR_BUFF], *p, output[STR_BUFF];
  char readbuff[STR_BUFF];
  move_s move, comp_move;
  int depth = 4;
  bool force_mode, show_board;
  double nps, elapsed;
  clock_t cpu_start, cpu_end;
  move_s game_history[600];
  move_x game_history_x[600];
  int is_edit_mode, edit_color;
  int confirm_moves;
  int pingnum;
  int braindeadinterface;
  int automode;
  rtime_t xstart_time;

  read_rcfile();
  initialize_zobrist();
 
  Variant = Normal;
  //Variant = Crazyhouse;

  memcpy(material, std_material, sizeof(std_material));
  //memcpy(material, zh_material, sizeof(zh_material));

  if (!init_book())
    printf("No .OPN opening book found.\n");

  lrn_standard = learn_open("standard", "standard");
  lrn_zh	   = learn_open("crazyhouse", "bug");
  lrn_suicide  = learn_open("suicide", "suicide");
  lrn_losers   = learn_open("losers", "losers");

  start_up ();
  init_game ();

  initialize_hash();
  clear_tt();
  reset_ecache();
  
  if (init_segtb())
    SEGTB = TRUE;
  else
    SEGTB = FALSE;
  
  EGTBProbes = 0;
  EGTBHits = 0;
  ECacheProbes = 0;
  ECacheHits = 0;
  TTProbes = 0;
  TTStores = 0;
  TTHits = 0;
  bookidx = 0;
  total_moves = 0;
  ply = 0;
  braindeadinterface = 0;
  moves_to_tc = 40;
  min_per_game = 5;
  time_left = 30000;
  my_rating = opp_rating = 2000;
  maxdepth = 40;
  must_go = 1;
  tradefreely = 1;
  automode = 0;
 
  xb_mode = FALSE;
  force_mode = FALSE;
  comp_color = 0;
  show_board = TRUE;
  is_pondering = FALSE;
  allow_pondering = TRUE;
  is_analyzing = FALSE;
  is_edit_mode = FALSE;
  have_partner = FALSE;
  must_sit = FALSE;
  go_fast = FALSE;
  fixed_time = FALSE;
  phase = Opening;
  root_to_move = WHITE;
  kibitzed = FALSE;
  confirm_moves = FALSE;

  move_number = 0;
  memset(game_history, 0, sizeof(game_history));
  memset(game_history_x, 0, sizeof(game_history_x));

  hash_history[move_number] = hash;
  
  setbuf (stdout, NULL);
  setbuf (stdin, NULL);

  /* keep looping for input, and responding to it: */
  while (TRUE) {

    /* case where it's the computer's turn to move: */
    if (!is_edit_mode && (comp_color == white_to_move || automode) 
	&& !force_mode && !must_sit && !result) {

      /* whatever happens, never allow pondering in normal search */
      is_pondering = FALSE;
  
      cpu_start = clock ();
      comp_move = think ();
      cpu_end = clock();

      ply = 0;

      /* must_sit can be changed by search */
      if (!must_sit || must_go != 0)
	{
	  /* check for a game end: */
	  if ((
	      ((Variant == Losers || Variant == Suicide)
	        && 
	      ((result != white_is_mated) && (result != black_is_mated)))
	      || 
	      ((Variant == Normal || Variant == Crazyhouse || Variant == Bughouse)
	      && ((comp_color == 1 && result != white_is_mated) 
	         ||
	         (comp_color == 0 && result != black_is_mated)
	        ))) 
	      && result != stalemate 
	      && result != draw_by_fifty 
	      && result != draw_by_rep) 
	  {
	    
	    comp_to_coord (comp_move, output);
	   
	    hash_history[move_number] = hash;
	    
	    game_history[move_number] = comp_move;
	    make (&comp_move, 0);
	   
	    /* saves state info */
	    game_history_x[move_number++] = path_x[0];
	    
	    userealholdings = 0;
	    must_go--;
	    
            /* check to see if we draw by rep/fifty after our move: */
	    if (is_draw ()) {
	    	result = draw_by_rep;
	    }
	    else if (fifty > 100) {
	        result = draw_by_fifty;
	    }
	    
	    root_to_move ^= 1;

	    reset_piece_square ();
	    
	    if (book_ply < 40) {
	      if (!book_ply) {
		strcpy(opening_history, output);
	      }
	      else {
		strcat(opening_history, output);
	      }
	    }
	    
	    book_ply++;
	    
	    printf ("\nNodes: %d (%0.2f%% qnodes)\n", nodes,
		    (float) ((float) qnodes / (float) nodes * 100.0));
	    
	    elapsed = (cpu_end-cpu_start)/(double) CLOCKS_PER_SEC;
	    nps = (float) nodes/(float) elapsed;
	    
	    if (!elapsed)
	      printf ("NPS: N/A\n");
	    else
	      printf ("NPS: %d\n", (int32_t) nps);
	    
	    printf("ECacheProbes : %d   ECacheHits : %d   HitRate : %f%%\n", 
		   ECacheProbes, ECacheHits, 
		   ((float)ECacheHits/((float)ECacheProbes+1)) * 100);
	    
	    printf("TTStores : %d TTProbes : %d   TTHits : %d   HitRate : %f%%\n", 
		   TTStores, TTProbes, TTHits, 
		   ((float)TTHits/((float)TTProbes+1)) * 100);
	    
	    printf("NTries : %d  NCuts : %d  CutRate : %f%%  TExt: %d\n", 
		   NTries, NCuts, (((float)NCuts*100)/((float)NTries+1)), TExt);
	    
	    printf("Check extensions: %d  Razor drops : %d  Razor Material : %d\n", ext_check, razor_drop, razor_material);

            printf("EGTB Hits: %d  EGTB Probes: %d  Efficiency: %3.1f%%\n", EGTBHits, EGTBProbes,
		(((float)EGTBHits*100)/(float)(EGTBProbes+1)));
	    
	    printf("Move ordering : %f%%\n", (((float)FHF*100)/(float)(FH+1)));
	    
	    printf("Material score: %d   Eval : %d  White hand: %d  Black hand : %d\n", 
		   Material, (int)eval(), white_hand_eval, black_hand_eval);
	    
	    printf("Hash : %X  HoldHash : %X\n", (unsigned)hash, (unsigned)hold_hash);

	    /* check to see if we mate our opponent with our current move: */
	    if (!result) {
	      if (xb_mode) {

		/* safety in place here */
		if (comp_move.from != dummy.from || comp_move.target != dummy.target)
		    printf ("move %s\n", output);

		if (Variant == Bughouse)
		  {
		    CheckBadFlow(FALSE);
		  }	
	      }
	      else {
		if (comp_move.from != dummy.from || comp_move.target != dummy.target)
		printf ("\n%s\n", output);
      	      }
	    }
	    else {
	      if (xb_mode) {
		if (comp_move.from != dummy.from || comp_move.target != dummy.target)
		    printf ("move %s\n", output);
	      }
	      else {
		if (comp_move.from != dummy.from || comp_move.target != dummy.target)
		printf ("\n%s\n", output);
	      }
	      if (result == white_is_mated) {
		printf ("0-1 {Black Mates}\n");
	      }
	      else if (result == black_is_mated) {
		printf ("1-0 {White Mates}\n");
	      }
	      else if (result == draw_by_fifty) {
	        printf ("1/2-1/2 {Fifty move rule}\n");
	      }
	      else if (result == draw_by_rep) {
	        printf ("1/2-1/2 {3 fold repetition}\n");
	      }
	      else {
		printf ("1/2-1/2 {Draw}\n");
	      }
	      automode = 0;
	    }
	  }
	  /* we have been mated or stalemated: */
	  else {
	    if (result == white_is_mated) {
	      printf ("0-1 {Black Mates}\n");
	    }
	    else if (result == black_is_mated) {
	      printf ("1-0 {White Mates}\n");
	    }
            else if (result == draw_by_fifty) {
	      printf ("1/2-1/2 {Fifty move rule}\n");
	    }
	    else if (result == draw_by_rep) {
	      printf ("1/2-1/2 {3 fold repetition}\n");
	    }
	    else {
	      printf ("1/2-1/2 {Draw}\n");
	    }
	    automode = 0;
	  }
	}
    }

    /* get our input: */
    if (!xb_mode) {
      if (show_board) {
	printf ("\n");
	display_board (stdout, 1-comp_color);
      }
      if (!automode)
      {
      	printf ("Sjeng: ");
      	rinput (input, STR_BUFF, stdin);
      }
    }
    else {
      /* start pondering */

      if ((must_sit || (allow_pondering && !is_edit_mode && !force_mode &&
	      move_number != 0) || is_analyzing) && !result && !automode)
	{
	  is_pondering = TRUE;
	  think();
	  is_pondering = FALSE;

	  ply = 0;
	}
      if (!automode)
      {
      	rinput (input, STR_BUFF, stdin);
      }
    }

    /* check to see if we have a move.  If it's legal, play it. */
    if (!is_edit_mode && is_move (&input[0])) {
      if (verify_coord (input, &move)) {
	
	if (confirm_moves) printf ("Legal move: %s\n", input);

	game_history[move_number] = move;
	hash_history[move_number] = hash;
	
        make (&move, 0);
	game_history_x[move_number++] = path_x[0];
	
	reset_piece_square ();
	
	root_to_move ^= 1;
	
	if (book_ply < 40) {
	  if (!book_ply) {
	    strcpy(opening_history, input);
	  }
	  else {
	    strcat(opening_history, input);
	  }
        }
	
	book_ply++;
	
	if (show_board) {
	  printf ("\n");
	  display_board (stdout, 1-comp_color);
	}
      }
      else {
	printf ("Illegal move: %s\n", input);
	}
    }
    else {

      /* make everything lower case for convenience: */
      /* GCP: except for setboard, which is case sensitive */
      if (!strstr(input, "setboard"))
      	for (p = input; *p; p++) *p = tolower (*p);

      /* command parsing: */
      if (!strcmp (input, "quit") || (!input[0] && feof(stdin))) {
	safe_fclose(lrn_standard);
	safe_fclose(lrn_zh);
	safe_fclose(lrn_suicide);
	safe_fclose(lrn_losers);
	free_hash();
	free_ecache();
	exit (EXIT_SUCCESS);
      }
      else if (!strcmp (input, "exit"))
	{
	  if (is_analyzing)
	    {
	      is_analyzing = FALSE;
	      is_pondering = FALSE;
	      time_for_move = 0;
	    }
	  else
	    {
	      safe_fclose(lrn_standard);
	      safe_fclose(lrn_zh);
	      safe_fclose(lrn_suicide);
	      safe_fclose(lrn_losers);
	      free_hash();
	      free_ecache();
	      exit (EXIT_SUCCESS);
	    }
	}
      else if (!strcmp (input, "diagram") || !strcmp (input, "d")) {
	toggle_bool (&show_board);
      }
      else if (!strncmp (input, "perft", 5)) {
	sscanf (input+6, "%d", &depth);
	raw_nodes = 0;
	xstart_time = rtime();
	perft (depth);
	printf ("Raw nodes for depth %d: %d\n", depth, raw_nodes);
	printf("Time : %.2f\n", (float)rdifftime(rtime(), xstart_time)/100.);
      }
	  else if (!strcmp (input, "confirm_moves")) {
		  confirm_moves = TRUE;
		  printf("Will now confirm moves.\n");
	  }
      else if (!strcmp (input, "new")) {

	if (xb_mode)
	  {
	    printf("tellics set 1 Sjeng " VERSION " (2001-9-28/%s)\n", setcode);
	  }

	if (!is_analyzing)
	{	
	  memcpy(material, std_material, sizeof(std_material));
	  Variant = Normal;

	 // memcpy(material, zh_material, sizeof(zh_material));
	  //Variant = Crazyhouse;
	    
	  init_game ();
	  initialize_hash();

	  if (!braindeadinterface)
	  {
	    clear_tt();
	    init_book();
	    reset_ecache();
	  }
  
	  force_mode = FALSE;
	  must_sit = FALSE;
	  go_fast = FALSE;
	  piecedead = FALSE;
	  partnerdead = FALSE;
	  kibitzed = FALSE;
	  fixed_time = FALSE;

	  root_to_move = WHITE;
  
	  comp_color = 0;
	  move_number = 0;
	  hash_history[move_number] = 0;
	  bookidx = 0;
	  my_rating = opp_rating = 2000;
          must_go = 0;
	  tradefreely = 1;
	  automode = 0;
	  
	  CheckBadFlow(TRUE);
	  ResetHandValue();
	}
	else
	{
	  init_game ();
	  move_number = 0;
	}
	
      }
      else if (!strcmp (input, "xboard")) {
	xb_mode = TRUE;
	toggle_bool (&show_board);
	signal (SIGINT, SIG_IGN);
	printf ("\n");
	
	/* Reset f5 in case we left with partner */
	printf("tellics set f5 1=1\n");
	
	BegForPartner();
      }
      else if (!strcmp (input, "nodes")) {
	printf ("Number of nodes: %d (%0.2f%% qnodes)\n", nodes,
		(float) ((float) qnodes / (float) nodes * 100.0));
      }
      else if (!strcmp (input, "nps")) {
	elapsed = (cpu_end-cpu_start)/(double) CLOCKS_PER_SEC;
	nps = (float) nodes/(float) elapsed;
	if (!elapsed)
	  printf ("NPS: N/A\n");
	else
	  printf ("NPS: %d\n", (int32_t) nps);
      }
      else if (!strcmp (input, "post")) {
	toggle_bool (&post);
	if (xb_mode)
	  post = TRUE;
      }
      else if (!strcmp (input, "nopost")) {
	post = FALSE;
      }
      else if (!strcmp (input, "random")) {
	continue;
      }
      else if (!strcmp (input, "hard")) {

	allow_pondering = TRUE;

	continue;
      }
      else if (!strcmp (input, "easy")) {

	allow_pondering = FALSE;

	continue;
      }
      else if (!strcmp (input, "?")) {
	continue;
      }
      else if (!strcmp (input, "white")) {
	white_to_move = 1;
	root_to_move = WHITE;
	comp_color = 0;
      }
      else if (!strcmp (input, "black")) {
	white_to_move = 0;
	root_to_move = BLACK;
	comp_color = 1;
      }
      else if (!strcmp (input, "force")) {
	force_mode = TRUE;
      }
      else if (!strcmp (input, "eval")) {
	check_phase();
	printf("Eval: %d\n", (int)eval());
      }
      else if (!strcmp (input, "go")) {
	comp_color = white_to_move;
	force_mode = FALSE;
      }
      else if (!strncmp (input, "time", 4)) {
	sscanf (input+5, "%d", &time_left);
      }
      else if (!strncmp (input, "otim", 4)) {
	sscanf (input+5, "%d", &opp_time);
      }
      else if (!strncmp (input, "level", 5)) {
         if (strstr(input+6, ":"))
	 {
	   /* time command with seconds */
	   sscanf (input+6, "%d %d:%d %d", &moves_to_tc, &min_per_game, 
		   &sec_per_game, &inc);
	   time_left = (min_per_game*6000) + (sec_per_game * 100);
	   opp_time = time_left;
	 }
	 else
	   {
	     /* extract the time controls: */
	     sscanf (input+6, "%d %d %d", &moves_to_tc, &min_per_game, &inc);
	     time_left = min_per_game*6000;
	     opp_time = time_left;
	   }
	 fixed_time = FALSE;
	 time_cushion = 0; 
      }
      else if (!strncmp (input, "rating", 6)) {
	sscanf (input+7, "%d %d", &my_rating, &opp_rating);
	if (my_rating == 0) my_rating = 2000;
	if (opp_rating == 0) opp_rating = 2000;
      }
      else if (!strncmp (input, "holding", 7)) {
	ProcessHoldings(input);     
      }
      else if (!strncmp (input, "variant", 7)) {
	if (strstr(input, "normal"))
	  {
	    Variant = Normal;
	    memcpy(material, std_material, sizeof(std_material));
	    init_book();
	  }
	else if (strstr(input, "crazyhouse"))
	  {
	    Variant = Crazyhouse;
	    memcpy(material, zh_material, sizeof(zh_material));
	    init_book();
	  }
	else if (strstr(input, "bughouse"))
	  {
	    Variant = Bughouse;
	    memcpy(material, zh_material, sizeof(zh_material));
	    init_book();
	  }
	else if (strstr(input, "suicide"))
	  {
	    Variant = Suicide;
	    Giveaway = FALSE;
	    memcpy(material, suicide_material, sizeof(suicide_material));
	    init_book();
	  }
	else if (strstr(input, "giveaway"))
	  {
	    Variant = Suicide;
	    Giveaway = TRUE;
	    memcpy(material, suicide_material, sizeof(suicide_material));
	    init_book();
	  }
	else if (strstr(input, "losers"))
	  {
	    Variant = Losers;
	    memcpy(material, losers_material, sizeof(losers_material));
	    init_book();
	  }
	
	initialize_hash();
	clear_tt();
	reset_ecache();

      }
      else if (!strncmp (input, "analyze", 7)) {
	is_analyzing = TRUE;
	is_pondering = TRUE;
	think();
	ply = 0;
      }
      else if (!strncmp (input, "undo", 4)) {
	    printf("Move number : %d\n", move_number);
	if (move_number > 0)
	  {
		  ply = 1;
	    path_x[0] = game_history_x[--move_number];
	    unmake(&game_history[move_number], 0);
	    reset_piece_square();
	    root_to_move ^= 1;
	  }
	result = 0;
      }
      else if (!strncmp (input, "remove", 5)) {
	if (move_number > 1)
	  {
		  ply = 1;
	    path_x[0] = game_history_x[--move_number];
	    unmake(&game_history[move_number], 0);
	    reset_piece_square();

		  ply = 1;
	    path_x[0] = game_history_x[--move_number];
	    unmake(&game_history[move_number], 0);
	    reset_piece_square();
	  }
	result = 0;
      }
      else if (!strncmp (input, "edit", 4)) {
	is_edit_mode = TRUE;
	edit_color = WHITE;
      }
      else if (!strncmp (input, ".", 1) && is_edit_mode) {
	is_edit_mode = FALSE;
	if (wking_loc == 30) white_castled = no_castle;
	if (bking_loc == 114) black_castled = no_castle;
	book_ply = 50;
	ep_square = 0;
	move_number = 0;
	memset(opening_history, 0, sizeof(opening_history));
	clear_tt();
	initialize_hash();
	reset_piece_square();
      }
      else if (is_edit_mode && !strncmp (input, "c", 1)) {
	if (edit_color == WHITE) edit_color = BLACK; else edit_color = WHITE;
	}
      else if (is_edit_mode && !strncmp (input, "#", 1)) {
	reset_board();
	move_number = 0;
      }
      else if (is_edit_mode 
	       && isalpha(input[0]) 
	       && isalpha(input[1]) 
	       && isdigit(input[2])) {
	PutPiece(edit_color, input[0], input[1], input[2]);
      }
      else if (!strncmp (input, "partner", 7)) {
	HandlePartner(input+7);
	}
      else if (!strncmp (input, "$partner", 8)) {
	HandlePartner(input+8);
      }
      else if (!strncmp (input, "ptell", 5)) {
	HandlePtell(input);
      }
      else if (!strncmp (input, "test", 4)) {
	run_epd_testsuite();
      }
      else if (!strncmp (input, "st", 2)) {
	sscanf(input+3, "%d", &fixed_time);
	fixed_time = fixed_time * 100;
      }
      else if (!strncmp (input, "book", 4)) {
	build_book();
      }
      else if (!strncmp (input, "speed",  5)) {
	speed_test();
      }
      else if (!strncmp (input, "result", 6)) {
	if (cfg_booklearn)
	  {
	    if (strstr (input+7, "1-0"))
	      {
		if (comp_color == 1)
		  book_learning(WIN);
		else
		  book_learning(LOSS);
	      }
	    else if (strstr(input+7, "0-1"))
	      {
		if (comp_color == 1)
		  book_learning(LOSS);
		else
		  book_learning(WIN);
	      }
	    else if (strstr(input+7, "1/2-1/2"))
	      {
		book_learning(DRAW);
	      };
	  }
	}
      else if (!strncmp (input, "prove", 5)) {
	printf("\nMax time to search (s): ");
	start_time = rtime();
	rinput(readbuff, STR_BUFF, stdin);
	pn_time = atoi(readbuff) * 100;
	printf("\n");
	proofnumbersearch();      
       }
      else if (!strncmp (input, "ping", 4)) {
	sscanf (input+5, "%d", &pingnum);
	printf("pong %d\n", pingnum);
      }
      else if (!strncmp (input, "fritz", 5)) {
	braindeadinterface = TRUE;
      }
      else if (!strncmp (input, "reset", 5)) {
	
	memcpy(material, std_material, sizeof(std_material));
	Variant = Normal;
	
	init_game ();
	initialize_hash();
	
	clear_tt();
	init_book();
	reset_ecache();       
	
	force_mode = FALSE;
	fixed_time = FALSE;
	
	root_to_move = WHITE;
	
	comp_color = 0;
	move_number = 0;
	bookidx = 0;
	my_rating = opp_rating = 2000;
      }
      else if (!strncmp (input, "setboard", 8)) {
	setup_epd_line(input+9);
      }
      else if (!strncmp (input, "buildegtb", 9)) {
        Variant = Suicide;
	gen_all_tables();
      }
      else if (!strncmp (input, "lookup", 6)) {
	Variant = Suicide;
        printf("Value : %d\n", egtb(white_to_move));
      }
      else if (!strncmp (input, ".", 1)) {
        /* periodic updating and were not searching */
	/* most likely due to proven mate */
	continue;
      }
      else if (!strncmp (input, "sd", 2)) {
	sscanf(input+3, "%d", &maxdepth);
	printf("New max depth set to: %d\n", maxdepth);
	continue;
      }
      else if (!strncmp (input, "auto", 4)) {
	automode = 1;
	continue;
      }
      else if (!strncmp (input, "protover", 8)) {
	printf("feature ping=1 setboard=1 playother=0 san=0 usermove=0 time=1\n");
	printf("feature draw=0 sigint=0 sigterm=0 reuse=1 analyze=1\n");
	printf("feature myname=\"Sjeng " VERSION "\"\n");
	printf("feature variants=\"normal,bughouse,crazyhouse,suicide,giveaway,losers\"\n");
	printf("feature colors=1 ics=0 name=0 pause=0 done=1\n");
	xb_mode = 2;
      }
      else if (!strncmp (input, "accepted", 8)) {
	/* do nothing as of yet */
      }
      else if (!strncmp (input, "rejected", 8)) {
	printf("Interface does not support a required feature...expect trouble.\n");
      }
      else if (!strncmp (input, "warranty", 8)) {
	  printf("\n  BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
		 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n"
		 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
		 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
		 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
		 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n"
		 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n"
		 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
		 "REPAIR OR CORRECTION.\n"
		 "\n");
	  printf("  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
		 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
		 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
		 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
		 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
		 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
		 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
		 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
		 "POSSIBILITY OF SUCH DAMAGES.\n\n");

	}
      else if (!strncmp (input, "distribution", 12)) {
	printf("\n  You may copy and distribute verbatim copies of the Program's\n"
	       "source code as you receive it, in any medium, provided that you\n"
	       "conspicuously and appropriately publish on each copy an appropriate\n"
	       "copyright notice and disclaimer of warranty; keep intact all the\n"
	       "notices that refer to this License and to the absence of any warranty;\n"
	       "and give any other recipients of the Program a copy of this License\n"
	       "along with the Program.\n"
	       "\n"
	       "You may charge a fee for the physical act of transferring a copy, and\n"
	       "you may at your option offer warranty protection in exchange for a fee.\n\n");

	}
      else if (!strcmp (input, "help")) {
	printf ("\n%s\n\n", divider);
	printf ("diagram/d:       toggle diagram display\n");
	printf ("exit/quit:       terminate Sjeng\n");
	printf ("go:              make Sjeng play the side to move\n");
	printf ("new:             start a new game\n");
	printf ("level <x>:       the xboard style command to set time\n");
	printf ("  <x> should be in the form: <a> <b> <c> where:\n");
	printf ("  a -> moves to TC (0 if using an ICS style TC)\n");
	printf ("  b -> minutes per game\n");
	printf ("  c -> increment in seconds\n");
	printf ("nodes:           outputs the number of nodes searched\n");
	printf ("nps:             outputs Sjeng's NPS in search\n");
	printf ("perft <x>:       compute raw nodes to depth x\n");
	printf ("post:            toggles thinking output\n");
	printf ("xboard:          put Sjeng into xboard mode\n");
	printf ("test:            run an EPD testsuite\n");
	printf ("speed:           test movegen and evaluation speed\n");
	printf ("warranty:        show warranty details\n");
	printf ("distribution:    show distribution details\n");
	printf( "proof:           try to prove or disprove the current pos\n");
	printf( "sd <x>:          limit thinking to depth x\n");
	printf( "st <x>:          limit thinking to x centiseconds\n");
	printf( "setboard <FEN>:  set board to a specified FEN string\n");
	printf( "undo:            back up a half move\n");
	printf( "remove:          back up a full move\n");
	printf( "force:           disable computer moving\n");
	printf( "auto:            computer plays both sides\n");
	printf ("\n%s\n\n", divider);
	
        show_board = 0;
      }
      else if (!xb_mode) {
	printf ("Illegal move: %s\n", input);
      }

    }

  }

  return 0;

}