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