1 /******************************************************************************
2  *
3  * Module Name: asloptions - compiler command line processing
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "aslcompiler.h"
45 #include "acapps.h"
46 #include "acdisasm.h"
47 
48 #define _COMPONENT          ACPI_COMPILER
49         ACPI_MODULE_NAME    ("asloption")
50 
51 
52 /* Local prototypes */
53 
54 static int
55 AslDoOptions (
56     int                     argc,
57     char                    **argv,
58     BOOLEAN                 IsResponseFile);
59 
60 static void
61 AslMergeOptionTokens (
62     char                    *InBuffer,
63     char                    *OutBuffer);
64 
65 static int
66 AslDoResponseFile (
67     char                    *Filename);
68 
69 
70 #define ASL_TOKEN_SEPARATORS    " \t\n"
71 #define ASL_SUPPORTED_OPTIONS   "@:b|c|d^D:e:f^gh^i|I:l^m:no|p:P^r:s|t|T+G^v^w|x:z"
72 
73 
74 /*******************************************************************************
75  *
76  * FUNCTION:    AslCommandLine
77  *
78  * PARAMETERS:  argc/argv
79  *
80  * RETURN:      Last argv index
81  *
82  * DESCRIPTION: Command line processing
83  *
84  ******************************************************************************/
85 
86 int
87 AslCommandLine (
88     int                     argc,
89     char                    **argv)
90 {
91     int                     BadCommandLine = 0;
92     ACPI_STATUS             Status;
93 
94 
95     /* Minimum command line contains at least the command and an input file */
96 
97     if (argc < 2)
98     {
99         printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
100         Usage ();
101         exit (1);
102     }
103 
104     /* Process all command line options */
105 
106     BadCommandLine = AslDoOptions (argc, argv, FALSE);
107 
108     if (Gbl_DoTemplates)
109     {
110         Status = DtCreateTemplates (Gbl_TemplateSignature);
111         if (ACPI_FAILURE (Status))
112         {
113             exit (-1);
114         }
115         exit (1);
116     }
117 
118     /* Next parameter must be the input filename */
119 
120     if (!argv[AcpiGbl_Optind] &&
121         !Gbl_DisasmFlag)
122     {
123         printf ("Missing input filename\n");
124         BadCommandLine = TRUE;
125     }
126 
127     if (Gbl_DoSignon)
128     {
129         printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
130         if (Gbl_IgnoreErrors)
131         {
132             printf ("Ignoring all errors, forcing AML file generation\n\n");
133         }
134     }
135 
136     if (BadCommandLine)
137     {
138         printf ("Use -h option for help information\n");
139         exit (1);
140     }
141 
142     return (AcpiGbl_Optind);
143 }
144 
145 
146 /*******************************************************************************
147  *
148  * FUNCTION:    AslDoOptions
149  *
150  * PARAMETERS:  argc/argv           - Standard argc/argv
151  *              IsResponseFile      - TRUE if executing a response file.
152  *
153  * RETURN:      Status
154  *
155  * DESCRIPTION: Command line option processing
156  *
157  ******************************************************************************/
158 
159 static int
160 AslDoOptions (
161     int                     argc,
162     char                    **argv,
163     BOOLEAN                 IsResponseFile)
164 {
165     ACPI_STATUS             Status;
166     UINT32                  j;
167 
168 
169     /* Get the command line options */
170 
171     while ((j = AcpiGetopt (argc, argv, ASL_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
172     {
173     case '@':   /* Begin a response file */
174 
175         if (IsResponseFile)
176         {
177             printf ("Nested command files are not supported\n");
178             return (-1);
179         }
180 
181         if (AslDoResponseFile (AcpiGbl_Optarg))
182         {
183             return (-1);
184         }
185         break;
186 
187     case 'b':   /* Debug options */
188 
189         switch (AcpiGbl_Optarg[0])
190         {
191         case 'f':
192 
193             AslCompilerdebug = 1; /* same as yydebug */
194             DtParserdebug = 1;
195             PrParserdebug = 1;
196             Gbl_DebugFlag = TRUE;
197             break;
198 
199         case 'p':   /* Prune ASL parse tree */
200 
201             /* Get the required argument */
202 
203             if (AcpiGetoptArgument (argc, argv))
204             {
205                 return (-1);
206             }
207 
208             Gbl_PruneParseTree = TRUE;
209             Gbl_PruneDepth = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
210             break;
211 
212         case 's':
213 
214             Gbl_DebugFlag = TRUE;
215             break;
216 
217         case 't':
218 
219             /* Get the required argument */
220 
221             if (AcpiGetoptArgument (argc, argv))
222             {
223                 return (-1);
224             }
225 
226             Gbl_PruneType = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
227             break;
228 
229         default:
230 
231             printf ("Unknown option: -b%s\n", AcpiGbl_Optarg);
232             return (-1);
233         }
234 
235         break;
236 
237     case 'c':
238 
239         switch (AcpiGbl_Optarg[0])
240         {
241         case 'r':
242 
243             Gbl_NoResourceChecking = TRUE;
244             break;
245 
246         default:
247 
248             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
249             return (-1);
250         }
251         break;
252 
253     case 'd':   /* Disassembler */
254 
255         switch (AcpiGbl_Optarg[0])
256         {
257         case '^':
258 
259             Gbl_DoCompile = FALSE;
260             break;
261 
262         case 'a':
263 
264             Gbl_DoCompile = FALSE;
265             Gbl_DisassembleAll = TRUE;
266             break;
267 
268         case 'b':   /* Do not convert buffers to resource descriptors */
269 
270             AcpiGbl_NoResourceDisassembly = TRUE;
271             break;
272 
273         case 'c':
274 
275             break;
276 
277         case 'l':   /* Use legacy ASL code (not ASL+) for disassembly */
278 
279             Gbl_DoCompile = FALSE;
280             AcpiGbl_CstyleDisassembly = FALSE;
281             break;
282 
283         default:
284 
285             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
286             return (-1);
287         }
288 
289         Gbl_DisasmFlag = TRUE;
290         break;
291 
292     case 'D':   /* Define a symbol */
293 
294         PrAddDefine (AcpiGbl_Optarg, NULL, TRUE);
295         break;
296 
297     case 'e':   /* External files for disassembler */
298 
299         /* Get entire list of external files */
300 
301         AcpiGbl_Optind--;
302         argv[AcpiGbl_Optind] = AcpiGbl_Optarg;
303 
304         while (argv[AcpiGbl_Optind] &&
305               (argv[AcpiGbl_Optind][0] != '-'))
306         {
307             Status = AcpiDmAddToExternalFileList (argv[AcpiGbl_Optind]);
308             if (ACPI_FAILURE (Status))
309             {
310                 printf ("Could not add %s to external list\n", argv[AcpiGbl_Optind]);
311                 return (-1);
312             }
313 
314             AcpiGbl_Optind++;
315         }
316         break;
317 
318     case 'f':
319 
320         switch (AcpiGbl_Optarg[0])
321         {
322         case '^':   /* Ignore errors and force creation of aml file */
323 
324             Gbl_IgnoreErrors = TRUE;
325             break;
326 
327         case 'e':   /* Disassembler: Get external declaration file */
328 
329             if (AcpiGetoptArgument (argc, argv))
330             {
331                 return (-1);
332             }
333 
334             Gbl_ExternalRefFilename = AcpiGbl_Optarg;
335             break;
336 
337         default:
338 
339             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
340             return (-1);
341         }
342         break;
343 
344     case 'G':
345 
346         Gbl_CompileGeneric = TRUE;
347         break;
348 
349     case 'g':   /* Get all ACPI tables */
350 
351         printf ("-g option is deprecated, use acpidump utility instead\n");
352         exit (1);
353 
354     case 'h':
355 
356         switch (AcpiGbl_Optarg[0])
357         {
358         case '^':
359 
360             Usage ();
361             exit (0);
362 
363         case 'c':
364 
365             UtDisplayConstantOpcodes ();
366             exit (0);
367 
368         case 'f':
369 
370             AslFilenameHelp ();
371             exit (0);
372 
373         case 'r':
374 
375             /* reserved names */
376 
377             ApDisplayReservedNames ();
378             exit (0);
379 
380         case 't':
381 
382             UtDisplaySupportedTables ();
383             exit (0);
384 
385         default:
386 
387             printf ("Unknown option: -h%s\n", AcpiGbl_Optarg);
388             return (-1);
389         }
390 
391     case 'I':   /* Add an include file search directory */
392 
393         FlAddIncludeDirectory (AcpiGbl_Optarg);
394         break;
395 
396     case 'i':   /* Output AML as an include file */
397 
398         switch (AcpiGbl_Optarg[0])
399         {
400         case 'a':
401 
402             /* Produce assembly code include file */
403 
404             Gbl_AsmIncludeOutputFlag = TRUE;
405             break;
406 
407         case 'c':
408 
409             /* Produce C include file */
410 
411             Gbl_C_IncludeOutputFlag = TRUE;
412             break;
413 
414         case 'n':
415 
416             /* Compiler/Disassembler: Ignore the NOOP operator */
417 
418             AcpiGbl_IgnoreNoopOperator = TRUE;
419             break;
420 
421         default:
422 
423             printf ("Unknown option: -i%s\n", AcpiGbl_Optarg);
424             return (-1);
425         }
426         break;
427 
428     case 'l':   /* Listing files */
429 
430         switch (AcpiGbl_Optarg[0])
431         {
432         case '^':
433 
434             /* Produce listing file (Mixed source/aml) */
435 
436             Gbl_ListingFlag = TRUE;
437             break;
438 
439         case 'i':
440 
441             /* Produce preprocessor output file */
442 
443             Gbl_PreprocessorOutputFlag = TRUE;
444             break;
445 
446         case 'm':
447 
448             /* Produce hardware map summary file */
449 
450             Gbl_MapfileFlag = TRUE;
451             break;
452 
453         case 'n':
454 
455             /* Produce namespace file */
456 
457             Gbl_NsOutputFlag = TRUE;
458             break;
459 
460         case 's':
461 
462             /* Produce combined source file */
463 
464             Gbl_SourceOutputFlag = TRUE;
465             break;
466 
467         default:
468 
469             printf ("Unknown option: -l%s\n", AcpiGbl_Optarg);
470             return (-1);
471         }
472         break;
473 
474     case 'm':   /* Set line buffer size */
475 
476         Gbl_LineBufferSize = (UINT32) strtoul (AcpiGbl_Optarg, NULL, 0) * 1024;
477         if (Gbl_LineBufferSize < ASL_DEFAULT_LINE_BUFFER_SIZE)
478         {
479             Gbl_LineBufferSize = ASL_DEFAULT_LINE_BUFFER_SIZE;
480         }
481         printf ("Line Buffer Size: %u\n", Gbl_LineBufferSize);
482         break;
483 
484     case 'n':   /* Parse only */
485 
486         Gbl_ParseOnlyFlag = TRUE;
487         break;
488 
489     case 'o':   /* Control compiler AML optimizations */
490 
491         switch (AcpiGbl_Optarg[0])
492         {
493         case 'a':
494 
495             /* Disable all optimizations */
496 
497             Gbl_FoldConstants = FALSE;
498             Gbl_IntegerOptimizationFlag = FALSE;
499             Gbl_ReferenceOptimizationFlag = FALSE;
500             break;
501 
502         case 'f':
503 
504             /* Disable folding on "normal" expressions */
505 
506             Gbl_FoldConstants = FALSE;
507             break;
508 
509         case 'i':
510 
511             /* Disable integer optimization to constants */
512 
513             Gbl_IntegerOptimizationFlag = FALSE;
514             break;
515 
516         case 'n':
517 
518             /* Disable named reference optimization */
519 
520             Gbl_ReferenceOptimizationFlag = FALSE;
521             break;
522 
523         case 't':
524 
525             /* Display compile time(s) */
526 
527             Gbl_CompileTimesFlag = TRUE;
528             break;
529 
530         default:
531 
532             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
533             return (-1);
534         }
535         break;
536 
537     case 'P':   /* Preprocessor options */
538 
539         switch (AcpiGbl_Optarg[0])
540         {
541         case '^':   /* Proprocess only, emit (.i) file */
542 
543             Gbl_PreprocessOnly = TRUE;
544             Gbl_PreprocessorOutputFlag = TRUE;
545             break;
546 
547         case 'n':   /* Disable preprocessor */
548 
549             Gbl_PreprocessFlag = FALSE;
550             break;
551 
552         default:
553 
554             printf ("Unknown option: -P%s\n", AcpiGbl_Optarg);
555             return (-1);
556         }
557         break;
558 
559     case 'p':   /* Override default AML output filename */
560 
561         Gbl_OutputFilenamePrefix = AcpiGbl_Optarg;
562         UtConvertBackslashes (Gbl_OutputFilenamePrefix);
563         Gbl_UseDefaultAmlFilename = FALSE;
564         break;
565 
566     case 'r':   /* Override revision found in table header */
567 
568         Gbl_RevisionOverride = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
569         break;
570 
571     case 's':   /* Create AML in a source code file */
572 
573         switch (AcpiGbl_Optarg[0])
574         {
575         case 'a':
576 
577             /* Produce assembly code output file */
578 
579             Gbl_AsmOutputFlag = TRUE;
580             break;
581 
582         case 'c':
583 
584             /* Produce C hex output file */
585 
586             Gbl_C_OutputFlag = TRUE;
587             break;
588 
589         case 'o':
590 
591             /* Produce AML offset table in C */
592 
593             Gbl_C_OffsetTableFlag = TRUE;
594             break;
595 
596         default:
597 
598             printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
599             return (-1);
600         }
601         break;
602 
603     case 't':   /* Produce hex table output file */
604 
605         switch (AcpiGbl_Optarg[0])
606         {
607         case 'a':
608 
609             Gbl_HexOutputFlag = HEX_OUTPUT_ASM;
610             break;
611 
612         case 'c':
613 
614             Gbl_HexOutputFlag = HEX_OUTPUT_C;
615             break;
616 
617         case 's':
618 
619             Gbl_HexOutputFlag = HEX_OUTPUT_ASL;
620             break;
621 
622         default:
623 
624             printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
625             return (-1);
626         }
627         break;
628 
629     case 'T':   /* Create a ACPI table template file */
630 
631         Gbl_DoTemplates = TRUE;
632         Gbl_TemplateSignature = AcpiGbl_Optarg;
633         break;
634 
635     case 'v':   /* Version and verbosity settings */
636 
637         switch (AcpiGbl_Optarg[0])
638         {
639         case '^':
640 
641             printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
642             exit (0);
643 
644         case 'a':
645 
646             /* Disable all error/warning/remark messages */
647 
648             Gbl_NoErrors = TRUE;
649             break;
650 
651         case 'e':
652 
653             /* Disable all warning/remark messages (errors only) */
654 
655             Gbl_DisplayRemarks = FALSE;
656             Gbl_DisplayWarnings = FALSE;
657             break;
658 
659         case 'i':
660             /*
661              * Support for integrated development environment(s).
662              *
663              * 1) No compiler signon
664              * 2) Send stderr messages to stdout
665              * 3) Less verbose error messages (single line only for each)
666              * 4) Error/warning messages are formatted appropriately to
667              *    be recognized by MS Visual Studio
668              */
669             Gbl_VerboseErrors = FALSE;
670             Gbl_DoSignon = FALSE;
671             Gbl_Files[ASL_FILE_STDERR].Handle = stdout;
672             break;
673 
674         case 'o':
675 
676             Gbl_DisplayOptimizations = TRUE;
677             break;
678 
679         case 'r':
680 
681             Gbl_DisplayRemarks = FALSE;
682             break;
683 
684         case 's':
685 
686             Gbl_DoSignon = FALSE;
687             break;
688 
689         case 't':
690 
691             Gbl_VerboseTemplates = TRUE;
692             break;
693 
694         case 'w':
695 
696             /* Get the required argument */
697 
698             if (AcpiGetoptArgument (argc, argv))
699             {
700                 return (-1);
701             }
702 
703             Status = AslDisableException (AcpiGbl_Optarg);
704             if (ACPI_FAILURE (Status))
705             {
706                 return (-1);
707             }
708             break;
709 
710         default:
711 
712             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
713             return (-1);
714         }
715         break;
716 
717     case 'w': /* Set warning levels */
718 
719         switch (AcpiGbl_Optarg[0])
720         {
721         case '1':
722 
723             Gbl_WarningLevel = ASL_WARNING;
724             break;
725 
726         case '2':
727 
728             Gbl_WarningLevel = ASL_WARNING2;
729             break;
730 
731         case '3':
732 
733             Gbl_WarningLevel = ASL_WARNING3;
734             break;
735 
736         case 'e':
737 
738             Gbl_WarningsAsErrors = TRUE;
739             break;
740 
741         default:
742 
743             printf ("Unknown option: -w%s\n", AcpiGbl_Optarg);
744             return (-1);
745         }
746         break;
747 
748     case 'x':   /* Set debug print output level */
749 
750         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 16);
751         break;
752 
753     case 'z':
754 
755         Gbl_UseOriginalCompilerId = TRUE;
756         break;
757 
758     default:
759 
760         return (-1);
761     }
762 
763     return (0);
764 }
765 
766 
767 /*******************************************************************************
768  *
769  * FUNCTION:    AslMergeOptionTokens
770  *
771  * PARAMETERS:  InBuffer            - Input containing an option string
772  *              OutBuffer           - Merged output buffer
773  *
774  * RETURN:      None
775  *
776  * DESCRIPTION: Remove all whitespace from an option string.
777  *
778  ******************************************************************************/
779 
780 static void
781 AslMergeOptionTokens (
782     char                    *InBuffer,
783     char                    *OutBuffer)
784 {
785     char                    *Token;
786 
787 
788     *OutBuffer = 0;
789 
790     Token = strtok (InBuffer, ASL_TOKEN_SEPARATORS);
791     while (Token)
792     {
793         strcat (OutBuffer, Token);
794         Token = strtok (NULL, ASL_TOKEN_SEPARATORS);
795     }
796 }
797 
798 
799 /*******************************************************************************
800  *
801  * FUNCTION:    AslDoResponseFile
802  *
803  * PARAMETERS:  Filename        - Name of the response file
804  *
805  * RETURN:      Status
806  *
807  * DESCRIPTION: Open a response file and process all options within.
808  *
809  ******************************************************************************/
810 
811 static int
812 AslDoResponseFile (
813     char                    *Filename)
814 {
815     char                    *argv = StringBuffer2;
816     FILE                    *ResponseFile;
817     int                     OptStatus = 0;
818     int                     Opterr;
819     int                     Optind;
820 
821 
822     ResponseFile = fopen (Filename, "r");
823     if (!ResponseFile)
824     {
825         printf ("Could not open command file %s, %s\n",
826             Filename, strerror (errno));
827         return (-1);
828     }
829 
830     /* Must save the current GetOpt globals */
831 
832     Opterr = AcpiGbl_Opterr;
833     Optind = AcpiGbl_Optind;
834 
835     /*
836      * Process all lines in the response file. There must be one complete
837      * option per line
838      */
839     while (fgets (StringBuffer, ASL_MSG_BUFFER_SIZE, ResponseFile))
840     {
841         /* Compress all tokens, allowing us to use a single argv entry */
842 
843         AslMergeOptionTokens (StringBuffer, StringBuffer2);
844 
845         /* Process the option */
846 
847         AcpiGbl_Opterr = 0;
848         AcpiGbl_Optind = 0;
849 
850         OptStatus = AslDoOptions (1, &argv, TRUE);
851         if (OptStatus)
852         {
853             printf ("Invalid option in command file %s: %s\n",
854                 Filename, StringBuffer);
855             break;
856         }
857     }
858 
859     /* Restore the GetOpt globals */
860 
861     AcpiGbl_Opterr = Opterr;
862     AcpiGbl_Optind = Optind;
863 
864     fclose (ResponseFile);
865     return (OptStatus);
866 }
867