Options.c

From OraWiki

Jump to: navigation, search
#include <options.h>
#include <stdio.h>
#include <unistd.h>


enum boolean {true, false};
static const char *optString = "i:p:d:hst:o:nc:e:b:?";
void display_usage( void );


void initoptions()
/*
 * Set defaults for command line options
 *
 */
{
    /* Initialize globalArgs before we get to work. */
    globalArgs.iniFile = "watchdog.ini";
    globalArgs.oneshot = "generic";
    globalArgs.http_port = 81;
    globalArgs.debug_level = 1;
    globalArgs.run_http = 0;
    globalArgs.run_daemon = 0;
    globalArgs.run_scheduler = 0;
    globalArgs.schedule = 120;
    globalArgs.httpbase = "/etc/watchdog/htdocs";
    globalArgs.cgibase = "/cgi-bin";
    globalArgs.index_file = "index.html";
    globalArgs.error_file = "/error.html";
}


void getoptions(int argc, char *argv[])
{
  enum boolean error=false;
  int opt = 0;

  initoptions();
  opt = getopt( argc, argv, optString );

    /*
     * Get and check all Arguments as required to get a running programm!
     *
     */

    while( opt != -1 ) {
        switch( opt ) {
            case 'b':
                       globalArgs.httpbase=optarg;
                       printf("HTTP directory is %s\n",optarg);
                       break;
            case 'c':
                       globalArgs.cgibase=optarg;
                       printf("CGI subdirectory is %s\n",optarg);
                       break;

            case 'i':
                {
                FILE * in=fopen(optarg,"r");
                if (in)
                {
                globalArgs.iniFile = optarg; /* true */
                printf("Inififile is %s\n",optarg);
                            }
                            else
                            {
                                   puts( "*******************************************************************");
                                   puts( "*** -i violation. No such file.                                 ***");
                                   puts( "*******************************************************************");
                                   exit( EXIT_FAILURE );
                            }

                            }
                break;

            case 'o':
                if (error == false )
                {
                globalArgs.oneshot = optarg; /* true */
                printf("Check only section [%s] of %s\n",optarg, globalArgs.iniFile);
                exit (ONE_SHOT);
                            }
                            else
                            {
                                   puts( "*******************************************************************");
                                   puts( "*** -o violation. It's only allowed to use -o and -i together.  ***");
                                   puts( "*******************************************************************");
                                   exit( EXIT_FAILURE );
                            }
                break;

            case 'p':
                globalArgs.http_port = atoi(optarg);
                printf("Port is %s\n",optarg);
                error=true;  /* Don't use this param with -o! */
                break;

            case 'd':
                globalArgs.debug_level = atoi(optarg);

                if ((globalArgs.debug_level >-1 & globalArgs.debug_level <6) & ! isalpha(optarg[0]) )
                {
                printf("Debug level is %d\n",globalArgs.debug_level);
                error=true;  /* Don't use this param with -o! */
                            }
                            else
                            {
                                   puts( "*******************************************************************");
                                   puts( "*** -d number format violation . [0-5]                          ***");
                                   puts( "*******************************************************************");
                                   exit( EXIT_FAILURE );
                            }

                break;

            case 'h':
                globalArgs.run_http=1;
                printf("Starting HTTP server\n");
                error=true;  /* Don't use this param with -o! */
                break;

            case 's':
                globalArgs.run_scheduler=1;
                printf("Enabling scheduler\n");
                error=true;  /* Don't use this param with -o! */
                break;

            case 't':
                globalArgs.schedule= atoi(optarg);
                if (globalArgs.schedule >59 & globalArgs.schedule <3601 )
                {
                printf("Scheduler set to %d seconds\n", globalArgs.schedule);
                error=true;  /* Don't use this param with -o! */
                            }
                            else
                            {
                                   puts( "*******************************************************************");
                                   puts( "*** -t number format violation . [60-3600]                      ***");
                                   puts( "*******************************************************************");
                                   exit( EXIT_FAILURE );
                }

                break;

            case '?':
                display_usage();
                break;

            default:
                /* You won't actually get here. */
                display_usage();
                break;
        }

        opt = getopt( argc, argv, optString );
    }



}



void display_usage( void )
{
    puts( "*******************************************************************");
    puts( "*** Watchdog - High Availability processor                      ***" );
    puts( "*** (c) 1989-2007 OraForecast.com                               ***" );
    puts( "***                                                             ***" );
    puts( "*** Gerald.Roehrbein@OraForecast.com                            ***" );
    puts( "***                                                             ***" );
    puts( "*** Commandline options                                         ***" );
    puts( "***                                                             ***" );
    puts( "*** -i <inifile> path and name of Watchdog's ini file           ***" );
    puts( "*** -p <number> where Watchdog will offer HTTP protocol.        ***" );
    puts( "*** -d <number> Debuglevel between 0 and 5.                     ***" );
    puts( "*** -h enable HTTP server.                                      ***" );
    puts( "*** -s enable scheduler.                                        ***" );
    puts( "*** -t check interval. Check delay between 50% and 100% of this ***" );
    puts( "*** -n enable daemon mode. Run Watchdog as a background daemon  ***" );
    puts( "*** -o <section> check one section defined in ini file.         ***" );
    puts( "*** -? Help                                                     ***" );
    puts( "***                                                             ***" );
    puts( "*** Watchdog executes parameters by their order.                ***" );
    puts( "*** If you want to start a HTTP server at a different port than ***" );
    puts( "*** you have to set the port first.                             ***" );
    puts( "*** If not the server listens at the default port.              ***" );
    puts( "***                                                             ***" );
        puts( "*******************************************************************" );

    exit( EXIT_FAILURE );
}
Personal tools