openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/runtime/stream.c
rsRetVal strmReadMultiLine(strm_t* pThis, cstr_t** ppCStr, regex_t* start_preg,
                           regex_t* end_preg, const sbool bEscapeLF,
                           const sbool discardTruncatedMsg,
                           const sbool msgDiscardingError,
                           int64* const strtOffs)
{
    uchar c;
    uchar finished = 0;
    cstr_t* thisLine = NULL;
    rsRetVal readCharRet;
    const time_t tCurr = pThis->readTimeout ? getTime(NULL) : 0;
    int maxMsgSize = glblGetMaxLine();
    DEFiRet;

    do
    {
        CHKiRet(strmReadChar(pThis, &c)); /* immediately exit on EOF */
        pThis->lastRead = tCurr;
        CHKiRet(cstrConstruct(&thisLine));
        /* append previous message to current message if necessary */
        if (pThis->prevLineSegment != NULL)
        {
            CHKiRet(cstrAppendCStr(thisLine, pThis->prevLineSegment));
            cstrDestruct(&pThis->prevLineSegment);
        }

        while (c != '\n')
        {
            CHKiRet(cstrAppendChar(thisLine, c));
            readCharRet = strmReadChar(pThis, &c);
            if (readCharRet == RS_RET_EOF)
            { /* end of file reached without \n? */
                CHKiRet(
                    rsCStrConstructFromCStr(&pThis->prevLineSegment, thisLine));
            }
            CHKiRet(readCharRet);
        }
        cstrFinalize(thisLine);

        /* we have a line, now let's assemble the message */
        const int isStartMatch =
            start_preg
                ? !regexec(start_preg, (char*)rsCStrGetSzStrNoNULL(thisLine), 0,
                           NULL, 0)
                : 0;
        const int isEndMatch =
            end_preg ? !regexec(end_preg, (char*)rsCStrGetSzStrNoNULL(thisLine),
                                0, NULL, 0)
                     : 0;

        if (isStartMatch)
        {
            /* in this case, the *previous* message is complete and we are
             * at the start of a new one.
             */
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment != NULL)
                {
                    /* may be NULL in initial poll! */
                    finished = 1;
                    *ppCStr = pThis->prevMsgSegment;
                }
            }
            CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment, thisLine));
            pThis->ignoringMsg = 0;
        }
        else
        {
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment == NULL)
                {
                    /* may be NULL in initial poll or after timeout! */
                    CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment,
                                                    thisLine));
                }
                else
                {
                    if (bEscapeLF)
                    {
                        rsCStrAppendStrWithLen(pThis->prevMsgSegment,
                                               (uchar*)"\\n", 2);
                    }
                    else
                    {
                        cstrAppendChar(pThis->prevMsgSegment, '\n');
                    }

                    int currLineLen = cstrLen(thisLine);
                    if (currLineLen > 0)
                    {
                        int len;
                        if ((len = cstrLen(pThis->prevMsgSegment) +
                                   currLineLen) < maxMsgSize)
                        {
                            CHKiRet(cstrAppendCStr(pThis->prevMsgSegment,
                                                   thisLine));
                            /* we could do this faster, but for now keep it
                             * simple */
                        }
                        else
                        {
                            len = currLineLen - (len - maxMsgSize);
                            for (int z = 0; z < len; z++)
                            {
                                cstrAppendChar(pThis->prevMsgSegment,
                                               thisLine->pBuf[z]);
                            }
                            finished = 1;
                            *ppCStr = pThis->prevMsgSegment;
                            CHKiRet(rsCStrConstructFromszStr(
                                &pThis->prevMsgSegment, thisLine->pBuf + len));
                            if (discardTruncatedMsg == 1)
                            {
                                pThis->ignoringMsg = 1;
                            }
                            if (msgDiscardingError == 1)
                            {
                                if (discardTruncatedMsg == 1)
                                {
                                    LogError(
                                        0, RS_RET_ERR,
                                        "imfile error: message received is "
                                        "larger than max msg size; "
                                        "rest of message will not be "
                                        "processed");
                                }
                                else
                                {
                                    LogError(
                                        0, RS_RET_ERR,
                                        "imfile error: message received is "
                                        "larger than max msg size; message "
                                        "will be split and processed as "
                                        "another message");
                                }
                            }
                        }
                    }
                }
            }
        }
        if (isEndMatch)
        {
            /* in this case, the *current* message is complete and we are
             * at the end of it.
             */
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment != NULL)
                {
                    finished = 1;
                    *ppCStr = pThis->prevMsgSegment;
                    pThis->prevMsgSegment = NULL;
                }
            }
            pThis->ignoringMsg = 0;
        }
        cstrDestruct(&thisLine);
    } while (finished == 0);

