FreeBSD/sys/contrib/dev/acpica/compiler/dtio.c
/******************************************************************************
 *
 * FUNCTION:    DtGetNextLine
 *
 * PARAMETERS:  Handle              - Open file handle for the source file
 *
 * RETURN:      Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
 *
 * DESCRIPTION: Get the next valid source line. Removes all comments.
 *              Ignores empty lines.
 *
 * Handles both slash-asterisk and slash-slash comments.
 * Also, quoted strings, but no escapes within.
 *
 * Line is returned in Gbl_CurrentLineBuffer.
 * Line number in original file is returned in Gbl_CurrentLineNumber.
 *
 *****************************************************************************/
DtGetNextLine
PARAMETERS:  Handle              - Open file handle for the source file

Filled line buffer and offset of start-of-line (ASL_EOF on EOF)

Get the next valid source line. Removes all comments.
Ignores empty lines.



Line is returned in Gbl_CurrentLineBuffer.
Line number in original file is returned in Gbl_CurrentLineNumber.
DtGetNextLine PARAMETERS : Handle - Open file handle for the source file Filled line buffer and offset of start - of - line ( ASL_EOF on EOF ) Get the next valid source line . Removes all comments . Ignores empty lines . Line is returned in Gbl_CurrentLineBuffer . Line number in original file is returned in Gbl_CurrentLineNumber .
UINT32
DtGetNextLine (
    FILE                    *Handle,
    UINT32                  Flags)
{
    BOOLEAN                 LineNotAllBlanks = FALSE;
    UINT32                  State = DT_NORMAL_TEXT;
    UINT32                  CurrentLineOffset;
    UINT32                  i;
    int                     c;
    memset (Gbl_CurrentLineBuffer, 0, Gbl_LineBufferSize);
    for (i = 0; ;)
    {
        if (i >= Gbl_LineBufferSize)
        {
            UtExpandLineBuffers ();
        }
        c = getc (Handle);
        if (c == EOF)
        {
            switch (State)
            {
            case DT_START_QUOTED_STRING:
            case DT_SLASH_ASTERISK_COMMENT:
                AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
                break;
            default:
                break;
            }
            if (i == 0)
            {
                return (ASL_EOF);
            }
            c = '\n';
            State = DT_NORMAL_TEXT;
        }
        switch (State)
        {
        case DT_NORMAL_TEXT:
            Gbl_CurrentLineBuffer[i] = (char) c;
            switch (c)
            {
            case '/':
                State = DT_START_COMMENT;
                break;
            case '"':
                State = DT_START_QUOTED_STRING;
                LineNotAllBlanks = TRUE;
                i++;
                break;
            case '\\':
                State = DT_MERGE_LINES;
                break;
            case '\n':
                CurrentLineOffset = Gbl_NextLineOffset;
                Gbl_NextLineOffset = (UINT32) ftell (Handle);
                Gbl_CurrentLineNumber++;
                if ((i != 0) && LineNotAllBlanks)
                {
                    if ((i + 1) >= Gbl_LineBufferSize)
                    {
                        UtExpandLineBuffers ();
                    }
                    Gbl_CurrentLineBuffer[i+1] = 0; 
                    return (CurrentLineOffset);
                }
                i = 0;
                LineNotAllBlanks = FALSE;
                break;
            default:
                if (c != ' ')
                {
                    LineNotAllBlanks = TRUE;
                }
                i++;
                break;
            }
            break;
        case DT_START_QUOTED_STRING:
            Gbl_CurrentLineBuffer[i] = (char) c;
            i++;
            switch (c)
            {
            case '"':
                State = DT_NORMAL_TEXT;
                break;
            case '\\':
                State = DT_ESCAPE_SEQUENCE;
                break;
            case '\n':
                if (!(Flags & DT_ALLOW_MULTILINE_QUOTES))
                {
                    AcpiOsPrintf (
                        "ERROR at line %u: Unterminated quoted string\n",
                        Gbl_CurrentLineNumber++);
                    State = DT_NORMAL_TEXT;
                }
                break;
            default:    
                break;
            }
            break;
        case DT_ESCAPE_SEQUENCE:
            Gbl_CurrentLineBuffer[i] = (char) c;
            i++;
            State = DT_START_QUOTED_STRING;
            break;
        case DT_START_COMMENT:
            switch (c)
            {
            case '*':
                State = DT_SLASH_ASTERISK_COMMENT;
                break;
            case '/':
                State = DT_SLASH_SLASH_COMMENT;
                break;
            default:    
                i++;    
                if (i >= Gbl_LineBufferSize)
                {
                    UtExpandLineBuffers ();
                }
                Gbl_CurrentLineBuffer[i] = (char) c;
                i++;
                State = DT_NORMAL_TEXT;
                break;
            }
            break;
        case DT_SLASH_ASTERISK_COMMENT:
            switch (c)
            {
            case '\n':
                Gbl_NextLineOffset = (UINT32) ftell (Handle);
                Gbl_CurrentLineNumber++;
                break;
            case '*':
                State = DT_END_COMMENT;
                break;
            default:
                break;
            }
            break;
        case DT_SLASH_SLASH_COMMENT:
            if (c == '\n')
            {
                ungetc (c, Handle);
                State = DT_NORMAL_TEXT;
            }
            break;
        case DT_END_COMMENT:
            switch (c)
            {
            case '/':
                State = DT_NORMAL_TEXT;
                break;
            case '\n':
                CurrentLineOffset = Gbl_NextLineOffset;
                Gbl_NextLineOffset = (UINT32) ftell (Handle);
                Gbl_CurrentLineNumber++;
                break;
            case '*':
                break;
            default:
                State = DT_SLASH_ASTERISK_COMMENT;
                break;
            }
            break;
        case DT_MERGE_LINES:
            if (c != '\n')
            {
                i++; 
                ungetc (c, Handle);
                State = DT_NORMAL_TEXT;
            }
            else
            {
                Gbl_CurrentLineBuffer[i] = ' ';
                i++;
                CurrentLineOffset = Gbl_NextLineOffset;
                Gbl_NextLineOffset = (UINT32) ftell (Handle);
                Gbl_CurrentLineNumber++;
                State = DT_NORMAL_TEXT;
            }
            break;
        default:
            DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
            return (ASL_EOF);
        }
    }
}
UINT32 DtGetNextLine ( FILE * Handle , UINT32 Flags ) { BOOLEAN LineNotAllBlanks = FALSE ; UINT32 State = DT_NORMAL_TEXT ; UINT32 CurrentLineOffset ; UINT32 i ; int c ; memset ( Gbl_CurrentLineBuffer , 0 , Gbl_LineBufferSize ) ; for ( i = 0 ; ; ) { if ( i >= Gbl_LineBufferSize ) { UtExpandLineBuffers ( ) ; } c = getc ( Handle ) ; if ( c == EOF ) { switch ( State ) { case DT_START_QUOTED_STRING : case DT_SLASH_ASTERISK_COMMENT : AcpiOsPrintf ( " \n " , State ) ; break ; default : break ; } if ( i == 0 ) { return ( ASL_EOF ) ; } c = ' \n ' ; State = DT_NORMAL_TEXT ; } switch ( State ) { case DT_NORMAL_TEXT : Gbl_CurrentLineBuffer [ i ] = ( char ) c ; switch ( c ) { case ' ' : State = DT_START_COMMENT ; break ; case ' ' : State = DT_START_QUOTED_STRING ; LineNotAllBlanks = TRUE ; i ++ ; break ; case ' \\ ' : State = DT_MERGE_LINES ; break ; case ' \n ' : CurrentLineOffset = Gbl_NextLineOffset ; Gbl_NextLineOffset = ( UINT32 ) ftell ( Handle ) ; Gbl_CurrentLineNumber ++ ; if ( ( i != 0 ) && LineNotAllBlanks ) { if ( ( i + 1 ) >= Gbl_LineBufferSize ) { UtExpandLineBuffers ( ) ; } Gbl_CurrentLineBuffer [ i + 1 ] = 0 ; return ( CurrentLineOffset ) ; } i = 0 ; LineNotAllBlanks = FALSE ; break ; default : if ( c != ' ' ) { LineNotAllBlanks = TRUE ; } i ++ ; break ; } break ; case DT_START_QUOTED_STRING : Gbl_CurrentLineBuffer [ i ] = ( char ) c ; i ++ ; switch ( c ) { case ' ' : State = DT_NORMAL_TEXT ; break ; case ' \\ ' : State = DT_ESCAPE_SEQUENCE ; break ; case ' \n ' : if ( ! ( Flags & DT_ALLOW_MULTILINE_QUOTES ) ) { AcpiOsPrintf ( " \n " , Gbl_CurrentLineNumber ++ ) ; State = DT_NORMAL_TEXT ; } break ; default : break ; } break ; case DT_ESCAPE_SEQUENCE : Gbl_CurrentLineBuffer [ i ] = ( char ) c ; i ++ ; State = DT_START_QUOTED_STRING ; break ; case DT_START_COMMENT : switch ( c ) { case ' ' : State = DT_SLASH_ASTERISK_COMMENT ; break ; case ' ' : State = DT_SLASH_SLASH_COMMENT ; break ; default : i ++ ; if ( i >= Gbl_LineBufferSize ) { UtExpandLineBuffers ( ) ; } Gbl_CurrentLineBuffer [ i ] = ( char ) c ; i ++ ; State = DT_NORMAL_TEXT ; break ; } break ; case DT_SLASH_ASTERISK_COMMENT : switch ( c ) { case ' \n ' : Gbl_NextLineOffset = ( UINT32 ) ftell ( Handle ) ; Gbl_CurrentLineNumber ++ ; break ; case ' ' : State = DT_END_COMMENT ; break ; default : break ; } break ; case DT_SLASH_SLASH_COMMENT : if ( c == ' \n ' ) { ungetc ( c , Handle ) ; State = DT_NORMAL_TEXT ; } break ; case DT_END_COMMENT : switch ( c ) { case ' ' : State = DT_NORMAL_TEXT ; break ; case ' \n ' : CurrentLineOffset = Gbl_NextLineOffset ; Gbl_NextLineOffset = ( UINT32 ) ftell ( Handle ) ; Gbl_CurrentLineNumber ++ ; break ; case ' ' : break ; default : State = DT_SLASH_ASTERISK_COMMENT ; break ; } break ; case DT_MERGE_LINES : if ( c != ' \n ' ) { i ++ ; ungetc ( c , Handle ) ; State = DT_NORMAL_TEXT ; } else { Gbl_CurrentLineBuffer [ i ] = ' ' ; i ++ ; CurrentLineOffset = Gbl_NextLineOffset ; Gbl_NextLineOffset = ( UINT32 ) ftell ( Handle ) ; Gbl_CurrentLineNumber ++ ; State = DT_NORMAL_TEXT ; } break ; default : DtFatal ( ASL_MSG_COMPILER_INTERNAL , NULL , " " ) ; return ( ASL_EOF ) ; } } }
FUNCTION:    DtGetNextLine
 PARAMETERS:  Handle              - Open file handle for the source file
