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
static rsRetVal strmReadLine(strm_t* const pThis, cstr_t** ppCStr, uint8_t mode,
                             sbool bEscapeLF, uint32_t trimLineOverBytes,
                             int64* const strtOffs)
{
    uchar c;
    uchar finished;
    DEFiRet;

    assert(pThis != NULL);
    assert(ppCStr != NULL);

    CHKiRet(cstrConstruct(ppCStr));
    CHKiRet(strmReadChar(pThis, &c));

    /* append previous message to current message if necessary */
    if (pThis->prevLineSegment != NULL)
    {
        cstrFinalize(pThis->prevLineSegment);
        dbgprintf("readLine: have previous line segment: '%s'\n",
                  rsCStrGetSzStrNoNULL(pThis->prevLineSegment));
        CHKiRet(cstrAppendCStr(*ppCStr, pThis->prevLineSegment));
        cstrDestruct(&pThis->prevLineSegment);
    }
    if (mode == 0)
    {
        while (c != '\n')
        {
            CHKiRet(cstrAppendChar(*ppCStr, c));
            CHKiRet(strmReadChar(pThis, &c));
        }
        if (trimLineOverBytes > 0 &&
            (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes)
        {
            /* Truncate long line at trimLineOverBytes position */
            dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes,
                      mode);
            rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes);
            cstrAppendChar(*ppCStr, '\n');
        }
        cstrFinalize(*ppCStr);
    }
    else if (mode == 1)
    {
        finished = 0;
        while (finished == 0)
        {
            if (c != '\n')
            {
                CHKiRet(cstrAppendChar(*ppCStr, c));
                CHKiRet(strmReadChar(pThis, &c));
                pThis->bPrevWasNL = 0;
            }
            else
            {
                if ((((*ppCStr)->iStrLen) > 0))
                {
                    if (pThis->bPrevWasNL)
                    {
                        rsCStrTruncate(*ppCStr, (bEscapeLF) ? 4 : 1);
                        /* remove the prior newline */
                        finished = 1;
                    }
                    else
                    {
                        if (bEscapeLF)
                        {
                            CHKiRet(rsCStrAppendStrWithLen(
                                *ppCStr, (uchar*)"#012", sizeof("#012") - 1));
                        }
                        else
                        {
                            CHKiRet(cstrAppendChar(*ppCStr, c));
                        }
                        CHKiRet(strmReadChar(pThis, &c));
                        pThis->bPrevWasNL = 1;
                    }
                }
                else
                {
                    finished = 1; /* this is a blank line, a \n with nothing
                             since the last complete record */
                }
            }
        }
        cstrFinalize(*ppCStr);
        pThis->bPrevWasNL = 0;
    }
    else if (mode == 2)
    {
        /* indented follow-up lines */
        finished = 0;
        while (finished == 0)
        {
            if ((*ppCStr)->iStrLen == 0)
            {
                if (c != '\n')
                {
                    /* nothing in the buffer, and it's not a newline, add it to
                     * the buffer */
                    CHKiRet(cstrAppendChar(*ppCStr, c));
                    CHKiRet(strmReadChar(pThis, &c));
                }
                else
                {
                    finished = 1; /* this is a blank line, a \n with nothing
                             since the last complete record */
                }
            }
            else
            {
                if (pThis->bPrevWasNL)
                {
                    if ((c == ' ') || (c == '\t'))
                    {
                        CHKiRet(cstrAppendChar(*ppCStr, c));
                        CHKiRet(strmReadChar(pThis, &c));
                        pThis->bPrevWasNL = 0;
                    }
                    else
                    {
                        /* clean things up by putting the character we just read
                         * back into the input buffer and removing the LF
                         * character that is currently at the
                         * end of the output string */
                        CHKiRet(strmUnreadChar(pThis, c));
                        rsCStrTruncate(*ppCStr, (bEscapeLF) ? 4 : 1);
                        finished = 1;
                    }
                }
                else
                { /* not the first character after a newline, add it to the
                     buffer */
                    if (c == '\n')
                    {
                        pThis->bPrevWasNL = 1;
                        if (bEscapeLF)
                        {
                            CHKiRet(rsCStrAppendStrWithLen(
                                *ppCStr, (uchar*)"#012", sizeof("#012") - 1));
                        }
                        else
                        {
                            CHKiRet(cstrAppendChar(*ppCStr, c));
                        }
                    }
                    else
                    {
                        CHKiRet(cstrAppendChar(*ppCStr, c));
                    }
                    CHKiRet(strmReadChar(pThis, &c));
                }
            }
        }
        if (trimLineOverBytes > 0 &&
            (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes)
        {
            /* Truncate long line at trimLineOverBytes position */
            dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes,
                      mode);
            rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes);
            cstrAppendChar(*ppCStr, '\n');
        }
        cstrFinalize(*ppCStr);
        pThis->bPrevWasNL = 0;
    }