finalize_it:
    *strtOffs = pThis->strtOffs;
    if (thisLine != NULL)
    {
        cstrDestruct(&thisLine);
    }
    if (iRet == RS_RET_OK)
    {
        pThis->strtOffs = pThis->iCurrOffs; /* we are at begin of next line */
        cstrFinalize(*ppCStr);
    }
    else
    {
        if (pThis->readTimeout && (pThis->prevMsgSegment != NULL) &&
            (tCurr > pThis->lastRead + pThis->readTimeout))
        {
            if (rsCStrConstructFromCStr(ppCStr, pThis->prevMsgSegment) ==
                RS_RET_OK)
            {
                cstrFinalize(*ppCStr);
                cstrDestruct(&pThis->prevMsgSegment);
                pThis->lastRead = tCurr;
                pThis->strtOffs =
                    pThis->iCurrOffs; /* we are at begin of next line */
                dbgprintf("stream: generated msg based on timeout: %s\n",
                          cstrGetSzStrNoNULL(*ppCStr));
                iRet = RS_RET_OK;
            }
        }
    }
    RETiRet;
}
/* read a multi-line message from a strm file.
 * The multi-line message is terminated based on the user-provided
 * startRegex or endRegex (Posix ERE). For performance reasons, the regex
 * must already have been compiled by the user.
 * added 2015-05-12 rgerhards
 */
read a multi-line message from a strm file.
The multi-line message is terminated based on the user-provided
startRegex or endRegex (Posix ERE). For performance reasons, the regex
must already have been compiled by the user.
read a multi - line message from a strm file . The multi - line message is terminated based on the user - provided startRegex or endRegex ( Posix ERE ) . For performance reasons the regex must already have been compiled by the user .
rsRetVal strmReadMultiLine(strm_t* pThis, cstr_t** ppCStr, regex_t* start_preg,
                           regex_t* end_preg, const sbool bEscapeLF,
                           const sbool discardTruncatedMsg,
                           const sbool msgDiscardingError,
                           int64* const strtOffs)
{
    uchar c;
    uchar finished = 0;
    cstr_t* thisLine = NULL;
    rsRetVal readCharRet;
    const time_t tCurr = pThis->readTimeout ? getTime(NULL) : 0;
    int maxMsgSize = glblGetMaxLine();
    DEFiRet;
    do
    {
        CHKiRet(strmReadChar(pThis, &c)); 
        pThis->lastRead = tCurr;
        CHKiRet(cstrConstruct(&thisLine));
        if (pThis->prevLineSegment != NULL)
        {
            CHKiRet(cstrAppendCStr(thisLine, pThis->prevLineSegment));
            cstrDestruct(&pThis->prevLineSegment);
        }
        while (c != '\n')
        {
            CHKiRet(cstrAppendChar(thisLine, c));
            readCharRet = strmReadChar(pThis, &c);
            if (readCharRet == RS_RET_EOF)
            { 
                CHKiRet(
                    rsCStrConstructFromCStr(&pThis->prevLineSegment, thisLine));
            }
            CHKiRet(readCharRet);
        }
        cstrFinalize(thisLine);
        const int isStartMatch =
            start_preg
                ? !regexec(start_preg, (char*)rsCStrGetSzStrNoNULL(thisLine), 0,
                           NULL, 0)
                : 0;
        const int isEndMatch =
            end_preg ? !regexec(end_preg, (char*)rsCStrGetSzStrNoNULL(thisLine),
                                0, NULL, 0)
                     : 0;
        if (isStartMatch)
        {
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment != NULL)
                {
                    finished = 1;
                    *ppCStr = pThis->prevMsgSegment;
                }
            }
            CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment, thisLine));
            pThis->ignoringMsg = 0;
        }
        else
        {
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment == NULL)
                {
                    CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment,
                                                    thisLine));
                }
                else
                {
                    if (bEscapeLF)
                    {
                        rsCStrAppendStrWithLen(pThis->prevMsgSegment,
                                               (uchar*)"\\n", 2);
                    }
                    else
                    {
                        cstrAppendChar(pThis->prevMsgSegment, '\n');
                    }
                    int currLineLen = cstrLen(thisLine);
                    if (currLineLen > 0)
                    {
                        int len;
                        if ((len = cstrLen(pThis->prevMsgSegment) +
                                   currLineLen) < maxMsgSize)
                        {
                            CHKiRet(cstrAppendCStr(pThis->prevMsgSegment,
                                                   thisLine));
                        }
                        else
                        {
                            len = currLineLen - (len - maxMsgSize);
                            for (int z = 0; z < len; z++)
                            {
                                cstrAppendChar(pThis->prevMsgSegment,
                                               thisLine->pBuf[z]);
                            }
                            finished = 1;
                            *ppCStr = pThis->prevMsgSegment;
                            CHKiRet(rsCStrConstructFromszStr(
                                &pThis->prevMsgSegment, thisLine->pBuf + len));
                            if (discardTruncatedMsg == 1)
                            {
                                pThis->ignoringMsg = 1;
                            }
                            if (msgDiscardingError == 1)
                            {
                                if (discardTruncatedMsg == 1)
                                {
                                    LogError(
                                        0, RS_RET_ERR,
                                        "imfile error: message received is "
                                        "larger than max msg size; "
                                        "rest of message will not be "
                                        "processed");
                                }
                                else
                                {
                                    LogError(
                                        0, RS_RET_ERR,
                                        "imfile error: message received is "
                                        "larger than max msg size; message "
                                        "will be split and processed as "
                                        "another message");
                                }
                            }
                        }
                    }
                }
            }
        }
        if (isEndMatch)
        {
            if (pThis->ignoringMsg == 0)
            {
                if (pThis->prevMsgSegment != NULL)
                {
                    finished = 1;
                    *ppCStr = pThis->prevMsgSegment;
                    pThis->prevMsgSegment = NULL;
                }
            }
            pThis->ignoringMsg = 0;
        }
        cstrDestruct(&thisLine);
    } while (finished == 0);
