+#define TALLOC_DEBUG_ENABLE 1
+
+/***** command-line option handling / setup *****/
+
+static void print_version_info(void)
+{
+ printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
+}
+
+/* Strip trailing / from a path name.*/
+static void remove_trailing_slash(char *s) {
+ size_t n;
+
+ n=strlen(s);
+ if(s[n-1]=='/') {
+ s[n-1]='\0';
+ }
+}
+
+/* argp global parameters */
+const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
+
+/* doc strings */
+static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
+static const char arg_doc[]=""; /* string describing arguments, if any */
+
+/* define the options here. Fields are:
+ * { long-name, short-name, variable name, options, help description } */
+static const struct argp_option cmdline_options[] = {
+ { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
+ { "config-validate", 'C', NULL, 0, "Validate configuration files and exit"},
+ { "version", 1, NULL, 0, "Print version information and exit"},
+ { NULL }
+};
+
+/* structure for communicating with option parser */
+struct cmdline_args {
+ int version_requested;
+ int validate_config_and_exit;
+ char *config_dir;
+};
+
+/* parser for individual options - fills in a struct cmdline_args */
+static error_t parse_option(int key, char *arg, struct argp_state *state)
+{
+ /* get a shorthand to the command line argument structure, part of state */
+ struct cmdline_args *arguments=state->input;
+
+ switch (key) {
+ case 'c':
+ if (arg == NULL) {
+ /* somehow we got called without an argument */
+ return ARGP_ERR_UNKNOWN;
+ }
+ arguments->config_dir=arg;
+ break;
+
+ case 1:
+ arguments->version_requested=1;
+ break;
+
+ case 'C':
+ arguments->validate_config_and_exit=1;
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0; /* success */
+}
+
+/* assemble the argp parser */
+static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
+
+
+/***** talloc error handling *****/
+/* called when talloc tries to abort */
+static void tr_abort(const char *reason)
+{
+ tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
+ abort();
+}
+
+#if TALLOC_DEBUG_ENABLE
+static void tr_talloc_log(const char *msg)
+{
+ tr_debug("talloc: %s", msg);
+}
+#endif /* TALLOC_DEBUG_ENABLE */
+
+static void configure_signals(void)
+{
+ sigset_t signals;
+ /* ignore SIGPIPE */
+ sigemptyset(&signals);
+ sigaddset(&signals, SIGPIPE);
+ pthread_sigmask(SIG_BLOCK, &signals, NULL);
+}
+
+int main(int argc, char *argv[])