OSSP CVS Repository

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

ossp-pkg/rc/rc_proc.c 1.5
/*  OSSP rc - Run-command processor
**  Copyright (c) 2002 Ralf S. Engelschall
**  Copyright (c) 2002 Cable & Wireless Deutschland GmbH
**  Copyright (c) 2002 The OSSP Project <http://www.ossp.org/>
**
**  This file is part of OSSP rc, a portable Run-command processor
**  which can be found at http://www.ossp.org/pkg/lib/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_proc.c: Run-command processor ISO C source file
*/

#include <stdlib.h>     /* Standard system headers  */
#include <fcntl.h>      /* For reading rc files     */
#include <unistd.h>     /* For reading rc files     */
#include <string.h>     /* For string manipulation  */

/* FIXME: Remove */
#include <sys/stat.h>

#include "rc.h"         /* Public interfaces        */
#include "rc_config.h"  /* Option definitions       */


/************************************************
* procNew(void)                                 *
* Construct a processor                         *
************************************************/
rc_proc_t *procNew(void)
{
    rc_proc_t *pNewrc = NULL;

    pNewrc = malloc(sizeof(rc_proc_t));
    pNewrc->m_pAnal = analNew();        /* Construct a configuration analyser */
    pNewrc->m_pScript = scriptNew();    /* Construct a run-command script     */
    analParse(pNewrc->m_pAnal);         /* Preprocess the anal configuration  */

    return(pNewrc);
}

/************************************************
* procReadtmp(rc_proc_t *, const char *)        *
* Open and store a temp file                    *
************************************************/
rc_return_t procReadtmp(rc_proc_t *pRc, const char *szTmpname)
{
    fprintf(stderr, "%s!!!\n", szTmpname);

    return(RC_THROW(RC_OK));
}

/************************************************
* procPopulate(rc_proc_t *)                     *
* Populate the processor with run commands      *
************************************************/
rc_return_t procPopulate(rc_proc_t *pRc)
{
    int nFdrc     = 0;
    int nFdfunc   = 0;
    int nRet      = 0;
    char *szBuf   = NULL;

    char *szFucka = NULL;

    /* FIXME: Do not hardcode location or prefix! */
    szFucka = (char *)malloc(strlen(pRc->m_pAnal->m_szRcs) + 8);
    strcpy(szFucka, "rcfiles/rc.");
    strcat(szFucka, pRc->m_pAnal->m_szRcs);

    szBuf = (char *)calloc(0, RC_READ_BUFSIZE);

    /* Open the rc file unconditionally */
    if ((nFdrc = open(szFucka, O_RDONLY)) == -1) {
/*        RC_THROW(RC_ERR_IO);*/
        TRACE("Problem with procPopulate open(2)");
    }

    /* Open the func file if it belongs to the configuration */
    if (pRc->m_pAnal->m_szFuncs) {
        if ((nFdfunc = open(pRc->m_pAnal->m_szFuncs, O_RDONLY)) == -1) {
    /*        RC_THROW(RC_ERR_IO);*/
        TRACE("Problem with procPopulate open(2)");
        }
        /* Read data from the func file */
        while ((nRet = read(nFdfunc, szBuf, RC_READ_BUFSIZE)) > 0)
            scriptAppend(pRc->m_pScript, szBuf, nRet);
        if (nRet == -1) /* Handle read errors */
            TRACE("Problem with procPopulate read(2)");
    /*        RC_THROW(RC_ERR_IO);*/
    }

    /* Read data from the rc file */
    while ((nRet = read(nFdrc, szBuf, RC_READ_BUFSIZE)) > 0)
        scriptAppend(pRc->m_pScript, szBuf, nRet);
    if (nRet == -1) /* Handle read errors */
        TRACE("Problem with procPopulate read(2)");
/*        RC_THROW(RC_ERR_IO);*/

    /* Filehandle cleanups */
    close(nFdrc);
    close(nFdfunc);

    /* Memory cleanups */
    if (szFucka) {
        free(szFucka);
        szFucka = NULL;
    }
    if (szBuf) {
        free(szBuf);
        szBuf = NULL;
    }

    return(RC_THROW(RC_OK));
}

/************************************************
* procRun(rc_proc_t *)                          *
* Run the processed run-command script          *
************************************************/
rc_return_t procRun(rc_proc_t *pRc)
{
    
    if (configGetval(RC_DBG_VAL)) /* Dump the running config table */
        configDebug();            /* if debug switch is turned on  */

    scriptDump(pRc->m_pScript);

    return(RC_THROW(RC_OK));
}

/************************************************
* procDelete(rc_proc_t *)                       *
* Destruct a processor                          *
************************************************/
rc_return_t procDelete(rc_proc_t *pRc)
{
    scriptDelete(pRc->m_pScript);   /* Destroy the script        */
    analDelete(pRc->m_pAnal);       /* Destroy the analyser      */
    free(pRc);                      /* Free the processor itself */

    return(RC_THROW(RC_OK));
}

CVSTrac 2.0.1