MI_Result climain()

in Unix/cli/cli_c.c [2587:2971]


MI_Result climain(int argc, const MI_Char* argv[])
{
    MI_Application miApplication = MI_APPLICATION_NULL;
    MI_Session miSession = MI_SESSION_NULL;
    MI_Result miResult;
    MI_DestinationOptions _miDestinationOptions = MI_DESTINATIONOPTIONS_NULL;
    MI_DestinationOptions *miDestinationOptions = NULL;
    MI_UserCredentials miUserCredentials = {0};

    Initialize();

    /*Log_OpenStdErr();
    Log_SetLevel(LOG_VERBOSE);*/
    CatchCtrlC();

    // Setup default stderr and stdout streams:
    serr = stderr;
    sout = stdout;

    arg0 = argv[0];

    // const MI_Uint64 CONNECT_TIMEOUT_USEC = 10 * 1000 * 1000;

    // Get the options:
    miResult = GetCommandLineDestDirOption(&argc, argv);
    if (miResult != MI_RESULT_OK)
    {
        return miResult;
    }

    // Get configuration file options:
    miResult = GetConfigFileOptions();
    if (miResult != MI_RESULT_OK)
    {
        return miResult;
    }

    // Get the options:
    miResult = GetCommandLineOptions(&argc, argv);
    if (miResult != MI_RESULT_OK)
    {
        return miResult;
    }

    // Get the options:
    miResult = VerifyCommandLineOptions();
    if (miResult != MI_RESULT_OK)
    {
        return miResult;
    }

    // There must be at least 1 argument left:
    if (argc < 2)
    {
        if (argc >= 1)
        {
            Ftprintf(sout, USAGE, tcs(argv[0]));
            if (opts.help)
                miResult = MI_RESULT_OK;
            else
                miResult = MI_RESULT_INVALID_PARAMETER;
        }
        else
        {
            Ftprintf(sout, USAGE, tcs(MI_T("omicli")));
            miResult = MI_RESULT_INVALID_PARAMETER;
        }
        return miResult;
    }
    if (opts.trace)
    {
        OpenLogFile();
    }

    if (Tcscmp(argv[1], MI_T("enc")) != 0)
    {
        miResult = MI_Application_Initialize(0, NULL, NULL, &miApplication);
        if (miResult != MI_RESULT_OK)
            return miResult;

        miDestinationOptions = &_miDestinationOptions;
        miResult = MI_Application_NewDestinationOptions(&miApplication, miDestinationOptions);
        if (miResult != MI_RESULT_OK)
            goto CleanupApplication;

        if (opts.user)
        {
            if (opts.auth)
            {
                miUserCredentials.authenticationType = opts.auth;
            }
            else
            {
                miUserCredentials.authenticationType = MI_AUTH_TYPE_BASIC;
            }
            if (opts.hostname)
            {
                miUserCredentials.credentials.usernamePassword.domain = opts.hostname;
            }
            else
            {
                miUserCredentials.credentials.usernamePassword.domain = MI_T("localhost");
            }
            miUserCredentials.credentials.usernamePassword.username = opts.user;
            miUserCredentials.credentials.usernamePassword.password = opts.password;

            miResult = MI_DestinationOptions_AddDestinationCredentials(miDestinationOptions, &miUserCredentials);
            if (miResult != MI_RESULT_OK)
            {
                if (miResult == MI_RESULT_INVALID_PARAMETER)
                    err(PAL_T("Invalid authentication type."));

                goto CleanupApplication;
            }
        }

        if (opts.maxEnvSize)
        {
            miResult = MI_DestinationOptions_SetMaxEnvelopeSize(miDestinationOptions, opts.maxEnvSize);
            if (miResult != MI_RESULT_OK)
            {
                goto CleanupApplication;
            }
        }
    
        if (opts.maxElements)
        {
            miResult = MI_DestinationOptions_SetMaxElements(miDestinationOptions, opts.maxElements);
            if (miResult != MI_RESULT_OK)
            {
                goto CleanupApplication;
            }
        }

        if (opts.port)
        {
            miResult = MI_DestinationOptions_SetDestinationPort(miDestinationOptions, opts.port);
            if (miResult != MI_RESULT_OK)
            {
                goto CleanupApplication;
            }
        }

        if (opts.encryption)
        {
            const MI_Char *transport;
            MI_Boolean privacy;

            if (Tcscasecmp(PAL_T("http"),  opts.encryption) == 0 )
            {
                transport = MI_DESTINATIONOPTIONS_TRANSPORT_HTTP;
                privacy = MI_TRUE;
            }
            else if (Tcscasecmp(PAL_T("https"), opts.encryption) == 0 )
            {
                transport = MI_DESTINATIONOPTIONS_TRANSPORT_HTTPS;
                privacy = MI_TRUE;
            }
            else if (Tcscasecmp(PAL_T("none"),  opts.encryption) == 0 )
            {
                transport = MI_DESTINATIONOPTIONS_TRANSPORT_HTTP;
                privacy = MI_FALSE;
            }
            else
            {
                goto CleanupApplication;
            }            

            miResult = MI_DestinationOptions_SetTransport(miDestinationOptions, transport);
            if (miResult != MI_RESULT_OK)
            {
                goto CleanupApplication;
            }
            miResult = MI_DestinationOptions_SetPacketPrivacy(miDestinationOptions, privacy);
            if (miResult != MI_RESULT_OK)
            {
                goto CleanupApplication;
            }
        }

        miResult = MI_Application_NewSession(&miApplication, NULL, opts.hostname, miDestinationOptions, NULL, NULL, &miSession);
        if (miResult != MI_RESULT_OK)
        {
            if (opts.hostname)
            {
                Ftprintf(sout, tcs(MI_T("omicli: Failed to create session to %T\n")), opts.hostname);
            }
            else
            {
                Ftprintf(sout, tcs(MI_T("omicli: Failed to create session to %T\n")), MI_T("local machine"));
            }
            goto CleanupApplication;
        }

        // Remember start time (will calculate total time in PrintSummary()
        s_startTime = CPU_GetTimeStamp();
    }

    if (Tcscmp(argv[1], MI_T("ei")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            if (opts.queryexpr)
            {
                miResult = QueryInstances(&miSession, argc, argv);
                if (miResult != MI_RESULT_OK)
                    goto CleanupSession;
            }
            else
            {
                miResult = EnumerateInstances(&miSession, argc, argv);
                if (miResult != MI_RESULT_OK)
                    goto CleanupSession;
            }
        }
    }
    else if (Tcscmp(argv[1], MI_T("id")) == 0)
    {
        const MI_Char* args[5];
        int i;

        if (argc != 2)
        {
            Ftprintf(serr, PAL_T("Usage: %T id\n\n"), tcs(arg0));
            miResult = MI_RESULT_INVALID_PARAMETER;
            goto CleanupSession;
        }

        args[0]= argv[0];
        args[1]= MI_T("ei");
        args[2]= MI_T("root/omi");
        args[3]= MI_T("OMI_Identify");
        args[4]= NULL;

        for (i = 0; i < opts.repeat; i++)
        {
            miResult = EnumerateInstances(&miSession, 4, args);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("wql")) == 0 ||
             Tcscmp(argv[1], MI_T("cql")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = Query(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("noop")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = NoOp(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("enc")) == 0)
    {
        miResult = Encode(argc, argv);
        goto CleanupApplication;
    }
    else if (Tcscmp(argv[1], MI_T("gi")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = GetInstance(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("ci")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = CreateInstance(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("mi")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = ModifyInstance(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("di")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = DeleteInstance(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("iv")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = Invoke(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("a")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = Associators(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("r")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = References(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("gc")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = GetClass(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else if (Tcscmp(argv[1], MI_T("sub")) == 0)
    {
        int i;
        for (i = 0; i < opts.repeat; i++)
        {
            miResult = Subscribe(&miSession, argc, argv);
            if (miResult != MI_RESULT_OK)
                goto CleanupSession;
        }
    }
    else
    {
        err(PAL_T("unknown command: %T"), tcs(argv[1]));
        miResult = MI_RESULT_INVALID_PARAMETER;
        goto CleanupSession;
    }

    if (opts.summary)
        PrintSummary();

CleanupSession:
    MI_Session_Close(&miSession, NULL, NULL);

CleanupApplication:
    if (miDestinationOptions)
        MI_DestinationOptions_Delete(miDestinationOptions);

    MI_Application_Close(&miApplication);

    if (sout != stdout)
        fclose(sout);

    if (serr != stdout)
        fclose(serr);

    return miResult;
}