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