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