コード例 #1
0
ファイル: endian.c プロジェクト: syzdek/dmstools
/// main statement
/// @param[in] argc   number of arguments
/// @param[in] argv   array of arguments
int main(int argc, char * argv[])
{
   int       c;
   int       opt_index;

   // getopt options
   static char   short_opt[] = "hV";
   static struct option long_opt[] =
   {
      {"help",          no_argument, 0, 'h'},
      {"version",       no_argument, 0, 'V'},
      {NULL,            0,           0, 0  }
   };

   while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
   {
      switch(c)
      {
         case -1:	/* no more arguments */
         case 0:	/* long options toggles */
            break;
         case 'h':
            my_usage();
            return(0);
         case 'V':
            my_version();
            return(0);
         case '?':
            fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME);
            return(1);
         default:
            fprintf(stderr, _("%s: unrecognized option `--%c'\n"
                  "Try `%s --help' for more information.\n"
               ),  PROGRAM_NAME, c, PROGRAM_NAME
            );
            return(1);
      };
   };

   // checks for extra arguments
   if (optind != argc)
   {
      fprintf(stderr, _("%s: unknown option `%s'\n"), PROGRAM_NAME, argv[optind]);
      fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME);
      return(1);
   };

   // prints byte order
   if ((is_big_endian()))
      printf("big endian\n");
   else
      printf("little endian\n");

   return(0);
}
コード例 #2
0
ファイル: recurse.c プロジェクト: syzdek/dmstools
int main(int argc, char * argv[])
{
   int        c;
   int        i;
   int        opts;
   int        option_index;
   char    ** list;

   static char   short_options[] = "acfhLqrvV";
   static struct option long_options[] =
   {
      {"help",          no_argument, 0, 'h'},
      {"quiet",         no_argument, 0, 'q'},
      {"silent",        no_argument, 0, 'q'},
      {"verbose",       no_argument, 0, 'v'},
      {"version",       no_argument, 0, 'V'},
      {NULL,            0,           0, 0  }
   };

   opts         = 0;
   option_index = 0;
   list         = NULL;

   while((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1)
   {
      switch(c)
      {
         case -1:       /* no more arguments */
         case 0:        /* long option toggles */
            break;
         case 'a':
            opts |= MY_OPT_HIDDEN;
            break;
         case 'c':
            opts |= MY_OPT_CONTINUE;
            break;
         case 'f':
            opts |= MY_OPT_FORCE;
            break;
         case 'h':
            my_usage();
            return(0);
         case 'L':
            opts |= MY_OPT_LINKS;
            break;
         case 'q':
            opts |= MY_OPT_QUIET;
            break;
         case 'r':
            opts |= MY_OPT_RECURSE;
            break;
         case 'v':
            opts |= MY_OPT_VERBOSE;
            break;
         case 'V':
            my_version();
            return(0);
         case '?':
            fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
            return(1);
         default:
            fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
            fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
            return(1);
      };
   };

   if (argc < (optind+1))
   {
      fprintf(stderr, "%s: missing required arguments\n", PROGRAM_NAME);
      fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
      return(1);
   };

   for(i = optind; i < argc; i++)
      switch (recurse_directory(argv[i], opts, NULL, NULL))
      {
         case -1: return(1);
         case 0:  break;
         default:
            if (!(opts & MY_OPT_CONTINUE))
               return(1);
            break;
      };
   return(0);
}
コード例 #3
0
ファイル: socket-client.c プロジェクト: bindle/tallymark
int main(int argc, char * argv[])
{
   int               c;
   int               opt_index;
   int               err;
   int               s;
   char            * str;
   ssize_t           len;
   int               family;
   int               protocol;
   int               socktype;
   const char      * port;
   struct addrinfo   hints;
   struct addrinfo * hintsp;
   struct addrinfo * res;
   char              buff[1024];
   int               timeout;
   size_t            buff_len;
   char              straddr[INET6_ADDRSTRLEN];
   struct pollfd     fds[1];
   socklen_t         socklen;
   union
   {
      struct sockaddr         sa;
      struct sockaddr_in      sa_in;
      struct sockaddr_in6     sa_in6;
      struct sockaddr_storage sa_storage;
   } addr;

   static char   short_opt[] = "46hm:t:TUV";
   static struct option long_opt[] =
   {
      { "help",          no_argument, 0, 'h'},
      { "version",       no_argument, 0, 'V'},
      { NULL,            0,           0, 0  }
   };

   if ((str = rindex(argv[0], '/')) != NULL)
   {
      str++;
      argv[0] = str;
   };

   protocol = 0;
   family   = PF_UNSPEC;
   port     = "2211";
   buff_len = 0;
   timeout  = 0;

   while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
   {
      switch(c)
      {
         case -1:	/* no more arguments */
         case 0:	/* long options toggles */
         break;

         case '4':
         family = PF_INET;
         break;

         case '6':
         family = PF_INET6;
         break;

         case 'h':
         my_usage();
         return(0);

         case 'm':
         strncpy(buff, optarg, sizeof(buff)-1);
         buff[sizeof(buff)-1] = '\0';
         buff_len = strlen(buff);
         break;

         case 'T':
         protocol = IPPROTO_TCP;
         break;

         case 't':
         timeout = (int)strtol(optarg, NULL, 0);
         break;

         case 'U':
         protocol = IPPROTO_UDP;
         break;

         case 'V':
         my_version();
         return(0);

         case '?':
         fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
         return(1);

         default:
         fprintf(stderr, "%s: unrecognized option `--%c'\n", argv[0], c);
         fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
         return(1);
      };
   };

   // check for hostname from CLI
   if (optind >= argc)
   {
      fprintf(stderr, "%s: missing required argument\n", argv[0]);
      fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
      return(1);
   };

   // check for port from CLI
   if ((optind+2) <= argc)
      port = argv[optind+1];

   // set defaults
   if (timeout == 0)
      timeout = 5000; // timeout is in ms
   if (buff_len == 0)
   {
      strncpy(buff, "Hello World", sizeof(buff)-1);
      buff[sizeof(buff)-1] = '\0';
      buff_len = strlen(buff);
   };

   printf("%s: resolving address ...\n", argv[0]);
   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_flags    = AI_V4MAPPED|AI_ALL;
   hints.ai_family   = family;
   hints.ai_socktype = 0; // 0 for any socket type or SOCK_[STREAM|DGRAM|RAW]
   hints.ai_protocol = protocol; // any protocol (IPPROTO_UDP/IPPROTO_TCP)
   hintsp            = &hints;
   if ((err = getaddrinfo(argv[optind], port, hintsp, &res)) != 0)
   {
      fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err));
      return(1);
   };
   memcpy(&addr, res->ai_addr, res->ai_addrlen);
   socklen  = res->ai_addrlen;
   protocol = res->ai_protocol;
   socktype = res->ai_socktype;
   freeaddrinfo(res);

   // map binary address into human readable form
   if ((err = getnameinfo(&addr.sa, socklen, straddr, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST)) != 0)
   {
      fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err));
      return(1);
   };

   printf("%s: creating socket ...\n", argv[0]);
   if ((s = socket(addr.sa.sa_family, socktype, protocol)) == -1)
   {
      perror("socket()");
      return(1);
   };

   printf("%s: connecting to %s://[%s]:%i ...\n", argv[0], (protocol == IPPROTO_UDP ? "udp" : "tcp"), straddr, ntohs(addr.sa_in6.sin6_port));
   if ((err = connect(s, &addr.sa, socklen)) == -1)
   {
      perror("connect()");
      close(s);
      return(1);
   };

   printf("%s: sending data ...\n", argv[0]);
   printf("%s: >>> %s\n", argv[0], buff);
   if ((len = sendto(s, buff, buff_len, 0, NULL, 0)) == -1)
   {
      perror("sendto()");
      return(1);
   };

   fds[0].fd      = s;
   fds[0].events  = POLLIN;
   printf("%s: waiting for data ...\n", argv[0]);
   if ((err = poll(fds, 1, timeout)) == -1)
   {
      perror("poll()");
      close(s);
      return(1);
   };
   if (err == 0)
   {
      printf("%s: connection timed out ...\n", argv[0]);
      close(s);
      return(1);
   };

   printf("%s: receiving data ...\n", argv[0]);
   if ((len = recvfrom(s, buff, sizeof(buff)-1, 0, NULL, NULL)) == -1)
   {
      perror("recvfrom()");
      close(s);
      return(1);
   };
   buff[len] = '\0';
   printf("%s: <<< %s\n", argv[0], buff);

   close(s);

   return(0);
}
コード例 #4
0
void loop_step(const char * input) {

   char string[65536];
   char buffer[256];

   // read a line

   if (input != NULL) {
      get_local(string,65536,input);
   } else {
      get_stdin(string,65536);
   }

   // parse

   if (false) {

   } else if (string_start_with(string,"debug ")) {

      if (!Searching) {
         parse_debug(string);
      } else {
         ASSERT(false);
      }

   } else if (string_start_with(string,"go ")) {

      if (!Searching && !Delay) {
         init();
         parse_go(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"isready")) {

      if (!Searching && !Delay) {
         init();
      }

      send("readyok"); // no need to wait when searching (fixit SMK)

   } else if (string_equal(string,"ponderhit")) {

      if (Searching) {

         ASSERT(Infinite);

         SearchInput->infinite = false;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;

      } else {

         ASSERT(false);
      }

   } else if (string_start_with(string,"position ")) {

      if (!Searching && !Delay) {
         init();
         parse_position(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"quit")) {

      ASSERT(!Searching); // violated by Arena UI
      ASSERT(!Delay);

      if (Searching && option_get_int("Threads") > 1) {

         // Arena issue

         SearchInfo->stop = true;
         Infinite = false;

         smp_sleep();
      }

      trans_free();
      tb_close();
      egbb_close();
      smp_close();

      exit(EXIT_SUCCESS);

   } else if (string_start_with(string,"setoption ")) {

      if (!Searching && !Delay) {
         parse_setoption(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"stop")) {

      if (Searching) {

         SearchInfo->stop = true;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;
      }

   } else if (string_equal(string,"uci")) {

      ASSERT(!Searching);
      ASSERT(!Delay);

      send("id name Fruit reloaded %s", my_version(buffer));
      send("id author Fabien Letouzey, Ryan Benitez and Daniel Mehrmann");

      option_list();

      send("uciok");

   } else if (string_equal(string,"ucinewgame")) {

      if (!Searching && !Delay && Init) {
         trans_clear();
         pawn_clear();
         material_clear();
      } else {
         ASSERT(false);
      }
   }
}