finalize_it:
    if (iRet == RS_RET_OK)
    {
        if (strtOffs != NULL)
        {
            *strtOffs = pThis->strtOffs;
        }
        pThis->strtOffs = pThis->iCurrOffs; /* we are at begin of next line */
    }
    else
    {
        if (*ppCStr != NULL)
        {
            if (cstrLen(*ppCStr) > 0)
            {
                /* we may have an empty string in an unsuccesfull poll or after
                 * restart! */
                if (rsCStrConstructFromCStr(&pThis->prevLineSegment, *ppCStr) !=
                    RS_RET_OK)
                {
                    /* we cannot do anything against this, but we can at least
                     * ensure we do not have any follow-on errors.
                     */
                    pThis->prevLineSegment = NULL;
                }
            }
            cstrDestruct(ppCStr);
        }
    }

    RETiRet;
}
/* read a 'paragraph' from a strm file.
 * A paragraph may be terminated by a LF, by a LFLF, or by LF<not whitespace>
 * depending on the option set. The termination LF characters are read, but are
 * not returned in the buffer (it is discared). The caller is responsible for
 * destruction of the returned CStr object! -- dlang 2010-12-13
 *
 * Parameter mode controls legacy multi-line processing:
 * mode = 0 single line mode (equivalent to ReadLine)
 * mode = 1 LFLF mode (paragraph, blank line between entries)
 * mode = 2 LF <not whitespace> mode, a log line starts at the beginning of
 * a line, but following lines that are indented are part of the same log entry
 */
read a 'paragraph' from a strm file.
A paragraph may be terminated by a LF, by a LFLF, or by LF
depending on the option set. The termination LF characters are read, but are
not returned in the buffer (it is discared). The caller is responsible for
destruction of the returned CStr object.