FUNCTION : DtGetNextLine PARAMETERS : Handle - Open file handle for the source file
/*
         * If line is too long, expand the line buffers. Also increases
         * Gbl_LineBufferSize.
         */ /* Standalone EOF is OK */ /*
             * Received an EOF in the middle of a line. Terminate the
             * line with a newline. The next call to this function will
             * return a standalone EOF. Thus, the upper parsing software
             * never has to deal with an EOF within a valid line (or
             * the last line does not get tossed on the floor.)
             */ /* Normal text, insert char into line buffer */ /*
                 * The continuation char MUST be last char on this line.
                 * Otherwise, it will be assumed to be a valid ASL char.
                 */ /*
                 * Exit if line is complete. Ignore empty lines (only \n)
                 * or lines that contain nothing but blanks.
                 */ /* Terminate string */ /* Toss this line and start a new one */ /* Insert raw chars until end of quoted string */ /* Get next character */ /* Just copy the escaped character. TBD: sufficient for table compiler? */ /* Open comment if this character is an asterisk or slash */ /* Not a comment */ /* Save the preceding slash */ /* Ignore chars until an asterisk-slash is found */ /* Ignore chars until end-of-line */ /* We will exit via the NORMAL_TEXT path */ /* End comment if this char is a slash */ /* Consume all adjacent asterisks */ /*
                 * This is not a continuation backslash, it is a normal
                 * normal ASL backslash - for example: Scope(\_SB_)
                 */ /* Keep the backslash that is already in the buffer */ /*
                 * This is a continuation line -- a backlash followed
                 * immediately by a newline. Insert a space between the
                 * lines (overwrite the backslash)
                 */ /* Ignore newline, this will merge the lines */