| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-01-20 22:19:09 +0100 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-01-20 22:19:09 +0100 | 
| commit | df3657b2a99ef20da99ac3c6c02f43cc23e70fca (patch) | |
| tree | 86a9e72bbbbaf7296b2d7cd2725a8bc42611a1f3 /src/cli | |
| parent | 0d49fb74eadcf933f696420cd182077927680d26 (diff) | |
Moving towards a server/clients structure.
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/cli.h | 145 | ||||
| -rw-r--r-- | src/cli/parameters.c | 373 | ||||
| -rw-r--r-- | src/cli/parameters.h | 13 | ||||
| -rw-r--r-- | src/cli/parameters_types.h | 71 | 
4 files changed, 56 insertions, 546 deletions
| diff --git a/src/cli/cli.h b/src/cli/cli.h deleted file mode 100644 index 5aec25b..0000000 --- a/src/cli/cli.h +++ /dev/null @@ -1,145 +0,0 @@ -#ifndef _ZoO_CLI_CLI_H_ -#define _ZoO_CLI_CLI_H_ - -#include <stdio.h> - -#include "../pervasive.h" - - -#ifndef ZoO_DEBUG_PROGRAM_FLOW -   #define ZoO_DEBUG_PROGRAM_FLOW   (0 || ZoO_DEBUG_ALL) -#endif - -#ifndef ZoO_DEBUG_CONFIG -   #define ZoO_DEBUG_CONFIG         (0 || ZoO_DEBUG_ALL) -#endif - -#ifndef ZoO_DEBUG_LEARNING -   #define ZoO_DEBUG_LEARNING       (0 || ZoO_DEBUG_ALL) -#endif - -#ifndef ZoO_DEBUG_NETWORK -   #define ZoO_DEBUG_NETWORK  1 -#endif - -#ifndef ZoO_DEBUG_NETWORK -   #define ZoO_DEBUG_NETWORK        (0 || ZoO_DEBUG_ALL) -#endif - -#define ZoO_ENABLE_WARNINGS_OUTPUT              1 -#define ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT        1 -#define ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT    1 -#define ZoO_ENABLE_FATAL_ERROR_OUTPUT           1 - -#ifdef ZoO_ENABLE_ERROR_LOCATION -   #define ZoO_LOCATION "[" __FILE__ "][" ZoO_TO_STRING(__LINE__) "]" -#else -   #define ZoO_LOCATION "" -#endif - -#define ZoO_PRINT_STDERR(symbol, str, ...)\ -   fprintf(stderr, "[" symbol "]" ZoO_LOCATION " " str "\n", __VA_ARGS__); - -/* - * Given that we use preprocessor contants as flags, we can expect the compilers - * to remove the test condition for disabled flags. No need to be shy about - * allowing many debug options. - */ - -#define ZoO_DEBUG(flag, str, ...)\ -   ZoO_ISOLATE\ -   (\ -      if (flag)\ -      {\ -         ZoO_PRINT_STDERR("D", str, __VA_ARGS__);\ -      }\ -   ) - - -#define ZoO_WARNING(str, ...)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_WARNINGS_OUTPUT)\ -      {\ -         ZoO_PRINT_STDERR("W", str, __VA_ARGS__);\ -      }\ -   ) - -#define ZoO_ERROR(str, ...)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ -      {\ -         ZoO_PRINT_STDERR("E", str, __VA_ARGS__);\ -      }\ -   ) - -#define ZoO_PROG_ERROR(str, ...)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ -      {\ -         ZoO_PRINT_STDERR("P", str, __VA_ARGS__);\ -      }\ -   ) - -#define ZoO_FATAL(str, ...)\ -   ZoO_ISOLATE\ -   (\ -     if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ -      {\ -         ZoO_PRINT_STDERR("F", str, __VA_ARGS__);\ -      }\ -   ) - -/* For outputs without dynamic content (static). ******************************/ - -#define ZoO_PRINT_S_STDERR(symbol, str)\ -   fprintf(stderr, "[" symbol "]" ZoO_LOCATION " " str "\n"); - -#define ZoO_S_DEBUG(flag, str)\ -   ZoO_ISOLATE\ -   (\ -      if (flag)\ -      {\ -         ZoO_PRINT_S_STDERR("D", str);\ -      }\ -   ) - -#define ZoO_S_WARNING(str)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_WARNINGS_OUTPUT)\ -      {\ -         ZoO_PRINT_S_STDERR("W", str);\ -      }\ -   ) - -#define ZoO_S_ERROR(str)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ -      {\ -         ZoO_PRINT_S_STDERR("E", str);\ -      }\ -   ) - -#define ZoO_S_PROG_ERROR(str)\ -   ZoO_ISOLATE\ -   (\ -      if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ -      {\ -         ZoO_PRINT_S_STDERR("P", str);\ -      }\ -   ) - -#define ZoO_S_FATAL(str)\ -   ZoO_ISOLATE\ -   (\ -     if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ -      {\ -         ZoO_PRINT_S_STDERR("F", str);\ -      }\ -   ) - -#endif diff --git a/src/cli/parameters.c b/src/cli/parameters.c index 77c33aa..82ac9d1 100644 --- a/src/cli/parameters.c +++ b/src/cli/parameters.c @@ -1,148 +1,38 @@ -#include <stdlib.h>  #include <stdio.h> -#include <string.h> +#include <stdlib.h>  #include <errno.h> -#include "../pervasive.h" - -#include "error.h" +#include "../core/index.h"  #include "parameters.h" -static void load_default_parameters -( -   struct ZoO_parameters param [const restrict static 1] -) -{ -   param->data_filename        = ZoO_DEFAULT_DATA_FILENAME; -   param->new_data_filename    = (char *) NULL; - -   param->irc_server_addr     = ZoO_DEFAULT_IRC_SERVER_ADDR; -   param->irc_server_port     = ZoO_DEFAULT_IRC_SERVER_PORT; -   param->irc_server_channel  = ZoO_DEFAULT_IRC_SERVER_CHANNEL; -   param->irc_username        = ZoO_DEFAULT_IRC_USERNAME; -   param->irc_realname        = ZoO_DEFAULT_IRC_REALNAME; - -   param->reply_rate          = ZoO_DEFAULT_REPLY_RATE; - -   param->aliases_count = 0; -   param->aliases = NULL; -} - -static void print_help (const char exec [const restrict static 1]) -{ -   printf -   ( -      "Usage: %s [option_1 option_2 ...] NICKNAME [ALIAS_1 ALIAS_2 ...] \n" -      "NICKNAME is used as the IRC nickname value.\n" -      "If NICKNAME or any ALIAS is found in an event, the program will reply.\n" -      "\nAvailable options:\n" -      "   [--data-filename | -df] FILENAME\n" -      "      Learn content from FILENAME before connecting.\n" -      "      Default: %s.\n" -      "   [--new-data-filename | -ndf] FILENAME\n" -      "      Store new data learned in FILENAME.\n" -      "      Default: value of the --data-filename param.\n" -      "   [--irc-server-addr | -isa] IRC_SERVER_ADDR\n" -      "      Connect to this server address.\n" -      "      Default: %s.\n" -      "   [--irc-server-port | -isp] IRC_SERVER_PORT\n" -      "      Connect to this server port.\n" -      "      Default: %s.\n" -      "   [--irc-server-channel | -isc] IRC_SERVER_CHANNEL\n" -      "      Connect to this server's channel.\n" -      "      Default: %s.\n" -      "   [--irc-username | -iu] USERNAME\n" -      "      Connect using this as 'username' (shown in WHOIS).\n" -      "      Default: %s.\n" -      "   [--irc-realname | -ir] REALNAME\n" -      "      Connect using this as 'realname' (shown in WHOIS).\n" -      "      Default: %s.\n" -      "   [--reply-rate | -rr] REPLY_RATE\n" -      "      Chance to reply to an event (integer, range [0, 100]).\n" -      "      Default: %d.\n", -      exec, -      ZoO_DEFAULT_DATA_FILENAME, -      ZoO_DEFAULT_IRC_SERVER_ADDR, -      ZoO_DEFAULT_IRC_SERVER_PORT, -      ZoO_DEFAULT_IRC_SERVER_CHANNEL, -      ZoO_DEFAULT_IRC_USERNAME, -      ZoO_DEFAULT_IRC_REALNAME, -      ZoO_DEFAULT_REPLY_RATE -   ); -} - -static int parse_string_arg -( -   const char * restrict dest [const restrict static 1], -   int const i, -   const char * restrict argv [const restrict static 1], -   int const argc -) -{ -   if (i == argc) -   { -      ZoO_FATAL -      ( -         "Missing value for parameter '%s'.", -         /* Safe: i > 1 */ -         argv[i - 1] -      ); - -      return -1; -   } - -   *dest = argv[i]; - -   return 0; -} - -static int parse_integer_arg +static int parse_markov_order  ( -   int dest [const restrict static 1], -   int const i, -   const char * argv [const restrict static 1], -   int const argc, -   int const min_val, -   int const max_val +   struct ZoO_parameters param [const restrict static 1], +   const char argv [const restrict]  )  { -   long int result; -   char * endptr; -   const int old_errno = errno; - -   if (i == argc) -   { -      ZoO_FATAL -      ( -         "Missing value for parameter '%s'.", -         /* Safe: i > 1 */ -         argv[i - 1] -      ); - -      return -1; -   } +	long long int input; +	const int old_errno = errno; -   errno = 0; +	errno = 0; -   result = strtol(argv[i], &endptr, 10); +   input = strtoll(argv, (char **) NULL, 10);     if     (        (errno != 0) -      || ((*endptr) == '\n') -      || (result < min_val) -      || (result > max_val) +		|| (input > (long long int) ZoO_INDEX_MAX) +		|| (input < 1)     )     { -      ZoO_FATAL +		fprintf        ( -         "Invalid or missing value for parameter '%s', accepted range is " -         "[%d, %d] (integer).", -         /* Safe: i > 1 */ -         argv[i - 1], -         min_val, -         max_val +			stderr, +         "[F] Invalid or value for parameter 'MARKOV_ORDER', accepted" +			"range is " +         "[1, %lli] (integer).", +         (long long int) ZoO_INDEX_MAX        );        errno = old_errno; @@ -150,236 +40,45 @@ static int parse_integer_arg        return -1;     } -   *dest = (int) result; +	param->markov_order = (ZoO_index) input;     errno = old_errno;     return 0;  } -int ZoO_parameters_initialize +enum ZoO_invocation_objective ZoO_parameters_initialize  (     struct ZoO_parameters param [const restrict static 1],     int const argc, -   const char * argv [const restrict static argc] +   const char * argv [const static argc]  )  { -   int i; +   param->session = (const char *) NULL; +   param->markov_order = 1; +   param->storage = (const char *) NULL; -   load_default_parameters(param); - -   for (i = 1; i < argc; ++i) +   switch (argc)     { -      if -      ( -         (strcmp(argv[i], "--data-filename") == 0) -         || (strcmp(argv[i], "-df") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->data_filename), -               i, -               argv, -               argc -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--new-data-filename") == 0) -         || (strcmp(argv[i], "-ndf") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->new_data_filename), -               i, -               argv, -               argc -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--irc-server-addr") == 0) -         || (strcmp(argv[i], "-isa") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->irc_server_addr), -               i, -               argv, -               argc -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--irc-server-port") == 0) -         || (strcmp(argv[i], "-isp") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->irc_server_port), -               i, -               argv, -               argc -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--irc-server-channel") == 0) -         || (strcmp(argv[i], "-isc") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->irc_server_channel), -               i, -               argv, -               argc -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--irc-username") == 0) -         || (strcmp(argv[i], "-iu") == 0) -      ) -      { -         i += 1; +      case 4: +         param->session = argv[1]; +         param->storage = argv[3]; -         if -         ( -            parse_string_arg -            ( -               &(param->irc_username), -               i, -               argv, -               argc -            ) < 0 -         ) +         if (parse_markov_order(param, argv[2]) < 0)           { -            return -1; +            return ZoO_PRINTS_HELP;           } -      } -      else if -      ( -         (strcmp(argv[i], "--irc-realname") == 0) -         || (strcmp(argv[i], "-in") == 0) -      ) -      { -         i += 1; - -         if -         ( -            parse_string_arg -            ( -               &(param->irc_realname), -               i, -               argv, -               argc -            ) < 0 -         ) +         else           { -            return -1; +            return ZoO_RUNS;           } -      } -      else if -      ( -         (strcmp(argv[i], "--reply-rate") == 0) -         || (strcmp(argv[i], "-rr") == 0) -      ) -      { -         i += 1; -         if -         ( -            parse_integer_arg -            ( -               &(param->reply_rate), -               i, -               argv, -               argc, -               0, -               100 -            ) < 0 -         ) -         { -            return -1; -         } -      } -      else if -      ( -         (strcmp(argv[i], "--help") == 0) -         || (strcmp(argv[i], "-h") == 0) -      ) -      { -         print_help(argv[0]); +      case 3: +         param->session = argv[1]; -         return 0; -      } -      else -      { -         break; -      } -   } +			return ZoO_CLEANS_UP; -   if (i == argc) -   { -      ZoO_S_FATAL("Missing argument: NICKNAME"); - -      print_help(argv[0]); - -      return -1; +      default: +         return ZoO_PRINTS_HELP;     } - -   param->aliases_count = (argc - i); -   param->aliases = (argv + i); - -   if (param->new_data_filename == (char *) NULL) -   { -      param->new_data_filename = param->data_filename; -   } - -   return 1;  } diff --git a/src/cli/parameters.h b/src/cli/parameters.h index 1011e2b..7927ef7 100644 --- a/src/cli/parameters.h +++ b/src/cli/parameters.h @@ -1,11 +1,16 @@ -#ifndef _ZoO_IO_PARAMETERS_H_ -#define _ZoO_IO_PARAMETERS_H_ +#ifndef _ZoO_CLI_PARAMETERS_H_ +#define _ZoO_CLI_PARAMETERS_H_  #include "parameters_types.h" -int ZoO_parameters_initialize +char * ZoO_parameters_get_session_name  ( -   struct ZoO_parameters param [const static 1], +   const struct ZoO_parameters param [const restrict static 1] +); + +enum ZoO_invocation_objective ZoO_parameters_initialize +( +   struct ZoO_parameters param [const restrict static 1],     int const argc,     const char * argv [const static argc]  ); diff --git a/src/cli/parameters_types.h b/src/cli/parameters_types.h index 15b5254..f9ca88b 100644 --- a/src/cli/parameters_types.h +++ b/src/cli/parameters_types.h @@ -1,67 +1,18 @@ -#ifndef _ZoO_IO_PARAMETERS_TYPES_H_ -#define _ZoO_IO_PARAMETERS_TYPES_H_ +#ifndef _ZoO_CLI_PARAMETERS_TYPES_H_ +#define _ZoO_CLI_PARAMETERS_TYPES_H_ -#include "../pervasive.h" - -/******************************************************************************/ -/** DEFAULT VALUES ************************************************************/ -/******************************************************************************/ - -#ifndef ZoO_DEFAULT_DATA_FILENAME -   #define ZoO_DEFAULT_DATA_FILENAME      "./memory.txt" -#endif - -#ifndef ZoO_DEFAULT_IRC_SERVER_ADDR -   #define ZoO_DEFAULT_IRC_SERVER_ADDR    "irc.foonetic.net" -#endif - -#ifndef ZoO_DEFAULT_IRC_SERVER_PORT -   #define ZoO_DEFAULT_IRC_SERVER_PORT    "6667" -#endif - -#ifndef ZoO_DEFAULT_IRC_SERVER_CHANNEL -   #define ZoO_DEFAULT_IRC_SERVER_CHANNEL "#theborghivemind" -#endif - -#ifndef ZoO_DEFAULT_IRC_USERNAME -   #define ZoO_DEFAULT_IRC_USERNAME       "zeroofone" -#endif - -#ifndef ZoO_DEFAULT_IRC_REALNAME -   #define ZoO_DEFAULT_IRC_REALNAME       "Zero of One (bot)" -#endif - -#ifndef ZoO_DEFAULT_REPLY_RATE -   #define ZoO_DEFAULT_REPLY_RATE         8 -#endif - -/******************************************************************************/ -/** DEBUG LEVELS **************************************************************/ -/******************************************************************************/ - -#ifndef ZoO_DEBUG_PARAMETERS -   #define ZoO_DEBUG_PARAMETERS (0 || ZoO_DEBUG_ALL) -#endif - -/******************************************************************************/ -/** FUNCTIONS *****************************************************************/ -/******************************************************************************/ +enum ZoO_invocation_objective +{ +   ZoO_PRINTS_HELP, +   ZoO_CLEANS_UP, +   ZoO_RUNS +};  struct ZoO_parameters  { -   const char * restrict data_filename; -   const char * restrict new_data_filename; - -   const char * restrict irc_server_addr; -   const char * restrict irc_server_port; -   const char * restrict irc_server_channel; -   const char * restrict irc_username; -   const char * restrict irc_realname; - -   int reply_rate; - -   int aliases_count; -   const char * restrict * restrict aliases; +   const char * restrict session; +   ZoO_index markov_order; +   const char * restrict storage;  };  #endif | 