Parameter mode controls legacy multi-line processing:
mode = 0 single line mode (equivalent to ReadLine)
mode = 1 LFLF mode (paragraph, blank line between entries)
mode = 2 LF  mode, a log line starts at the beginning of
a line, but following lines that are indented are part of the same log entry
read a ' paragraph ' from a strm file . A paragraph may be terminated by a LF by a LFLF or by LF depending on the option set . The termination LF characters are read but are not returned in the buffer ( it is discared ) . The caller is responsible for destruction of the returned CStr object . Parameter mode controls legacy multi - line processing : mode = 0 single line mode ( equivalent to ReadLine ) mode = 1 LFLF mode ( paragraph blank line between entries ) mode = 2 LF mode a log line starts at the beginning of a line but following lines that are indented are part of the same log entry
static rsRetVal strmReadLine(strm_t* const pThis, cstr_t** ppCStr, uint8_t mode,
                             sbool bEscapeLF, uint32_t trimLineOverBytes,
                             int64* const strtOffs)
{
    uchar c;
    uchar finished;
    DEFiRet;
    assert(pThis != NULL);
    assert(ppCStr != NULL);
    CHKiRet(cstrConstruct(ppCStr));
    CHKiRet(strmReadChar(pThis, &c));
    if (pThis->prevLineSegment != NULL)
    {
        cstrFinalize(pThis->prevLineSegment);
        dbgprintf("readLine: have previous line segment: '%s'\n",
                  rsCStrGetSzStrNoNULL(pThis->prevLineSegment));
        CHKiRet(cstrAppendCStr(*ppCStr, pThis->prevLineSegment));
        cstrDestruct(&pThis->prevLineSegment);
    }
    if (mode == 0)
    {
        while (c != '\n')
        {
            CHKiRet(cstrAppendChar(*ppCStr, c));
            CHKiRet(strmReadChar(pThis, &c));
        }
        if (trimLineOverBytes > 0 &&
            (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes)
        {
            dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes,
                      mode);
            rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes);
            cstrAppendChar(*ppCStr, '\n');
        }
        cstrFinalize(*ppCStr);
    }
    else if (mode == 1)
    {
        finished = 0;
        while (finished == 0)
        {
            if (c != '\n')
            {
                CHKiRet(cstrAppendChar(*ppCStr, c));
                CHKiRet(strmReadChar(pThis, &c));
                pThis->bPrevWasNL = 0;
            }
            else
            {
                if ((((*ppCStr)->iStrLen) > 0))
                {
                    if (pThis->bPrevWasNL)
                    {
                        rsCStrTruncate(*ppCStr, (bEscapeLF) ? 4 : 1);
                        finished = 1;
                    }
                    else
                    {
                        if (bEscapeLF)
                        {
                            CHKiRet(rsCStrAppendStrWithLen(
                                *ppCStr, (uchar*)"#012", sizeof("#012") - 1));
                        }
                        else
                        {
                            CHKiRet(cstrAppendChar(*ppCStr, c));
                        }
                        CHKiRet(strmReadChar(pThis, &c));
                        pThis->bPrevWasNL = 1;
                    }
                }
                else
                {
                    finished = 1; 
                }
            }
        }
        cstrFinalize(*ppCStr);
        pThis->bPrevWasNL = 0;
    }
    else if (mode == 2)
    {
        finished = 0;
        while (finished == 0)
        {
            if ((*ppCStr)->iStrLen == 0)
            {
                if (c != '\n')
                {
                    CHKiRet(cstrAppendChar(*ppCStr, c));
                    CHKiRet(strmReadChar(pThis, &c));
                }
                else
                {
                    finished = 1; 
                }
            }
            else
            {
                if (pThis->bPrevWasNL)
                {
                    if ((c == ' ') || (c == '\t'))
                    {
                        CHKiRet(cstrAppendChar(*ppCStr, c));
                        CHKiRet(strmReadChar(pThis, &c));
                        pThis->bPrevWasNL = 0;
                    }
                    else
                    {
                        CHKiRet(strmUnreadChar(pThis, c));
                        rsCStrTruncate(*ppCStr, (bEscapeLF) ? 4 : 1);
                        finished = 1;
                    }
                }
                else
                { 
                    if (c == '\n')
                    {
                        pThis->bPrevWasNL = 1;
                        if (bEscapeLF)
                        {
                            CHKiRet(rsCStrAppendStrWithLen(
                                *ppCStr, (uchar*)"#012", sizeof("#012") - 1));
                        }
                        else
                        {
                            CHKiRet(cstrAppendChar(*ppCStr, c));
                        }
                    }
                    else
                    {
                        CHKiRet(cstrAppendChar(*ppCStr, c));
                    }
                    CHKiRet(strmReadChar(pThis, &c));
                }
            }
        }
        if (trimLineOverBytes > 0 &&
            (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes)
        {
            dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes,
                      mode);
            rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes);
            cstrAppendChar(*ppCStr, '\n');
        }
        cstrFinalize(*ppCStr);
        pThis->bPrevWasNL = 0;
    }
