diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2005-09-19 10:29:24 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2005-09-19 10:29:24 +0000 |
commit | 3e8ba29e5cf041b9056b7a14f0000a9c1557fed0 (patch) | |
tree | 656ff16e27b6d43408daea9052819385b32b19aa /parse.c | |
parent | e34ca1e7d952906fd2593850e3be47252211649c (diff) | |
download | rsyslog-3e8ba29e5cf041b9056b7a14f0000a9c1557fed0.tar.gz rsyslog-3e8ba29e5cf041b9056b7a14f0000a9c1557fed0.tar.xz rsyslog-3e8ba29e5cf041b9056b7a14f0000a9c1557fed0.zip |
added parsing of property-filter (but not complete property selector line
yet)
Diffstat (limited to 'parse.c')
-rw-r--r-- | parse.c | 133 |
1 files changed, 129 insertions, 4 deletions
@@ -7,6 +7,7 @@ * Rainer Gerhards and Adiscon GmbH. All Rights Reserved. * This code is placed under the GPL. */ +#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <ctype.h> @@ -65,6 +66,43 @@ rsRetVal parsInt(rsParsObj *pThis, int* pInt) return RS_RET_OK; } +/* Skip everything up to a specified character. + * Returns with ParsePointer set BEHIND this character. + * Returns RS_RET_OK if found, RS_RET_NOT_FOUND if not + * found. In that case, the ParsePointer is moved to the + * last character of the string. + * 2005-09-19 rgerhards + */ +rsRetVal parsSkipAfterChar(rsParsObj *pThis, char c) +{ + register char *pC; + rsRetVal iRet; + + rsCHECKVALIDOBJECT(pThis, OIDrsPars); + + pC = rsCStrGetBufBeg(pThis->pCStr); + + while(pThis->iCurrPos < rsCStrLen(pThis->pCStr)) { + if(pC[pThis->iCurrPos] == c) + break; + ++pThis->iCurrPos; + } + + /* delimiter found? */ + if(pC[pThis->iCurrPos] == c) { + if(pThis->iCurrPos+1 < rsCStrLen(pThis->pCStr)) { + iRet = RS_RET_OK; + pThis->iCurrPos++; /* 'eat' delimiter */ + } else { + iRet = RS_RET_FOUND_AT_STRING_END; + } + } else { + iRet = RS_RET_NOT_FOUND; + } + + return iRet; +} + /* Skip whitespace. Often used to trim parsable entries. * Returns with ParsePointer set to first non-whitespace * character (or at end of string). @@ -77,7 +115,7 @@ rsRetVal parsSkipWhitespace(rsParsObj *pThis) pC = rsCStrGetBufBeg(pThis->pCStr); - while(pThis->iCurrPos >= rsCStrLen(pThis->pCStr)) { + while(pThis->iCurrPos < rsCStrLen(pThis->pCStr)) { if(!isspace(*(pC+pThis->iCurrPos))) break; ++pThis->iCurrPos; @@ -109,20 +147,28 @@ rsRetVal parsDelimCStr(rsParsObj *pThis, rsCStrObj **ppCStr, char cDelim, int bT if((pCStr = rsCStrConstruct()) == NULL) return RS_RET_OUT_OF_MEMORY; - pC = rsCStrGetBufBeg(pThis->pCStr) + pThis->iCurrPos; - if(bTrimLeading) parsSkipWhitespace(pThis); - while(pThis->iCurrPos >= rsCStrLen(pThis->pCStr) + pC = rsCStrGetBufBeg(pThis->pCStr) + pThis->iCurrPos; + +printf("after trim leading, len %d - %d, pC: %c, Delim '%c'\n", pThis->iCurrPos, rsCStrLen(pThis->pCStr), + *pC, cDelim); + while(pThis->iCurrPos < rsCStrLen(pThis->pCStr) && *pC != cDelim) { + printf("parse[%d,%d]: %c\n", pThis->iCurrPos, rsCStrLen(pThis->pCStr), *pC); if((iRet = rsCStrAppendChar(pCStr, *pC)) != RS_RET_OK) { RSFREEOBJ(pCStr); return(iRet); } ++pThis->iCurrPos; + ++pC; } + if(*pC == cDelim) { + ++pThis->iCurrPos; /* eat delimiter */ + } + /* We got the string, now take it and see if we need to * remove anything at its end. */ @@ -144,6 +190,85 @@ rsRetVal parsDelimCStr(rsParsObj *pThis, rsCStrObj **ppCStr, char cDelim, int bT return RS_RET_OK; } +/* Parse a quoted string ("-some-data") from the given position. + * Leading whitespace before the first quote is skipped. During + * parsing, escape sequences are detected and converted: + * \\ - backslash character + * \" - quote character + * any other value \<somechar> is reserved for future use. + * + * After return, the parse pointer is paced after the trailing + * quote. + * + * Output: + * ppCStr Pointer to the parsed string - must be freed by caller and + * does NOT include the quotes. + * rgerhards, 2005-09-19 + */ +rsRetVal parsQuotedCStr(rsParsObj *pThis, rsCStrObj **ppCStr) +{ + register char *pC; + rsCStrObj *pCStr; + rsRetVal iRet; + + rsCHECKVALIDOBJECT(pThis, OIDrsPars); + + if((iRet = parsSkipAfterChar(pThis, '"')) != RS_RET_OK) + return iRet; + pC = rsCStrGetBufBeg(pThis->pCStr) + pThis->iCurrPos; + + /* OK, we most probably can obtain a value... */ + if((pCStr = rsCStrConstruct()) == NULL) + return RS_RET_OUT_OF_MEMORY; + +printf("at start of quoted string, len %d - %d, pC: '%c'\n", pThis->iCurrPos, rsCStrLen(pThis->pCStr), + *pC); + while(pThis->iCurrPos < rsCStrLen(pThis->pCStr)) { + printf("parse[%d,%d]: '%c'\n", pThis->iCurrPos, rsCStrLen(pThis->pCStr), *pC); + if(*pC == '"') { + break; /* we are done! */ + } else if(*pC == '\\') { + ++pThis->iCurrPos; + ++pC; + if(pThis->iCurrPos < rsCStrLen(pThis->pCStr)) { + /* in this case, we copy the escaped character + * to the output buffer (but do not rely on this, + * we might later introduce other things, like \007! + */ + if((iRet = rsCStrAppendChar(pCStr, *pC)) != RS_RET_OK) { + RSFREEOBJ(pCStr); + return(iRet); + } + } + } else { /* regular character */ + if((iRet = rsCStrAppendChar(pCStr, *pC)) != RS_RET_OK) { + RSFREEOBJ(pCStr); + return(iRet); + } + } + ++pThis->iCurrPos; + ++pC; + } + + if(*pC == '"') { + ++pThis->iCurrPos; /* 'eat' trailing quote */ + } else { + /* error - improperly quoted string! */ + RSFREEOBJ(pCStr); + return RS_RET_MISSING_TRAIL_QUOTE; + } + + /* We got the string, let's finish it... */ + if((iRet = rsCStrFinish(pCStr)) != RS_RET_OK) { + RSFREEOBJ(pCStr); + return(iRet); + } + + /* done! */ + *ppCStr = pCStr; + return RS_RET_OK; +} + /* * Local variables: * c-indent-level: 8 |