finalize_it:
    *strtOffs = pThis->strtOffs;
    if (thisLine != NULL)
    {
        cstrDestruct(&thisLine);
    }
    if (iRet == RS_RET_OK)
    {
        pThis->strtOffs = pThis->iCurrOffs; 
        cstrFinalize(*ppCStr);
    }
    else
    {
        if (pThis->readTimeout && (pThis->prevMsgSegment != NULL) &&
            (tCurr > pThis->lastRead + pThis->readTimeout))
        {
            if (rsCStrConstructFromCStr(ppCStr, pThis->prevMsgSegment) ==
                RS_RET_OK)
            {
                cstrFinalize(*ppCStr);
                cstrDestruct(&pThis->prevMsgSegment);
                pThis->lastRead = tCurr;
                pThis->strtOffs =
                    pThis->iCurrOffs; 
                dbgprintf("stream: generated msg based on timeout: %s\n",
                          cstrGetSzStrNoNULL(*ppCStr));
                iRet = RS_RET_OK;
            }
        }
    }
    RETiRet;
}
rsRetVal strmReadMultiLine ( strm_t * pThis , cstr_t * * ppCStr , regex_t * start_preg , regex_t * end_preg , const sbool bEscapeLF , const sbool discardTruncatedMsg , const sbool msgDiscardingError , int64 * const strtOffs ) { uchar c ; uchar finished = 0 ; cstr_t * thisLine = NULL ; rsRetVal readCharRet ; const time_t tCurr = pThis -> readTimeout ? getTime ( NULL ) : 0 ; int maxMsgSize = glblGetMaxLine ( ) ; DEFiRet ; do { CHKiRet ( strmReadChar ( pThis , & c ) ) ; pThis -> lastRead = tCurr ; CHKiRet ( cstrConstruct ( & thisLine ) ) ; if ( pThis -> prevLineSegment != NULL ) { CHKiRet ( cstrAppendCStr ( thisLine , pThis -> prevLineSegment ) ) ; cstrDestruct ( & pThis -> prevLineSegment ) ; } while ( c != ' \n ' ) { CHKiRet ( cstrAppendChar ( thisLine , c ) ) ; readCharRet = strmReadChar ( pThis , & c ) ; if ( readCharRet == RS_RET_EOF ) { CHKiRet ( rsCStrConstructFromCStr ( & pThis -> prevLineSegment , thisLine ) ) ; } CHKiRet ( readCharRet ) ; } cstrFinalize ( thisLine ) ; const int isStartMatch = start_preg ? ! regexec ( start_preg , ( char * ) rsCStrGetSzStrNoNULL ( thisLine ) , 0 , NULL , 0 ) : 0 ; const int isEndMatch = end_preg ? ! regexec ( end_preg , ( char * ) rsCStrGetSzStrNoNULL ( thisLine ) , 0 , NULL , 0 ) : 0 ; if ( isStartMatch ) { if ( pThis -> ignoringMsg == 0 ) { if ( pThis -> prevMsgSegment != NULL ) { finished = 1 ; * ppCStr = pThis -> prevMsgSegment ; } } CHKiRet ( rsCStrConstructFromCStr ( & pThis -> prevMsgSegment , thisLine ) ) ; pThis -> ignoringMsg = 0 ; } else { if ( pThis -> ignoringMsg == 0 ) { if ( pThis -> prevMsgSegment == NULL ) { CHKiRet ( rsCStrConstructFromCStr ( & pThis -> prevMsgSegment , thisLine ) ) ; } else { if ( bEscapeLF ) { rsCStrAppendStrWithLen ( pThis -> prevMsgSegment , ( uchar * ) " \\ " , 2 ) ; } else { cstrAppendChar ( pThis -> prevMsgSegment , ' \n ' ) ; } int currLineLen = cstrLen ( thisLine ) ; if ( currLineLen > 0 ) { int len ; if ( ( len = cstrLen ( pThis -> prevMsgSegment ) + currLineLen ) < maxMsgSize ) { CHKiRet ( cstrAppendCStr ( pThis -> prevMsgSegment , thisLine ) ) ; } else { len = currLineLen - ( len - maxMsgSize ) ; for ( int z = 0 ; z < len ; z ++ ) { cstrAppendChar ( pThis -> prevMsgSegment , thisLine -> pBuf [ z ] ) ; } finished = 1 ; * ppCStr = pThis -> prevMsgSegment ; CHKiRet ( rsCStrConstructFromszStr ( & pThis -> prevMsgSegment , thisLine -> pBuf + len ) ) ; if ( discardTruncatedMsg == 1 ) { pThis -> ignoringMsg = 1 ; } if ( msgDiscardingError == 1 ) { if ( discardTruncatedMsg == 1 ) { LogError ( 0 , RS_RET_ERR , " " " " " " " " ) ; } else { LogError ( 0 , RS_RET_ERR , " " " " " " " " ) ; } } } } } } } if ( isEndMatch ) { if ( pThis -> ignoringMsg == 0 ) { if ( pThis -> prevMsgSegment != NULL ) { finished = 1 ; * ppCStr = pThis -> prevMsgSegment ; pThis -> prevMsgSegment = NULL ; } } pThis -> ignoringMsg = 0 ; } cstrDestruct ( & thisLine ) ; } while ( finished == 0 ) ; finalize_it : * strtOffs = pThis -> strtOffs ; if ( thisLine != NULL ) { cstrDestruct ( & thisLine ) ; } if ( iRet == RS_RET_OK ) { pThis -> strtOffs = pThis -> iCurrOffs ; cstrFinalize ( * ppCStr ) ; } else { if ( pThis -> readTimeout && ( pThis -> prevMsgSegment != NULL ) && ( tCurr > pThis -> lastRead + pThis -> readTimeout ) ) { if ( rsCStrConstructFromCStr ( ppCStr , pThis -> prevMsgSegment ) == RS_RET_OK ) { cstrFinalize ( * ppCStr ) ; cstrDestruct ( & pThis -> prevMsgSegment ) ; pThis -> lastRead = tCurr ; pThis -> strtOffs = pThis -> iCurrOffs ; dbgprintf ( " \n " , cstrGetSzStrNoNULL ( * ppCStr ) ) ; iRet = RS_RET_OK ; } } } RETiRet ; }
read a multi-line message from a strm file.
read a multi - line message from a strm file .
/* immediately exit on EOF */ /* append previous message to current message if necessary */ /* end of file reached without \n? */ /* we have a line, now let's assemble the message */ /* in this case, the *previous* message is complete and we are
             * at the start of a new one.
             */ /* may be NULL in initial poll! */ /* may be NULL in initial poll or after timeout! */ /* we could do this faster, but for now keep it
                             * simple */ /* in this case, the *current* message is complete and we are
             * at the end of it.
             */ /* we are at begin of next line */ /* we are at begin of next line */