/Users/maurits/Documents/studie/afstuderen/biosphere/daemon/options.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: feb 14, 2007
00004  * 
00005  * Function:
00006  * Command-line and config file option parsing.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034 
00035 #include "options.h"
00036 #include "node.h"
00037 #include "settings.h"
00038 #include <biosphere.h>
00039 #include <list.h>
00040 
00041 #include <apr_pools.h>
00042 #include <apr_getopt.h>
00043 #include <apr_strings.h>
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 
00048 
00049 /* Global daemon settings and options: */
00050 unsigned        global_listen_port;
00051 unsigned        global_max_connections;
00052 char       *global_conffile =                   NULL;
00053 bs_list    *global_module_names_list =  NULL;
00054 char       *global_modules_dir =                NULL;
00055 bs_list    *global_node_address_list =  NULL;
00056 
00057 
00058 static void add_node_to_global_list(const char *optarg, apr_pool_t *mp)
00059 {
00060         char *addr;
00061         bs_uint16 port = 2915;
00062         unsigned i;
00063         bs_status rv;
00064         
00065         /* Get the address and port: */
00066         addr = apr_pstrdup(mp, optarg);
00067         for (i = 0; i < strlen(optarg); i++) {
00068                 if (optarg[i] == ':') {
00069                         addr = apr_pstrndup(mp, optarg, i);
00070                         if (optarg[i + 1] != '\0') {
00071                                 const char *tmp = optarg + i + 1;
00072                                 port = (bs_uint16) atoi(tmp);
00073                         }
00074                         break;
00075                 }
00076         }
00077         
00078         /* Add this node: */
00079         rv = add_node(addr, port);
00080         if (rv != BS_OK)
00081                 fprintf(stderr, "Unable to add node: %s:%d\n", addr, port);
00082 }
00083 
00084 
00092 static void init_defaults(apr_pool_t *mp)
00093 {
00094         /* Numerical type options: */
00095         global_listen_port =            BIOSPHERED_PORT;
00096         global_max_connections =        BIOSPHERED_MAX_CONNECTIONS;
00097         
00098         /* String type options: */
00099         global_conffile =               apr_pstrdup(mp, BIOSPHERED_CONFFILE);
00100         global_modules_dir =    apr_pstrdup(mp, BIOSPHERED_MODULES_DIR);
00101         
00102         /* Default modules is an empty list: */
00103         global_module_names_list = new_list(mp);
00104         global_node_address_list = new_list(mp);
00105 }
00106 
00107 
00108 bs_status init_options(int argc, const char *argv[], apr_pool_t *mp)
00109 {
00110         int                             optch;
00111     const char     *optarg;
00112         apr_status_t    rv;
00113         apr_getopt_t   *opt;
00114         
00115         static const apr_getopt_option_t opt_option[] = {
00116         /* long-option, short-option, has-arg flag, description */
00117         {"help",                 '?', FALSE, "print help information"},
00118         {"version",              'V', FALSE, "print version information"},
00119         {"max-clients",  'i', TRUE,  "maximum number of clients"},
00120         {"with-config",  'c', TRUE,  "path of config file to use"},
00121         {"with-module",  'm', TRUE,  "path to module to load"},
00122         {"node",         'n', TRUE,  "address of another node (address[:port]?)"},
00123         {"port",                 'p', TRUE,  "the port the daemon listens on"},
00124         {NULL, 0, 0, NULL} /* end (a.k.a. sentinel) */
00125     };
00126     
00127     init_defaults(mp);
00128     rv = apr_getopt_init(&opt, mp, argc, argv);
00129     if (rv != APR_SUCCESS) return BS_ERROR;
00130     
00131     /* parse the all options based on opt_option[] */
00132     while ((rv = apr_getopt_long(opt, opt_option, &optch, &optarg)) == APR_SUCCESS) {
00133         char *tmp;
00134         
00135         switch (optch) {
00136         case '?':
00137             printf("Here comes help information\n");
00138             return 1;
00139             break;
00140         case 'V':
00141             printf(BIOSPHERE_VERSION_STRING "\n");
00142             return 1;
00143             break;
00144         case 'i':
00145             global_max_connections = atoi(optarg);
00146             printf("Incoming connection limit: %d\n", global_max_connections);
00147             break;
00148         case 'c':
00149                 global_conffile = apr_pstrdup(mp, optarg);
00150                 printf("Reading config options from: %s\n", global_conffile);
00151             break;
00152         case 'm':
00153                 tmp = apr_pstrdup(mp, optarg);
00154                 list_append(global_module_names_list, tmp);
00155             printf("Using module: %s\n", tmp);
00156             break;
00157         case 'n':
00158                 add_node_to_global_list(optarg, mp);
00159                 printf("Adding node: %s\n", optarg);
00160                 break;
00161         case 'p':
00162                 global_listen_port = atoi(optarg);
00163                 printf("Incoming connection port: %d\n", global_listen_port);
00164                 break;
00165         default:
00166                 fprintf(stderr, "Bad command-line option provided: -%c %s\n", optch, optarg);
00167                 return BS_ERROR;
00168         }
00169     }
00170     if (rv != APR_EOF) {
00171         fprintf(stderr, "Bad command-line option provided\n");
00172         return BS_ERROR;
00173     }
00174       
00175     return BS_OK;
00176 }

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1