finalize_it:
    if (iRet == RS_RET_OK)
    {
        if (strtOffs != NULL)
        {
            *strtOffs = pThis->strtOffs;
        }
        pThis->strtOffs = pThis->iCurrOffs; 
    }
    else
    {
        if (*ppCStr != NULL)
        {
            if (cstrLen(*ppCStr) > 0)
            {
                if (rsCStrConstructFromCStr(&pThis->prevLineSegment, *ppCStr) !=
                    RS_RET_OK)
                {
                    pThis->prevLineSegment = NULL;
                }
            }
            cstrDestruct(ppCStr);
        }
    }
    RETiRet;
}
static rsRetVal strmReadLine ( strm_t * const pThis , cstr_t * * ppCStr , uint8_t mode , sbool bEscapeLF , uint32_t trimLineOverBytes , int64 * const strtOffs ) { uchar c ; uchar finished ; DEFiRet ; assert ( pThis != NULL ) ; assert ( ppCStr != NULL ) ; CHKiRet ( cstrConstruct ( ppCStr ) ) ; CHKiRet ( strmReadChar ( pThis , & c ) ) ; if ( pThis -> prevLineSegment != NULL ) { cstrFinalize ( pThis -> prevLineSegment ) ; dbgprintf ( " \n " , rsCStrGetSzStrNoNULL ( pThis -> prevLineSegment ) ) ; CHKiRet ( cstrAppendCStr ( * ppCStr , pThis -> prevLineSegment ) ) ; cstrDestruct ( & pThis -> prevLineSegment ) ; } if ( mode == 0 ) { while ( c != ' \n ' ) { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; CHKiRet ( strmReadChar ( pThis , & c ) ) ; } if ( trimLineOverBytes > 0 && ( uint32_t ) cstrLen ( * ppCStr ) > trimLineOverBytes ) { dbgprintf ( " \n " , trimLineOverBytes , mode ) ; rsCStrTruncate ( * ppCStr , cstrLen ( * ppCStr ) - trimLineOverBytes ) ; cstrAppendChar ( * ppCStr , ' \n ' ) ; } cstrFinalize ( * ppCStr ) ; } else if ( mode == 1 ) { finished = 0 ; while ( finished == 0 ) { if ( c != ' \n ' ) { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; CHKiRet ( strmReadChar ( pThis , & c ) ) ; pThis -> bPrevWasNL = 0 ; } else { if ( ( ( ( * ppCStr ) -> iStrLen ) > 0 ) ) { if ( pThis -> bPrevWasNL ) { rsCStrTruncate ( * ppCStr , ( bEscapeLF ) ? 4 : 1 ) ; finished = 1 ; } else { if ( bEscapeLF ) { CHKiRet ( rsCStrAppendStrWithLen ( * ppCStr , ( uchar * ) " " , sizeof ( " " ) - 1 ) ) ; } else { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; } CHKiRet ( strmReadChar ( pThis , & c ) ) ; pThis -> bPrevWasNL = 1 ; } } else { finished = 1 ; } } } cstrFinalize ( * ppCStr ) ; pThis -> bPrevWasNL = 0 ; } else if ( mode == 2 ) { finished = 0 ; while ( finished == 0 ) { if ( ( * ppCStr ) -> iStrLen == 0 ) { if ( c != ' \n ' ) { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; CHKiRet ( strmReadChar ( pThis , & c ) ) ; } else { finished = 1 ; } } else { if ( pThis -> bPrevWasNL ) { if ( ( c == ' ' ) || ( c == ' \t ' ) ) { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; CHKiRet ( strmReadChar ( pThis , & c ) ) ; pThis -> bPrevWasNL = 0 ; } else { CHKiRet ( strmUnreadChar ( pThis , c ) ) ; rsCStrTruncate ( * ppCStr , ( bEscapeLF ) ? 4 : 1 ) ; finished = 1 ; } } else { if ( c == ' \n ' ) { pThis -> bPrevWasNL = 1 ; if ( bEscapeLF ) { CHKiRet ( rsCStrAppendStrWithLen ( * ppCStr , ( uchar * ) " " , sizeof ( " " ) - 1 ) ) ; } else { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; } } else { CHKiRet ( cstrAppendChar ( * ppCStr , c ) ) ; } CHKiRet ( strmReadChar ( pThis , & c ) ) ; } } } if ( trimLineOverBytes > 0 && ( uint32_t ) cstrLen ( * ppCStr ) > trimLineOverBytes ) { dbgprintf ( " \n " , trimLineOverBytes , mode ) ; rsCStrTruncate ( * ppCStr , cstrLen ( * ppCStr ) - trimLineOverBytes ) ; cstrAppendChar ( * ppCStr , ' \n ' ) ; } cstrFinalize ( * ppCStr ) ; pThis -> bPrevWasNL = 0 ; } finalize_it : if ( iRet == RS_RET_OK ) { if ( strtOffs != NULL ) { * strtOffs = pThis -> strtOffs ; } pThis -> strtOffs = pThis -> iCurrOffs ; } else { if ( * ppCStr != NULL ) { if ( cstrLen ( * ppCStr ) > 0 ) { if ( rsCStrConstructFromCStr ( & pThis -> prevLineSegment , * ppCStr ) != RS_RET_OK ) { pThis -> prevLineSegment = NULL ; } } cstrDestruct ( ppCStr ) ; } } RETiRet ; }
read a 'paragraph' from a strm file.
read a ' paragraph ' from a strm file .
/* append previous message to current message if necessary */ /* Truncate long line at trimLineOverBytes position */ /* remove the prior newline */ /* this is a blank line, a \n with nothing
                             since the last complete record */ /* indented follow-up lines */ /* nothing in the buffer, and it's not a newline, add it to
                     * the buffer */ /* this is a blank line, a \n with nothing
                             since the last complete record */ /* clean things up by putting the character we just read
                         * back into the input buffer and removing the LF
                         * character that is currently at the
                         * end of the output string */ /* not the first character after a newline, add it to the
                     buffer */ /* Truncate long line at trimLineOverBytes position */ /* we are at begin of next line */ /* we may have an empty string in an unsuccesfull poll or after
                 * restart! */ /* we cannot do anything against this, but we can at least
                     * ensure we do not have any follow-on errors.
                     */