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