/* @(#)92    1.12  src/examples/type_mgr/tm_admin_args.c, examples.src, os2dce21.dss, 960602a.1 1/11/96 10:22:01 */
/*
 * COMPONENT_NAME:  examples.src
 *
 * FUNCTIONS:
 *
 * ORIGINS: 27
 *
 * (C) COPYRIGHT International Business Machines Corp. 1992, 1994
 * All Rights Reserved
 * Licensed Materials - Property of IBM
 *
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 */

/*********************************************************************
 *  File      :  tm_admin_args.c                                     *
 *********************************************************************
 *                                                                   *
 *  Functions :  tm_admin_args()                                     *
 *                                                                   *
 *  Comments  :  Routines that read and interpret command line args. *
 *               Global variables can be set, based on the cmd line  *
 *               args, and then used by other tm_admin modules.      *
 *                                                                   *
 *********************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#ifdef IBMOS2
# include <os2def.h>
#endif
#include <dce/dce_error.h>
#include <dce/rpc.h>
#include "type_mgr.h"
#include "tm_admin_if.h"
#include <util.h>
#include "tm_admin.h"

#define PRINT_USAGE \
    PRINT("\tUsage: %s   [ -l | -s | -a | -d | -v | -z ] \n",argv[0]); \
    PRINT("\t\t\t -o [ object name ] -t [ type_name ]\n");\
    PRINT("\t\t\t -n [ admin process name ]\n");\
    PRINT("\t\t\t -e [ server entry ]\n");\
    PRINT("\t\t\t -t [ type name ]\n");


/*
 *  Global variables that are used throughout the program.  Any variables
 *  defined here should have extern definitions in client.h, so the
 *  global vars can be accessed anywhere in the client program.
 */

char           g_object_ns_directory_path[ENTRY_LEN];
char           g_server_ns_directory_path[ENTRY_LEN];
char           g_server_ns_entry[ENTRY_LEN];
char           g_object_ns_entry[ENTRY_LEN];
char           g_type_entry[ENTRY_LEN];
unsigned32     g_operation ;
boolean32      g_trace;

extern int getopt(int argc, char *argv[], char *opstring);


/*********************************************************************
 *   Function    :  tm_admin_args()                                  *
 *********************************************************************
 *                                                                   *
 *   Description :  Parse the command line arguments, using the      *
 *                  getopt() function.  Sets the appropriate         *
 *                  variables where necessary.  Variables can then   *
 *                  be used in other parts of the program.           *
 *                                                                   *
 *   Returns     :  SUCCESS, or appropriate error code.              *
 *                                                                   *
 *********************************************************************/

unsigned32 admin_args( unsigned32 argc,
                       char       *argv[] )
{
    extern char        *optarg;
    extern unsigned32  optind;
    unsigned32         c;
    unsigned32         error = 0;

    /*
     *  If no command line args, print the command usage.
     */
    if (argc < 2)
    {
        PRINT_USAGE;
        return(++error);
    }

    /*
     *  The namespace directory is pre-defined.
     */

    strcpy( g_object_ns_directory_path, NAME_SERVICE_PATH );
    sprintf( g_server_ns_directory_path, "%s%s",
                NAME_SERVICE_PATH, NAME_SERVICE_TM_SERVER_PATH );

    /*
     * initialize the variables in order to check for semantic errors
     * on the command line.
     */

    g_operation = 0;
    g_trace = FALSE;
    strcpy( g_server_ns_entry, "" );
    strcpy( g_object_ns_entry, "" );
    strcpy( g_type_entry, "" );

    while ((c = getopt( argc, argv, "n:o:e:t:sadlvz" )) != EOF )
    {
        switch (c)
        {
            case 'a': /* add object request */
                if ( g_operation )  error++;
                g_operation = ADD_OBJECT_MODE;
                break;

            case 'd': /* delete object request */
                if ( g_operation )  error++;
                g_operation = DEL_OBJECT_MODE;
                break;

            case 'e': /* server entry name */
                strncpy( g_server_ns_entry,
                         optarg,
                         ENTRY_LEN-1 );
                break;

            case 'l': /* list objects request */
                if ( g_operation )  error++;
                g_operation = LIST_OBJECT_MODE;
                break;

            case 'n': /* tag name */
                strncpy( g_prog_tag, optarg, PROG_TAG_LEN-1 );
                break;

            case 'o': /* object name */
                strncpy( g_object_ns_entry, optarg, ENTRY_LEN-1 );
                break;

            case 's': /* stop server request */
                if ( g_operation )  error++;
                g_operation = STOP_SERVER_MODE;
                break;

            case 't': /* type name */
                /* The type name will be validated on the server side */
                strncpy( g_type_entry, optarg, ENTRY_LEN-1 );
                break;

            case 'v': /* list server request */
                if ( g_operation )  error++;
                g_operation = LIST_SERVER_MODE;
                break;

            case 'z': /* tracing */
                g_trace = TRUE;
                break;

            case ':': /* missing operand */
                error++;
                break;

            case '?':
                PRINT("Option -%c requires an operand!\n", c);
                error++;
                break;

            default:
                PRINT("Invalid command line arguments!\n");
                return(++error);

        } /* end switch */

        if ( error )
        {
            PRINT_USAGE;
            return(error);
        }
    } /* end while */

    if ( g_operation == 0 )
    {
        PRINT("There is no operation specified.[ -a | -d | -s | -l ]!\n");
        PRINT_USAGE;
        error++;
        return(error);
    }

    if ( strcmp( g_server_ns_entry, "" ) == 0 &&
               g_operation != LIST_SERVER_MODE )
    {
        PRINT("The server entry name is required.\n");
        PRINT_USAGE;
        error++;
        return(error);
    }

    if ( g_operation  == ADD_OBJECT_MODE ||
             g_operation  == DEL_OBJECT_MODE )
    {
        if ( strcmp( g_object_ns_entry, "" ) == 0 )
        {
            PRINT("No object name.\n");
            PRINT_USAGE;
            error++;
            return(error);
        }
        if ( strcmp( g_type_entry, "" ) == 0  &&
            g_operation  == ADD_OBJECT_MODE )
        {
            PRINT("No object type.\n");
            PRINT_USAGE;
            error++;
            return(error);
        }
    }

    /* check the combination of the flags */

    return(error);

} /* end admin_args() */

/* EOF tm_admin_args.c */

