/* OSSP rc - Run-command processor ** Copyright (c) 2002 Ralf S. Engelschall ** Copyright (c) 2002 Cable & Wireless Deutschland GmbH ** Copyright (c) 2002 The OSSP Project ** ** 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_script.c: Run-command processor ISO C source file */ #include #include #include "rc.h" /************************************************ * scriptNew(void) * * Construct a script * ************************************************/ rc_script_t *scriptNew(void) { rc_script_t *pScript = NULL; pScript = (rc_script_t *)malloc(sizeof(rc_script_t)); *pScript = NULL; return(pScript); } /************************************************ * scriptAppend(rc_script_t *, char *, size_t) * * Append text to a script * ************************************************/ rc_return_t scriptAppend(rc_script_t *pScript, char *szInbuf, size_t Size) { int nResize = 0; void *pvRealloc = NULL; assert(pScript); /* Script parameter must be valid */ if (!szInbuf) { TRACE("Problem with appendScript"); /* return(RC_THROW(RC_ERR_USE));*/ } /* Short circuit in case of dumb noop call */ if (Size == 0) { return(RC_THROW(RC_OK)); } /* Add 2 to end of nResize to ensure that a \0 precedes any strings */ nResize = (*pScript != NULL ? strlen(*pScript) : 0) + Size + 2; /* Don't trust realloc(3) in this case */ if ((pvRealloc = calloc(1, (size_t)nResize)) == NULL) { TRACE("Problem with appendScript"); /* return(RC_THROW(RC_ERR_MEM));*/ } /* Coerce strings into one Script again */ if (*pScript) { strcpy(pvRealloc, *pScript); strncat(pvRealloc, szInbuf, Size); } else strncpy(pvRealloc, szInbuf, Size); /* Cleanup and deallocate memory */ if (*pScript) { free(*pScript); *pScript = NULL; } *pScript = pvRealloc; return(RC_THROW(RC_OK)); } /************************************************ * scriptSection(rc_script_t *, const char *) * * Parse a script for a given section * ************************************************/ char *scriptSection(rc_script_t *pScript, const char *kszSecname) { char *szTempout = NULL; char *szTmpsec = NULL; char *piStart = NULL; char *piEnd = NULL; assert(pScript); /* Check for a valid incoming script */ szTmpsec = malloc(strlen(kszSecname) + sizeof(char)); strcpy(szTmpsec, "%"); strcat(szTmpsec, kszSecname); piStart = strstr(*pScript, szTmpsec); /* Find start of section */ if (!piStart) /* Short circuit if the */ return(NULL); /* section was not found */ piStart = strstr(piStart, "\n") + sizeof(char); /* Wrap to next line */ piEnd = strstr(piStart + sizeof(char), "%"); /* FIXME: Remove hardcoded */ if (piEnd) { szTempout = malloc(piEnd - piStart + sizeof(char)); strncpy(szTempout, piStart, piEnd - piStart); *(szTempout + (piEnd - piStart)) = NULL; /* Terminate outgoing string */ } else /* We are operating on the last section of the file */ szTempout = strdup(piStart); /* FIXME: Can we assume '\0' at EOF? */ return(szTempout); } /************************************************ * scriptDump(rc_script_t *) * * Print a script to standard out * ************************************************/ rc_return_t scriptDump(rc_script_t *pScript) { fprintf(stdout, "\n**************** Dumpskripte ****************\n"); fprintf(stdout, "%s\n", *pScript); fprintf(stdout, "**************** Dumpskripte ****************\n\n"); return(RC_THROW(RC_OK)); } /************************************************ * scriptDelete(rc_script_t *) * * Destruct a script * ************************************************/ rc_return_t scriptDelete(rc_script_t *pScript) { if (*pScript) free(*pScript); else { TRACE("Empty script created, unused, then destroyed"); /* RC_THROW(RC_WRN_NUL);*/ } free(pScript); return(RC_THROW(RC_OK)); }