xref: /illumos-gate/usr/src/cmd/acpi/iasl/aslsupport.l (revision b68ab351091d2aea3f72a011f64de12fb05fc833)
1 /******************************************************************************
2  *
3  * Module Name: aslsupport.l - Flex/lex scanner C support routines.
4  *              NOTE: Included into aslcompile.l, not compiled by itself.
5  *
6  *****************************************************************************/
7 
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights. You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code. No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision. In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change. Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee. Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution. In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government. In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************
116  *
117  * Alternatively, you may choose to be licensed under the terms of the
118  * following license:
119  *
120  * Redistribution and use in source and binary forms, with or without
121  * modification, are permitted provided that the following conditions
122  * are met:
123  * 1. Redistributions of source code must retain the above copyright
124  *    notice, this list of conditions, and the following disclaimer,
125  *    without modification.
126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127  *    substantially similar to the "NO WARRANTY" disclaimer below
128  *    ("Disclaimer") and any redistribution must be conditioned upon
129  *    including a substantially similar Disclaimer requirement for further
130  *    binary redistribution.
131  * 3. Neither the names of the above-listed copyright holders nor the names
132  *    of any contributors may be used to endorse or promote products derived
133  *    from this software without specific prior written permission.
134  *
135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Alternatively, you may choose to be licensed under the terms of the
148  * GNU General Public License ("GPL") version 2 as published by the Free
149  * Software Foundation.
150  *
151  *****************************************************************************/
152 
153 /* Configuration */
154 
155 #define ASL_SPACES_PER_TAB      4
156 
157 #define ASL_NORMAL_CHAR         0
158 #define ASL_ESCAPE_SEQUENCE     1
159 #define ASL_OCTAL_CONSTANT      2
160 #define ASL_HEX_CONSTANT        3
161 
162 
163 void
164 yyerror (char const *s)
165 {
166 
167   AcpiOsPrintf ("YYERROR: %s\n", s);
168 }
169 
170 
171 /*******************************************************************************
172  *
173  * FUNCTION:    AslParserCleanup
174  *
175  * Used to delete the current buffer
176  *
177  ******************************************************************************/
178 
179 void
180 AslParserCleanup (
181     void)
182 {
183 
184     yy_delete_buffer (YY_CURRENT_BUFFER);
185 }
186 
187 
188 /*******************************************************************************
189  *
190  * FUNCTION:    AslDoLineDirective
191  *
192  * PARAMETERS:  None. Uses input() to access current source code line
193  *
194  * RETURN:      Updates global line number and filename
195  *
196  * DESCRIPTION: Handle #line directives emitted by the preprocessor.
197  *
198  * The #line directive is emitted by the preprocesser, and is used to
199  * pass through line numbers from the original source code file to the
200  * preprocessor output file (.i). This allows any compiler-generated
201  * error messages to be displayed with the correct line number.
202  *
203  ******************************************************************************/
204 
205 static void
206 AslDoLineDirective (
207     void)
208 {
209     int                     c;
210     char                    *Token;
211     UINT32                  LineNumber;
212     char                    *Filename;
213     UINT32                  i;
214 
215    Gbl_HasIncludeFiles = TRUE;
216 
217     /* Eat the entire line that contains the #line directive */
218 
219     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
220 
221     while ((c = input()) != '\n' && c != EOF)
222     {
223         *Gbl_LineBufPtr = c;
224         Gbl_LineBufPtr++;
225     }
226     *Gbl_LineBufPtr = 0;
227 
228     /* First argument is the actual line number */
229 
230     Token = strtok (Gbl_CurrentLineBuffer, " ");
231     if (!Token)
232     {
233         goto ResetAndExit;
234     }
235 
236     /* First argument is the line number */
237 
238     LineNumber = (UINT32) UtDoConstant (Token);
239 
240     /* Emit the appropriate number of newlines */
241 
242     Gbl_CurrentColumn = 0;
243     if (LineNumber > Gbl_CurrentLineNumber)
244     {
245         for (i = 0; i < (LineNumber - Gbl_CurrentLineNumber); i++)
246         {
247             FlWriteFile (ASL_FILE_SOURCE_OUTPUT, "\n", 1);
248             Gbl_CurrentColumn++;
249         }
250     }
251 
252     FlSetLineNumber (LineNumber);
253 
254     /* Second argument is the optional filename (in double quotes) */
255 
256     Token = strtok (NULL, " \"");
257     if (Token)
258     {
259         Filename = ACPI_ALLOCATE_ZEROED (strlen (Token) + 1);
260         strcpy (Filename, Token);
261         FlSetFilename (Filename);
262     }
263 
264     /* Third argument is not supported at this time */
265 
266 ResetAndExit:
267 
268     /* Reset globals for a new line */
269 
270     Gbl_CurrentLineOffset += Gbl_CurrentColumn;
271     Gbl_CurrentColumn = 0;
272     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
273 }
274 
275 
276 /*******************************************************************************
277  *
278  * FUNCTION:    AslPopInputFileStack
279  *
280  * PARAMETERS:  None
281  *
282  * RETURN:      0 if a node was popped, -1 otherwise
283  *
284  * DESCRIPTION: Pop the top of the input file stack and point the parser to
285  *              the saved parse buffer contained in the fnode. Also, set the
286  *              global line counters to the saved values. This function is
287  *              called when an include file reaches EOF.
288  *
289  ******************************************************************************/
290 
291 int
292 AslPopInputFileStack (
293     void)
294 {
295     ASL_FILE_NODE           *Fnode;
296 
297 
298     Gbl_PreviousIncludeFilename = Gbl_Files[ASL_FILE_INPUT].Filename;
299     Fnode = Gbl_IncludeFileStack;
300     DbgPrint (ASL_PARSE_OUTPUT,
301         "\nPop InputFile Stack, Fnode %p\n", Fnode);
302 
303     DbgPrint (ASL_PARSE_OUTPUT,
304         "Include: Closing \"%s\"\n\n", Gbl_Files[ASL_FILE_INPUT].Filename);
305 
306     if (!Fnode)
307     {
308         return (-1);
309     }
310 
311     /* Close the current include file */
312 
313     fclose (yyin);
314 
315     /* Update the top-of-stack */
316 
317     Gbl_IncludeFileStack = Fnode->Next;
318 
319     /* Reset global line counter and filename */
320 
321     Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
322     Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
323 
324     /* Point the parser to the popped file */
325 
326     yy_delete_buffer (YY_CURRENT_BUFFER);
327     yy_switch_to_buffer (Fnode->State);
328 
329     /* All done with this node */
330 
331     ACPI_FREE (Fnode);
332     return (0);
333 }
334 
335 
336 /*******************************************************************************
337  *
338  * FUNCTION:    AslPushInputFileStack
339  *
340  * PARAMETERS:  InputFile           - Open file pointer
341  *              Filename            - Name of the file
342  *
343  * RETURN:      None
344  *
345  * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
346  *              to this file. Called when an include file is successfully
347  *              opened.
348  *
349  ******************************************************************************/
350 
351 void
352 AslPushInputFileStack (
353     FILE                    *InputFile,
354     char                    *Filename)
355 {
356     ASL_FILE_NODE           *Fnode;
357     YY_BUFFER_STATE         State;
358 
359 
360     /* Save the current state in an Fnode */
361 
362     Fnode = UtLocalCalloc (sizeof (ASL_FILE_NODE));
363 
364     Fnode->File = yyin;
365     Fnode->Next = Gbl_IncludeFileStack;
366     Fnode->State = YY_CURRENT_BUFFER;
367     Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
368     Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
369 
370     /* Push it on the stack */
371 
372     Gbl_IncludeFileStack = Fnode;
373 
374     /* Point the parser to this file */
375 
376     State = yy_create_buffer (InputFile, YY_BUF_SIZE);
377     yy_switch_to_buffer (State);
378 
379     DbgPrint (ASL_PARSE_OUTPUT,
380         "\nPush InputFile Stack, returning %p\n\n", InputFile);
381 
382     /* Reset the global line count and filename */
383 
384     Gbl_Files[ASL_FILE_INPUT].Filename =
385         UtLocalCacheCalloc (strlen (Filename) + 1);
386 
387     strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
388 
389     Gbl_CurrentLineNumber = 1;
390     yyin = InputFile;
391 
392     /* converter: reset the comment state to STANDARD_COMMENT */
393 
394     Gbl_CommentState.CommentType = STANDARD_COMMENT;
395 }
396 
397 
398 /*******************************************************************************
399  *
400  * FUNCTION:    AslResetCurrentLineBuffer
401  *
402  * PARAMETERS:  None
403  *
404  * RETURN:      None
405  *
406  * DESCRIPTION: Reset the Line Buffer to zero, increment global line numbers.
407  *
408  ******************************************************************************/
409 
410 void
411 AslResetCurrentLineBuffer (
412     void)
413 {
414 
415     if (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle)
416     {
417         FlWriteFile (ASL_FILE_SOURCE_OUTPUT, Gbl_CurrentLineBuffer,
418             Gbl_LineBufPtr - Gbl_CurrentLineBuffer);
419     }
420 
421     Gbl_CurrentLineOffset += Gbl_CurrentColumn;
422     Gbl_CurrentColumn = 0;
423 
424     Gbl_CurrentLineNumber++;
425     Gbl_LogicalLineNumber++;
426     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
427 }
428 
429 
430 /*******************************************************************************
431  *
432  * FUNCTION:    AslInsertLineBuffer
433  *
434  * PARAMETERS:  SourceChar          - One char from the input ASL source file
435  *
436  * RETURN:      None
437  *
438  * DESCRIPTION: Put one character of the source file into the temp line buffer
439  *
440  ******************************************************************************/
441 
442 void
443 AslInsertLineBuffer (
444     int                     SourceChar)
445 {
446     UINT32                  i;
447     UINT32                  Count = 1;
448 
449 
450     if (SourceChar == EOF)
451     {
452         return;
453     }
454 
455     Gbl_InputByteCount++;
456 
457     /* Handle tabs. Convert to spaces */
458 
459     if (SourceChar == '\t')
460     {
461         SourceChar = ' ';
462         Count = ASL_SPACES_PER_TAB -
463                     (Gbl_CurrentColumn & (ASL_SPACES_PER_TAB-1));
464     }
465 
466     for (i = 0; i < Count; i++)
467     {
468         Gbl_CurrentColumn++;
469 
470         /* Insert the character into the line buffer */
471 
472         *Gbl_LineBufPtr = (UINT8) SourceChar;
473         Gbl_LineBufPtr++;
474 
475         if (Gbl_LineBufPtr >
476             (Gbl_CurrentLineBuffer + (Gbl_LineBufferSize - 1)))
477         {
478 #if 0
479             /*
480              * Warning if we have split a long source line.
481              * <Probably overkill>
482              */
483             sprintf (MsgBuffer, "Max %u", Gbl_LineBufferSize);
484             AslCommonError (ASL_WARNING, ASL_MSG_LONG_LINE,
485                 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
486                 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
487                 Gbl_Files[ASL_FILE_INPUT].Filename, MsgBuffer);
488 #endif
489 
490             AslResetCurrentLineBuffer ();
491         }
492         else if (SourceChar == '\n')
493         {
494             /* End of line */
495 
496             AslResetCurrentLineBuffer ();
497         }
498 
499         if (AcpiGbl_CaptureComments)
500         {
501             CvProcessCommentState (SourceChar);
502         }
503     }
504 }
505 
506 
507 /*******************************************************************************
508  *
509  * FUNCTION:    count
510  *
511  * PARAMETERS:  yytext              - Contains the matched keyword.
512  *              Type                - Keyword/Character type:
513  *                                      0 = anything except a keyword
514  *                                      1 = pseudo-keywords
515  *                                      2 = non-executable ASL keywords
516  *                                      3 = executable ASL keywords
517  *
518  * RETURN:      None
519  *
520  * DESCRIPTION: Count keywords and put them into the line buffer
521  *
522  ******************************************************************************/
523 
524 static void
525 count (
526     int                 Type)
527 {
528     char                *p;
529 
530 
531     switch (Type)
532     {
533     case 2:
534 
535         TotalKeywords++;
536         TotalNamedObjects++;
537         break;
538 
539     case 3:
540 
541         TotalKeywords++;
542         TotalExecutableOpcodes++;
543         break;
544 
545     default:
546 
547         break;
548     }
549 
550     for (p = yytext; *p != '\0'; p++)
551     {
552         AslInsertLineBuffer (*p);
553         *Gbl_LineBufPtr = 0;
554     }
555 }
556 
557 
558 /*******************************************************************************
559  *
560  * FUNCTION:    AslDoComment
561  *
562  * PARAMETERS:  none
563  *
564  * RETURN:      none
565  *
566  * DESCRIPTION: Process a standard comment.
567  *
568  ******************************************************************************/
569 
570 static BOOLEAN
571 AslDoComment (
572     void)
573 {
574     int                     c;
575     int                     c1 = 0;
576     char                    *StringBuffer = MsgBuffer;
577     char                    *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
578     ASL_COMMENT_STATE       CurrentState = Gbl_CommentState; /* to reference later on */
579 
580 
581     AslInsertLineBuffer ('/');
582     AslInsertLineBuffer ('*');
583     if (AcpiGbl_CaptureComments && CurrentState.CaptureComments)
584     {
585         *StringBuffer = '/';
586         ++StringBuffer;
587         *StringBuffer = '*';
588         ++StringBuffer;
589     }
590 
591 loop:
592 
593     /* Eat chars until end-of-comment */
594 
595     while (((c = input ()) != '*') && (c != EOF))
596     {
597         AslInsertLineBuffer (c);
598         if (AcpiGbl_CaptureComments && CurrentState.CaptureComments)
599         {
600             *StringBuffer = c;
601             ++StringBuffer;
602         }
603         c1 = c;
604     }
605 
606     if (c == EOF)
607     {
608         goto EarlyEOF;
609     }
610 
611     /*
612      * Check for nested comment -- can help catch cases where a previous
613      * comment was accidently left unterminated
614      */
615     if ((c1 == '/') && (c == '*'))
616     {
617         AslCommonError (ASL_WARNING, ASL_MSG_NESTED_COMMENT,
618             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
619             Gbl_InputByteCount, Gbl_CurrentColumn,
620             Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
621     }
622 
623     /* Comment is closed only if the NEXT character is a slash */
624 
625     AslInsertLineBuffer (c);
626     if (AcpiGbl_CaptureComments && CurrentState.CaptureComments)
627     {
628         *StringBuffer = c;
629         ++StringBuffer;
630     }
631 
632     if (((c1 = input ()) != '/') && (c1 != EOF))
633     {
634         unput (c1);
635         goto loop;
636     }
637 
638     if (c1 == EOF)
639     {
640         goto EarlyEOF;
641     }
642     if (StringBuffer > EndBuffer)
643     {
644         goto BufferOverflow;
645     }
646 
647     AslInsertLineBuffer (c1);
648     CvProcessComment (CurrentState, StringBuffer, c1);
649     return (TRUE);
650 
651 
652 EarlyEOF:
653     /*
654      * Premature End-Of-File
655      */
656     AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
657         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
658         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
659         Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
660     return (FALSE);
661 
662 
663 BufferOverflow:
664 
665     /* Comment was too long */
666 
667     AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
668         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
669         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
670         Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");
671     return (FALSE);
672 
673 }
674 
675 
676 /*******************************************************************************
677  *
678  * FUNCTION:    AslDoCommentType2
679  *
680  * PARAMETERS:  none
681  *
682  * RETURN:      none
683  *
684  * DESCRIPTION: Process a new "//" comment. Inline comments will be converted
685  *              to "/ *" standard comments.
686  *
687  ******************************************************************************/
688 
689 static BOOLEAN
690 AslDoCommentType2 (
691     void)
692 {
693     int                     c;
694     char                    *StringBuffer = MsgBuffer;
695     char                    *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
696     ASL_COMMENT_STATE       CurrentState = Gbl_CommentState;
697 
698 
699     AslInsertLineBuffer ('/');
700 
701     if (AcpiGbl_CaptureComments && CurrentState.CaptureComments)
702     {
703         AslInsertLineBuffer ('*');
704         *StringBuffer = '/';
705         ++StringBuffer;
706         *StringBuffer = '*';
707         ++StringBuffer;
708     }
709     else
710     {
711         AslInsertLineBuffer ('/');
712     }
713 
714     while (((c = input ()) != '\n') && (c != EOF))
715     {
716         AslInsertLineBuffer (c);
717         if (AcpiGbl_CaptureComments && CurrentState.CaptureComments)
718         {
719             *StringBuffer = c;
720             ++StringBuffer;
721         }
722     }
723 
724     if (c == EOF)
725     {
726         /* End of file is OK, change to newline. Let parser detect EOF later */
727 
728         c = '\n';
729     }
730 
731     if (StringBuffer > EndBuffer)
732     {
733         goto BufferOverflow;
734     }
735     AslInsertLineBuffer (c);
736 
737     CvProcessCommentType2 (CurrentState, StringBuffer);
738     return (TRUE);
739 
740 
741 BufferOverflow:
742 
743     /* Comment was too long */
744 
745     AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
746         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
747         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
748         Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");
749     return (FALSE);
750 
751 }
752 
753 
754 /*******************************************************************************
755  *
756  * FUNCTION:    AslDoStringLiteral
757  *
758  * PARAMETERS:  none
759  *
760  * RETURN:      none
761  *
762  * DESCRIPTION: Process a string literal (surrounded by quotes)
763  *
764  ******************************************************************************/
765 
766 static char
767 AslDoStringLiteral (
768     void)
769 {
770     char                *StringBuffer = MsgBuffer;
771     char                *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
772     char                *CleanString;
773     int                 StringChar;
774     UINT32              State = ASL_NORMAL_CHAR;
775     UINT32              i = 0;
776     UINT8               Digit;
777     char                ConvertBuffer[4];
778 
779 
780     /*
781      * Eat chars until end-of-literal.
782      * NOTE:  Put back the original surrounding quotes into the
783      * source line buffer.
784      */
785     AslInsertLineBuffer ('\"');
786     while ((StringChar = input()) != EOF)
787     {
788         AslInsertLineBuffer (StringChar);
789 
790 DoCharacter:
791         switch (State)
792         {
793         case ASL_NORMAL_CHAR:
794 
795             switch (StringChar)
796             {
797             case '\\':
798                 /*
799                  * Special handling for backslash-escape sequence. We will
800                  * toss the backslash and translate the escape char(s).
801                  */
802                 State = ASL_ESCAPE_SEQUENCE;
803                 continue;
804 
805             case '\"':
806 
807                 /* String terminator */
808 
809                 goto CompletedString;
810 
811             default:
812 
813                 break;
814             }
815             break;
816 
817 
818         case ASL_ESCAPE_SEQUENCE:
819 
820             State = ASL_NORMAL_CHAR;
821             switch (StringChar)
822             {
823             case 'a':
824 
825                 StringChar = 0x07;      /* BELL */
826                 break;
827 
828             case 'b':
829 
830                 StringChar = 0x08;      /* BACKSPACE */
831                 break;
832 
833             case 'f':
834 
835                 StringChar = 0x0C;      /* FORMFEED */
836                 break;
837 
838             case 'n':
839 
840                 StringChar = 0x0A;      /* LINEFEED */
841                 break;
842 
843             case 'r':
844 
845                 StringChar = 0x0D;      /* CARRIAGE RETURN*/
846                 break;
847 
848             case 't':
849 
850                 StringChar = 0x09;      /* HORIZONTAL TAB */
851                 break;
852 
853             case 'v':
854 
855                 StringChar = 0x0B;      /* VERTICAL TAB */
856                 break;
857 
858             case 'x':
859 
860                 State = ASL_HEX_CONSTANT;
861                 i = 0;
862                 continue;
863 
864             case '\'':                  /* Single Quote */
865             case '\"':                  /* Double Quote */
866             case '\\':                  /* Backslash */
867 
868                 break;
869 
870             default:
871 
872                 /* Check for an octal digit (0-7) */
873 
874                 if (ACPI_IS_OCTAL_DIGIT (StringChar))
875                 {
876                     State = ASL_OCTAL_CONSTANT;
877                     ConvertBuffer[0] = StringChar;
878                     i = 1;
879                     continue;
880                 }
881 
882                 /* Unknown escape sequence issue warning, but use the character */
883 
884                 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_ESCAPE,
885                     Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
886                     Gbl_CurrentLineOffset, Gbl_CurrentColumn,
887                     Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
888                 break;
889             }
890             break;
891 
892 
893         case ASL_OCTAL_CONSTANT:
894 
895             /* Up to three octal digits allowed */
896 
897             if (!ACPI_IS_OCTAL_DIGIT (StringChar) ||
898                 (i > 2))
899             {
900                 /*
901                  * Reached end of the constant. Convert the assembled ASCII
902                  * string and resume processing of the next character
903                  */
904                 ConvertBuffer[i] = 0;
905                 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 8);
906 
907                 /* Check for NULL or non-ascii character (ignore if so) */
908 
909                 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
910                 {
911                     AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
912                         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
913                         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
914                         Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
915                 }
916                 else
917                 {
918                     *StringBuffer = (char) Digit;
919                     StringBuffer++;
920                     if (StringBuffer >= EndBuffer)
921                     {
922                         goto BufferOverflow;
923                     }
924                 }
925 
926                 State = ASL_NORMAL_CHAR;
927                 goto DoCharacter;
928                 break;
929             }
930 
931             /* Append another digit of the constant */
932 
933             ConvertBuffer[i] = StringChar;
934             i++;
935             continue;
936 
937         case ASL_HEX_CONSTANT:
938 
939             /* Up to two hex digits allowed */
940 
941             if (!isxdigit (StringChar) ||
942                 (i > 1))
943             {
944                 /*
945                  * Reached end of the constant. Convert the assembled ASCII
946                  * string and resume processing of the next character
947                  */
948                 ConvertBuffer[i] = 0;
949                 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 16);
950 
951                 /* Check for NULL or non-ascii character (ignore if so) */
952 
953                 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
954                 {
955                     AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
956                         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
957                         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
958                         Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
959                 }
960                 else
961                 {
962                     *StringBuffer = (char) Digit;
963                     StringBuffer++;
964                     if (StringBuffer >= EndBuffer)
965                     {
966                         goto BufferOverflow;
967                     }
968                 }
969 
970                 State = ASL_NORMAL_CHAR;
971                 goto DoCharacter;
972                 break;
973             }
974 
975             /* Append another digit of the constant */
976 
977             ConvertBuffer[i] = StringChar;
978             i++;
979             continue;
980 
981         default:
982 
983             break;
984         }
985 
986         /* Save the finished character */
987 
988         *StringBuffer = StringChar;
989         StringBuffer++;
990         if (StringBuffer >= EndBuffer)
991         {
992             goto BufferOverflow;
993         }
994     }
995 
996     /*
997      * Premature End-Of-File
998      */
999     AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
1000         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
1001         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
1002         Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
1003     return (FALSE);
1004 
1005 
1006 CompletedString:
1007     /*
1008      * Null terminate the input string and copy string to a new buffer
1009      */
1010     *StringBuffer = 0;
1011 
1012     CleanString = UtLocalCacheCalloc (strlen (MsgBuffer) + 1);
1013     strcpy (CleanString, MsgBuffer);
1014     AslCompilerlval.s = CleanString;
1015     return (TRUE);
1016 
1017 
1018 BufferOverflow:
1019 
1020     /* Literal was too long */
1021 
1022     AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
1023         Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
1024         Gbl_CurrentLineOffset, Gbl_CurrentColumn,
1025         Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");
1026     return (FALSE);
1027 }
1028