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   "@:a: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 'a':   /* Debug options */
188 
189         switch (AcpiGbl_Optarg[0])
190         {
191         case 'r':
192 
193             Gbl_EnableReferenceTypechecking = TRUE;
194             break;
195 
196         default:
197 
198             printf ("Unknown option: -a%s\n", AcpiGbl_Optarg);
199             return (-1);
200         }
201 
202         break;
203 
204 
205     case 'b':   /* Debug options */
206 
207         switch (AcpiGbl_Optarg[0])
208         {
209         case 'f':
210 
211             AslCompilerdebug = 1; /* same as yydebug */
212             DtParserdebug = 1;
213             PrParserdebug = 1;
214             Gbl_DebugFlag = TRUE;
215             Gbl_KeepPreprocessorTempFile = TRUE;
216             break;
217 
218         case 'p':   /* Prune ASL parse tree */
219 
220             /* Get the required argument */
221 
222             if (AcpiGetoptArgument (argc, argv))
223             {
224                 return (-1);
225             }
226 
227             Gbl_PruneParseTree = TRUE;
228             Gbl_PruneDepth = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
229             break;
230 
231         case 's':
232 
233             Gbl_DebugFlag = TRUE;
234             break;
235 
236         case 't':
237 
238             /* Get the required argument */
239 
240             if (AcpiGetoptArgument (argc, argv))
241             {
242                 return (-1);
243             }
244 
245             Gbl_PruneType = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
246             break;
247 
248         default:
249 
250             printf ("Unknown option: -b%s\n", AcpiGbl_Optarg);
251             return (-1);
252         }
253 
254         break;
255 
256     case 'c':
257 
258         switch (AcpiGbl_Optarg[0])
259         {
260         case 'r':
261 
262             Gbl_NoResourceChecking = TRUE;
263             break;
264 
265         default:
266 
267             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
268             return (-1);
269         }
270         break;
271 
272     case 'd':   /* Disassembler */
273 
274         switch (AcpiGbl_Optarg[0])
275         {
276         case '^':
277 
278             Gbl_DoCompile = FALSE;
279             break;
280 
281         case 'a':
282 
283             Gbl_DoCompile = FALSE;
284             Gbl_DisassembleAll = TRUE;
285             break;
286 
287         case 'b':   /* Do not convert buffers to resource descriptors */
288 
289             AcpiGbl_NoResourceDisassembly = TRUE;
290             break;
291 
292         case 'c':
293 
294             break;
295 
296         case 'f':
297 
298             AcpiGbl_ForceAmlDisassembly = TRUE;
299             break;
300 
301         case 'l':   /* Use legacy ASL code (not ASL+) for disassembly */
302 
303             Gbl_DoCompile = FALSE;
304             AcpiGbl_CstyleDisassembly = FALSE;
305             break;
306 
307         default:
308 
309             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
310             return (-1);
311         }
312 
313         Gbl_DisasmFlag = TRUE;
314         break;
315 
316     case 'D':   /* Define a symbol */
317 
318         PrAddDefine (AcpiGbl_Optarg, NULL, TRUE);
319         break;
320 
321     case 'e':   /* External files for disassembler */
322 
323         /* Get entire list of external files */
324 
325         AcpiGbl_Optind--;
326         argv[AcpiGbl_Optind] = AcpiGbl_Optarg;
327 
328         while (argv[AcpiGbl_Optind] &&
329               (argv[AcpiGbl_Optind][0] != '-'))
330         {
331             Status = AcpiDmAddToExternalFileList (argv[AcpiGbl_Optind]);
332             if (ACPI_FAILURE (Status))
333             {
334                 printf ("Could not add %s to external list\n", argv[AcpiGbl_Optind]);
335                 return (-1);
336             }
337 
338             AcpiGbl_Optind++;
339         }
340         break;
341 
342     case 'f':
343 
344         switch (AcpiGbl_Optarg[0])
345         {
346         case '^':   /* Ignore errors and force creation of aml file */
347 
348             Gbl_IgnoreErrors = TRUE;
349             break;
350 
351         case 'e':   /* Disassembler: Get external declaration file */
352 
353             if (AcpiGetoptArgument (argc, argv))
354             {
355                 return (-1);
356             }
357 
358             Gbl_ExternalRefFilename = AcpiGbl_Optarg;
359             break;
360 
361         default:
362 
363             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
364             return (-1);
365         }
366         break;
367 
368     case 'G':
369 
370         Gbl_CompileGeneric = TRUE;
371         break;
372 
373     case 'g':   /* Get all ACPI tables */
374 
375         printf ("-g option is deprecated, use acpidump utility instead\n");
376         exit (1);
377 
378     case 'h':
379 
380         switch (AcpiGbl_Optarg[0])
381         {
382         case '^':
383 
384             Usage ();
385             exit (0);
386 
387         case 'c':
388 
389             UtDisplayConstantOpcodes ();
390             exit (0);
391 
392         case 'f':
393 
394             AslFilenameHelp ();
395             exit (0);
396 
397         case 'r':
398 
399             /* reserved names */
400 
401             ApDisplayReservedNames ();
402             exit (0);
403 
404         case 't':
405 
406             UtDisplaySupportedTables ();
407             exit (0);
408 
409         default:
410 
411             printf ("Unknown option: -h%s\n", AcpiGbl_Optarg);
412             return (-1);
413         }
414 
415     case 'I':   /* Add an include file search directory */
416 
417         FlAddIncludeDirectory (AcpiGbl_Optarg);
418         break;
419 
420     case 'i':   /* Output AML as an include file */
421 
422         switch (AcpiGbl_Optarg[0])
423         {
424         case 'a':
425 
426             /* Produce assembly code include file */
427 
428             Gbl_AsmIncludeOutputFlag = TRUE;
429             break;
430 
431         case 'c':
432 
433             /* Produce C include file */
434 
435             Gbl_C_IncludeOutputFlag = TRUE;
436             break;
437 
438         case 'n':
439 
440             /* Compiler/Disassembler: Ignore the NOOP operator */
441 
442             AcpiGbl_IgnoreNoopOperator = TRUE;
443             break;
444 
445         default:
446 
447             printf ("Unknown option: -i%s\n", AcpiGbl_Optarg);
448             return (-1);
449         }
450         break;
451 
452     case 'l':   /* Listing files */
453 
454         switch (AcpiGbl_Optarg[0])
455         {
456         case '^':
457 
458             /* Produce listing file (Mixed source/aml) */
459 
460             Gbl_ListingFlag = TRUE;
461             AcpiGbl_DmOpt_Listing = TRUE;
462             break;
463 
464         case 'i':
465 
466             /* Produce preprocessor output file */
467 
468             Gbl_PreprocessorOutputFlag = TRUE;
469             break;
470 
471         case 'm':
472 
473             /* Produce hardware map summary file */
474 
475             Gbl_MapfileFlag = TRUE;
476             break;
477 
478         case 'n':
479 
480             /* Produce namespace file */
481 
482             Gbl_NsOutputFlag = TRUE;
483             break;
484 
485         case 's':
486 
487             /* Produce combined source file */
488 
489             Gbl_SourceOutputFlag = TRUE;
490             break;
491 
492         default:
493 
494             printf ("Unknown option: -l%s\n", AcpiGbl_Optarg);
495             return (-1);
496         }
497         break;
498 
499     case 'm':   /* Set line buffer size */
500 
501         Gbl_LineBufferSize = (UINT32) strtoul (AcpiGbl_Optarg, NULL, 0) * 1024;
502         if (Gbl_LineBufferSize < ASL_DEFAULT_LINE_BUFFER_SIZE)
503         {
504             Gbl_LineBufferSize = ASL_DEFAULT_LINE_BUFFER_SIZE;
505         }
506         printf ("Line Buffer Size: %u\n", Gbl_LineBufferSize);
507         break;
508 
509     case 'n':   /* Parse only */
510 
511         Gbl_ParseOnlyFlag = TRUE;
512         break;
513 
514     case 'o':   /* Control compiler AML optimizations */
515 
516         switch (AcpiGbl_Optarg[0])
517         {
518         case 'a':
519 
520             /* Disable all optimizations */
521 
522             Gbl_FoldConstants = FALSE;
523             Gbl_IntegerOptimizationFlag = FALSE;
524             Gbl_ReferenceOptimizationFlag = FALSE;
525             break;
526 
527         case 'c':
528 
529             /* Display compile time(s) */
530 
531             Gbl_CompileTimesFlag = TRUE;
532             break;
533 
534         case 'f':
535 
536             /* Disable folding on "normal" expressions */
537 
538             Gbl_FoldConstants = FALSE;
539             break;
540 
541         case 'i':
542 
543             /* Disable integer optimization to constants */
544 
545             Gbl_IntegerOptimizationFlag = FALSE;
546             break;
547 
548         case 'n':
549 
550             /* Disable named reference optimization */
551 
552             Gbl_ReferenceOptimizationFlag = FALSE;
553             break;
554 
555         case 't':
556 
557             /* Disable heavy typechecking */
558 
559             Gbl_DoTypechecking = FALSE;
560             break;
561 
562         default:
563 
564             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
565             return (-1);
566         }
567         break;
568 
569     case 'P':   /* Preprocessor options */
570 
571         switch (AcpiGbl_Optarg[0])
572         {
573         case '^':   /* Proprocess only, emit (.i) file */
574 
575             Gbl_PreprocessOnly = TRUE;
576             Gbl_PreprocessorOutputFlag = TRUE;
577             break;
578 
579         case 'n':   /* Disable preprocessor */
580 
581             Gbl_PreprocessFlag = FALSE;
582             break;
583 
584         default:
585 
586             printf ("Unknown option: -P%s\n", AcpiGbl_Optarg);
587             return (-1);
588         }
589         break;
590 
591     case 'p':   /* Override default AML output filename */
592 
593         Gbl_OutputFilenamePrefix = AcpiGbl_Optarg;
594         UtConvertBackslashes (Gbl_OutputFilenamePrefix);
595         Gbl_UseDefaultAmlFilename = FALSE;
596         break;
597 
598     case 'r':   /* Override revision found in table header */
599 
600         Gbl_RevisionOverride = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
601         break;
602 
603     case 's':   /* Create AML in a source code file */
604 
605         switch (AcpiGbl_Optarg[0])
606         {
607         case 'a':
608 
609             /* Produce assembly code output file */
610 
611             Gbl_AsmOutputFlag = TRUE;
612             break;
613 
614         case 'c':
615 
616             /* Produce C hex output file */
617 
618             Gbl_C_OutputFlag = TRUE;
619             break;
620 
621         case 'o':
622 
623             /* Produce AML offset table in C */
624 
625             Gbl_C_OffsetTableFlag = TRUE;
626             break;
627 
628         default:
629 
630             printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
631             return (-1);
632         }
633         break;
634 
635     case 't':   /* Produce hex table output file */
636 
637         switch (AcpiGbl_Optarg[0])
638         {
639         case 'a':
640 
641             Gbl_HexOutputFlag = HEX_OUTPUT_ASM;
642             break;
643 
644         case 'c':
645 
646             Gbl_HexOutputFlag = HEX_OUTPUT_C;
647             break;
648 
649         case 's':
650 
651             Gbl_HexOutputFlag = HEX_OUTPUT_ASL;
652             break;
653 
654         default:
655 
656             printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
657             return (-1);
658         }
659         break;
660 
661     case 'T':   /* Create a ACPI table template file */
662 
663         Gbl_DoTemplates = TRUE;
664         Gbl_TemplateSignature = AcpiGbl_Optarg;
665         break;
666 
667     case 'v':   /* Version and verbosity settings */
668 
669         switch (AcpiGbl_Optarg[0])
670         {
671         case '^':
672 
673             printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
674             exit (0);
675 
676         case 'a':
677 
678             /* Disable all error/warning/remark messages */
679 
680             Gbl_NoErrors = TRUE;
681             break;
682 
683         case 'e':
684 
685             /* Disable all warning/remark messages (errors only) */
686 
687             Gbl_DisplayRemarks = FALSE;
688             Gbl_DisplayWarnings = FALSE;
689             break;
690 
691         case 'i':
692             /*
693              * Support for integrated development environment(s).
694              *
695              * 1) No compiler signon
696              * 2) Send stderr messages to stdout
697              * 3) Less verbose error messages (single line only for each)
698              * 4) Error/warning messages are formatted appropriately to
699              *    be recognized by MS Visual Studio
700              */
701             Gbl_VerboseErrors = FALSE;
702             Gbl_DoSignon = FALSE;
703             Gbl_Files[ASL_FILE_STDERR].Handle = stdout;
704             break;
705 
706         case 'o':
707 
708             Gbl_DisplayOptimizations = TRUE;
709             break;
710 
711         case 'r':
712 
713             Gbl_DisplayRemarks = FALSE;
714             break;
715 
716         case 's':
717 
718             Gbl_DoSignon = FALSE;
719             break;
720 
721         case 't':
722 
723             Gbl_VerboseTemplates = TRUE;
724             break;
725 
726         case 'w':
727 
728             /* Get the required argument */
729 
730             if (AcpiGetoptArgument (argc, argv))
731             {
732                 return (-1);
733             }
734 
735             Status = AslDisableException (AcpiGbl_Optarg);
736             if (ACPI_FAILURE (Status))
737             {
738                 return (-1);
739             }
740             break;
741 
742         default:
743 
744             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
745             return (-1);
746         }
747         break;
748 
749     case 'w': /* Set warning levels */
750 
751         switch (AcpiGbl_Optarg[0])
752         {
753         case '1':
754 
755             Gbl_WarningLevel = ASL_WARNING;
756             break;
757 
758         case '2':
759 
760             Gbl_WarningLevel = ASL_WARNING2;
761             break;
762 
763         case '3':
764 
765             Gbl_WarningLevel = ASL_WARNING3;
766             break;
767 
768         case 'e':
769 
770             Gbl_WarningsAsErrors = TRUE;
771             break;
772 
773         default:
774 
775             printf ("Unknown option: -w%s\n", AcpiGbl_Optarg);
776             return (-1);
777         }
778         break;
779 
780     case 'x':   /* Set debug print output level */
781 
782         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 16);
783         break;
784 
785     case 'z':
786 
787         Gbl_UseOriginalCompilerId = TRUE;
788         break;
789 
790     default:
791 
792         return (-1);
793     }
794 
795     return (0);
796 }
797 
798 
799 /*******************************************************************************
800  *
801  * FUNCTION:    AslMergeOptionTokens
802  *
803  * PARAMETERS:  InBuffer            - Input containing an option string
804  *              OutBuffer           - Merged output buffer
805  *
806  * RETURN:      None
807  *
808  * DESCRIPTION: Remove all whitespace from an option string.
809  *
810  ******************************************************************************/
811 
812 static void
813 AslMergeOptionTokens (
814     char                    *InBuffer,
815     char                    *OutBuffer)
816 {
817     char                    *Token;
818 
819 
820     *OutBuffer = 0;
821 
822     Token = strtok (InBuffer, ASL_TOKEN_SEPARATORS);
823     while (Token)
824     {
825         strcat (OutBuffer, Token);
826         Token = strtok (NULL, ASL_TOKEN_SEPARATORS);
827     }
828 }
829 
830 
831 /*******************************************************************************
832  *
833  * FUNCTION:    AslDoResponseFile
834  *
835  * PARAMETERS:  Filename        - Name of the response file
836  *
837  * RETURN:      Status
838  *
839  * DESCRIPTION: Open a response file and process all options within.
840  *
841  ******************************************************************************/
842 
843 static int
844 AslDoResponseFile (
845     char                    *Filename)
846 {
847     char                    *argv = StringBuffer2;
848     FILE                    *ResponseFile;
849     int                     OptStatus = 0;
850     int                     Opterr;
851     int                     Optind;
852 
853 
854     ResponseFile = fopen (Filename, "r");
855     if (!ResponseFile)
856     {
857         printf ("Could not open command file %s, %s\n",
858             Filename, strerror (errno));
859         return (-1);
860     }
861 
862     /* Must save the current GetOpt globals */
863 
864     Opterr = AcpiGbl_Opterr;
865     Optind = AcpiGbl_Optind;
866 
867     /*
868      * Process all lines in the response file. There must be one complete
869      * option per line
870      */
871     while (fgets (StringBuffer, ASL_MSG_BUFFER_SIZE, ResponseFile))
872     {
873         /* Compress all tokens, allowing us to use a single argv entry */
874 
875         AslMergeOptionTokens (StringBuffer, StringBuffer2);
876 
877         /* Process the option */
878 
879         AcpiGbl_Opterr = 0;
880         AcpiGbl_Optind = 0;
881 
882         OptStatus = AslDoOptions (1, &argv, TRUE);
883         if (OptStatus)
884         {
885             printf ("Invalid option in command file %s: %s\n",
886                 Filename, StringBuffer);
887             break;
888         }
889     }
890 
891     /* Restore the GetOpt globals */
892 
893     AcpiGbl_Opterr = Opterr;
894     AcpiGbl_Optind = Optind;
895 
896     fclose (ResponseFile);
897     return (OptStatus);
898 }
899