1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for data table compiler
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
118 #include <contrib/dev/acpica/compiler/aslcompiler.h>
119 #include <contrib/dev/acpica/compiler/dtcompiler.h>
121 #define _COMPONENT DT_COMPILER
122 ACPI_MODULE_NAME ("dtio")
125 /* Local prototypes */
147 DT_SUBTABLE *Subtable,
152 /* States for DtGetNextLine */
154 #define DT_NORMAL_TEXT 0
155 #define DT_START_QUOTED_STRING 1
156 #define DT_START_COMMENT 2
157 #define DT_SLASH_ASTERISK_COMMENT 3
158 #define DT_SLASH_SLASH_COMMENT 4
159 #define DT_END_COMMENT 5
161 static UINT32 Gbl_NextLineOffset;
164 /******************************************************************************
168 * PARAMETERS: String - Current source code line to trim
170 * RETURN: Trimmed line. Must be freed by caller.
172 * DESCRIPTION: Trim left and right spaces
174 *****************************************************************************/
186 /* Skip lines that start with a space */
188 if (!ACPI_STRCMP (String, " "))
190 ReturnString = UtLocalCalloc (1);
191 return (ReturnString);
194 /* Setup pointers to start and end of input string */
197 End = String + ACPI_STRLEN (String) - 1;
199 /* Find first non-whitespace character */
201 while ((Start <= End) && ((*Start == ' ') || (*Start == '\t')))
206 /* Find last non-space character */
210 if (*End == '\r' || *End == '\n')
224 /* Remove any quotes around the string */
235 /* Create the trimmed return string */
237 Length = ACPI_PTR_DIFF (End, Start) + 1;
238 ReturnString = UtLocalCalloc (Length + 1);
239 if (ACPI_STRLEN (Start))
241 ACPI_STRNCPY (ReturnString, Start, Length);
244 ReturnString[Length] = 0;
245 return (ReturnString);
249 /******************************************************************************
251 * FUNCTION: DtLinkField
253 * PARAMETERS: Field - New field object to link
257 * DESCRIPTION: Link one field name and value to the list
259 *****************************************************************************/
269 Prev = Next = Gbl_FieldList;
283 Gbl_FieldList = Field;
288 /******************************************************************************
290 * FUNCTION: DtParseLine
292 * PARAMETERS: LineBuffer - Current source code line
293 * Line - Current line number in the source
294 * Offset - Current byte offset of the line
298 * DESCRIPTION: Parse one source line
300 *****************************************************************************/
326 Colon = strchr (LineBuffer, ':');
327 if (!Colon || *(Colon - 1) != ' ')
335 while (Start < Colon)
343 /* Found left bracket, go to the right bracket */
347 while (Start < Colon && *Start != ']')
354 MYDEBUG ("ERROR: right bracket reaches colon position\n");
366 * There are two column values. One for the field name,
367 * and one for the field value.
369 Column = ACPI_PTR_DIFF (Colon, LineBuffer) + 3;
370 NameColumn = ACPI_PTR_DIFF (Start, LineBuffer) + 1;
372 Length = ACPI_PTR_DIFF (End, Start);
374 TmpName = UtLocalCalloc (Length + 1);
375 ACPI_STRNCPY (TmpName, Start, Length);
376 Name = DtTrim (TmpName);
379 Start = End = (Colon + 1);
383 /* Found left quotation, go to the right quotation and break */
388 while (*End && *End != '"')
407 Length = ACPI_PTR_DIFF (End, Start);
408 TmpValue = UtLocalCalloc (Length + 1);
409 ACPI_STRNCPY (TmpValue, Start, Length);
410 Value = DtTrim (TmpValue);
411 ACPI_FREE (TmpValue);
415 Field = UtLocalCalloc (sizeof (DT_FIELD));
417 Field->Value = Value;
419 Field->ByteOffset = Offset;
420 Field->NameColumn = NameColumn;
421 Field->Column = Column;
428 /******************************************************************************
430 * FUNCTION: DtGetNextLine
432 * PARAMETERS: Handle - Open file handle for the source file
434 * RETURN: Filled line buffer and offset of start-of-line (zero on EOF)
436 * DESCRIPTION: Get the next valid source line. Removes all comments.
437 * Ignores empty lines.
439 * Handles both slash-asterisk and slash-slash comments.
440 * Also, quoted strings, but no escapes within.
442 * Line is returned in Gbl_CurrentLineBuffer.
443 * Line number in original file is returned in Gbl_CurrentLineNumber.
445 *****************************************************************************/
451 UINT32 State = DT_NORMAL_TEXT;
452 UINT32 CurrentLineOffset;
457 for (i = 0; i < ASL_LINE_BUFFER_SIZE;)
459 c = (char) getc (Handle);
469 /* Normal text, insert char into line buffer */
471 Gbl_CurrentLineBuffer[i] = c;
475 State = DT_START_COMMENT;
479 State = DT_START_QUOTED_STRING;
484 CurrentLineOffset = Gbl_NextLineOffset;
485 Gbl_NextLineOffset = (UINT32) ftell (Handle);
486 Gbl_CurrentLineNumber++;
488 /* Exit if line is complete. Ignore blank lines */
492 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate line */
493 return (CurrentLineOffset);
503 case DT_START_QUOTED_STRING:
505 /* Insert raw chars until end of quoted string */
507 Gbl_CurrentLineBuffer[i] = c;
512 State = DT_NORMAL_TEXT;
516 case DT_START_COMMENT:
518 /* Open comment if this character is an asterisk or slash */
523 State = DT_SLASH_ASTERISK_COMMENT;
527 State = DT_SLASH_SLASH_COMMENT;
530 default: /* Not a comment */
531 i++; /* Save the preceeding slash */
532 Gbl_CurrentLineBuffer[i] = c;
534 State = DT_NORMAL_TEXT;
539 case DT_SLASH_ASTERISK_COMMENT:
541 /* Ignore chars until an asterisk-slash is found */
546 Gbl_NextLineOffset = (UINT32) ftell (Handle);
547 Gbl_CurrentLineNumber++;
551 State = DT_END_COMMENT;
559 case DT_SLASH_SLASH_COMMENT:
561 /* Ignore chars until end-of-line */
565 /* We will exit via the NORMAL_TEXT path */
568 State = DT_NORMAL_TEXT;
574 /* End comment if this char is a slash */
579 State = DT_NORMAL_TEXT;
583 State = DT_SLASH_ASTERISK_COMMENT;
589 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
594 printf ("ERROR - Input line is too long (max %u)\n", ASL_LINE_BUFFER_SIZE);
599 /******************************************************************************
601 * FUNCTION: DtScanFile
603 * PARAMETERS: Handle - Open file handle for the source file
605 * RETURN: Pointer to start of the constructed parse tree.
607 * DESCRIPTION: Scan source file, link all field names and values
608 * to the global parse tree: Gbl_FieldList
610 *****************************************************************************/
619 ACPI_FUNCTION_NAME (DtScanFile);
622 /* Get the file size */
624 Gbl_InputByteCount = DtGetFileSize (Handle);
626 Gbl_CurrentLineNumber = 0;
627 Gbl_CurrentLineOffset = 0;
628 Gbl_NextLineOffset = 0;
630 /* Scan line-by-line */
632 while ((Offset = DtGetNextLine (Handle)))
634 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
635 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
637 DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
640 return (Gbl_FieldList);
649 /******************************************************************************
651 * FUNCTION: DtWriteBinary
653 * PARAMETERS: DT_WALK_CALLBACK
657 * DESCRIPTION: Write one subtable of a binary ACPI table
659 *****************************************************************************/
663 DT_SUBTABLE *Subtable,
668 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
672 /******************************************************************************
674 * FUNCTION: DtOutputBinary
680 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
682 *****************************************************************************/
686 DT_SUBTABLE *RootTable)
694 /* Walk the entire parse tree, emitting the binary data */
696 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
697 Gbl_TableLength = DtGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);