OSSP CVS Repository

ossp - ossp-pkg/rc/rc_optimpl.c 1.2
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/rc/rc_optimpl.c 1.2
/*  rc - OSSP Run-command processor
**  Copyright (c) 2002 Cable & Wireless Deutschland GmbH
**  Copyright (c) 2002 The OSSP Project <http://www.ossp.org/>
**  Copyright (c) 2002 Ralf S. Engelschall
**
**  This file is part of OSSP rc, a portable Run-command processor
**  which can be found at http://www.ossp.org/pkg/rc/
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  rc_optimpl.c: Run-command processor ISO C source file
*/

#include "rc.h"
#include "rc_private.h"
#include "rc_option.h"  /* Option methods rely on popt */
#include "rc_const.h"   /* Strings and other constants */

/* Third party headers for libraries linked in         */
#include "popt.h"       /* OSSP popt options library   */


/* Loop through available options */
rc_return_t foptProcess(char cOpt)
{
    switch (cOpt) {
    case 'h':
        break;
    case 'V':
        break;
    case 'i':
        break;
    case 'q':
        break;
    }

    return(RC_THROW(RC_OK));
}

/* Parse command line options */
rc_return_t coptParse(int argc, char *argv[])
{
    ex_t Except;
    int  bCaught = 0;
    int  nBufpos = 0;           /* For tracking options           */
    char cOpt = 0;              /* For argument parsing           */
    char *szCLIBuf = NULL;
    int  nIter = 0;
    popt_context optCon;        /* Context for parsing options    */

    struct popt_option optionsTable[] = {
        /* Long options with short keys but no arguments */
        { RC_USE_NAME, '?', POPT_ARG_NONE,   0, RC_USE_VAL, RC_USE_DESC, NULL },
        { RC_DBG_NAME, 'd', POPT_ARG_NONE,   0, RC_DBG_VAL, RC_DBG_DESC, NULL },
        { RC_VER_NAME, 'V', POPT_ARG_NONE,   0, RC_VER_VAL, RC_VER_DESC, NULL },
        { RC_EVL_NAME, 'e', POPT_ARG_NONE,   0, RC_EVL_VAL, RC_EVL_DESC, NULL },
        { RC_HLP_NAME, 'h', POPT_ARG_NONE,   0, RC_HLP_VAL, RC_HLP_DESC, NULL },
        { RC_INF_NAME, 'i', POPT_ARG_NONE,   0, RC_INF_VAL, RC_INF_DESC, NULL },
        { RC_LBL_NAME, 'l', POPT_ARG_NONE,   0, RC_LBL_VAL, RC_LBL_DESC, NULL },
        { RC_PRN_NAME, 'p', POPT_ARG_NONE,   0, RC_PRN_VAL, RC_PRN_DESC, NULL },
        { RC_SIL_NAME, 's', POPT_ARG_NONE,   0, RC_SIL_VAL, RC_SIL_DESC, NULL },
        { RC_RAW_NAME, 'r', POPT_ARG_NONE,   0, RC_RAW_VAL, RC_RAW_DESC, NULL },
        { RC_VRB_NAME, 'v', POPT_ARG_NONE,   0, RC_VRB_VAL, RC_VRB_DESC, NULL },
        { RC_EXC_NAME, 'x', POPT_ARG_NONE,   0, RC_EXC_VAL, RC_EXC_DESC, NULL },
                                              
        /* Single argument long options with short keys */
        {RC_LOC_NAME, 'L', POPT_ARG_STRING, 0, RC_LOC_VAL, RC_LOC_DESC, "regx"},
        {RC_CNF_NAME, 'c', POPT_ARG_STRING, 0, RC_CNF_VAL, RC_CNF_DESC, "path"},
        {RC_FNC_NAME, 'f', POPT_ARG_STRING, 0, RC_FNC_VAL, RC_FNC_DESC, "path"},
        {RC_QRY_NAME, 'q', POPT_ARG_STRING, 0, RC_QRY_VAL, RC_QRY_DESC, "varx"},
        {RC_TMP_NAME, 't', POPT_ARG_STRING, 0, RC_TMP_VAL, RC_TMP_DESC, "path"},

        /* Single argument long options without short keys */
        { RC_OWN_NAME, 0, POPT_ARG_STRING, 0, RC_OWN_VAL, RC_OWN_DESC, "user" },
        { RC_GRP_NAME, 0, POPT_ARG_STRING, 0, RC_GRP_VAL, RC_GRP_DESC, "group"},
        { RC_MSK_NAME, 0, POPT_ARG_INT,    0, RC_MSK_VAL, RC_MSK_DESC, "umask"},
        { RC_ASS_NAME, 0, POPT_ARG_STRING, 0, RC_ASS_VAL, RC_ASS_DESC, "regx" },
        { RC_DEF_NAME, 0, POPT_ARG_STRING, 0, RC_DEF_VAL, RC_DEF_DESC, "regx" },
        { RC_REF_NAME, 0, POPT_ARG_STRING, 0, RC_REF_VAL, RC_REF_DESC, "regx" },
        { RC_PRM_NAME, 0, POPT_ARG_STRING, 0, RC_PRM_VAL, RC_PRM_DESC, "regx" },
        { RC_TRM_NAME, 0, POPT_ARG_STRING, 0, RC_TRM_VAL, RC_TRM_DESC, "regx" },
        { RC_NCF_NAME, 0, POPT_ARG_STRING, 0, RC_NCF_VAL, RC_NCF_DESC, "name" },
        { RC_CMN_NAME, 0, POPT_ARG_STRING, 0, RC_CMN_VAL, RC_CMN_DESC, "name" },
        { RC_DFL_NAME, 0, POPT_ARG_STRING, 0, RC_DFL_VAL, RC_DFL_DESC, "name" },
        { RC_ERR_NAME, 0, POPT_ARG_STRING, 0, RC_ERR_VAL, RC_ERR_DESC, "name" },

        POPT_AUTOHELP
        { NULL, 0, 0, NULL, 0 }
    };

    optCon = popt_getcontext(NULL, argc, (const char **)argv, optionsTable, 0);
    popt_setotheroptionhelp(optCon, "[options] <rcfile> <sections [args]>");

    if (argc < 2) {
        popt_printusage(optCon, stderr, 0);
        return(RC_THROW(RC_ERR_USE));
    }

    /* Now do options processing */
    while ((cOpt = popt_getnextopt(optCon)) >= 0)
    ex_try {
        foptProcess(cOpt);
    }
    ex_catch(Except) {
        if ((rc_return_t)Except.ex_value != RC_ERR_USE)
            fprintf(stderr, "Nonusage exception: %d\n",\
                (rc_return_t)Except.ex_value);
        bCaught = 1;
        fprintf(stderr, "Class '%s' threw exception %d in %s:%s():%d.\n",\
                (char *)Except.ex_class, *(int *)Except.ex_value,\
                Except.ex_file, Except.ex_func, Except.ex_line);
    }
    if (!bCaught)
        fprintf(stderr, "parseOpts: No exceptions caught.\n");

    szCLIBuf = (char *)popt_getarg(optCon);
    if ((szCLIBuf == NULL)) {
        fprintf(stderr, "Please specify at least one rcfile and section.\n");
        popt_printusage(optCon, stderr, 0);
        return(RC_THROW(RC_ERR_USE));
    }

    if (cOpt < -1) {
        /* An error occurred during option processing */
        fprintf(stderr, "%s: %s\n",
                popt_badoption(optCon, POPT_BADOPTION_NOALIAS),
                popt_strerror(cOpt));
        return(RC_THROW(RC_ERR_INT));
    }

    fprintf(stderr, "\nRun these commands: %s\n", szCLIBuf);
    popt_freecontext(optCon);
    return(RC_THROW(RC_OK));
}

CVSTrac 2.0.1