Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / asloptions.c
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             break;
444
445         case 'i':
446
447             /* Produce preprocessor output file */
448
449             Gbl_PreprocessorOutputFlag = TRUE;
450             break;
451
452         case 'm':
453
454             /* Produce hardware map summary file */
455
456             Gbl_MapfileFlag = TRUE;
457             break;
458
459         case 'n':
460
461             /* Produce namespace file */
462
463             Gbl_NsOutputFlag = TRUE;
464             break;
465
466         case 's':
467
468             /* Produce combined source file */
469
470             Gbl_SourceOutputFlag = TRUE;
471             break;
472
473         default:
474
475             printf ("Unknown option: -l%s\n", AcpiGbl_Optarg);
476             return (-1);
477         }
478         break;
479
480     case 'm':   /* Set line buffer size */
481
482         Gbl_LineBufferSize = (UINT32) strtoul (AcpiGbl_Optarg, NULL, 0) * 1024;
483         if (Gbl_LineBufferSize < ASL_DEFAULT_LINE_BUFFER_SIZE)
484         {
485             Gbl_LineBufferSize = ASL_DEFAULT_LINE_BUFFER_SIZE;
486         }
487         printf ("Line Buffer Size: %u\n", Gbl_LineBufferSize);
488         break;
489
490     case 'n':   /* Parse only */
491
492         Gbl_ParseOnlyFlag = TRUE;
493         break;
494
495     case 'o':   /* Control compiler AML optimizations */
496
497         switch (AcpiGbl_Optarg[0])
498         {
499         case 'a':
500
501             /* Disable all optimizations */
502
503             Gbl_FoldConstants = FALSE;
504             Gbl_IntegerOptimizationFlag = FALSE;
505             Gbl_ReferenceOptimizationFlag = FALSE;
506             break;
507
508         case 'f':
509
510             /* Disable folding on "normal" expressions */
511
512             Gbl_FoldConstants = FALSE;
513             break;
514
515         case 'i':
516
517             /* Disable integer optimization to constants */
518
519             Gbl_IntegerOptimizationFlag = FALSE;
520             break;
521
522         case 'n':
523
524             /* Disable named reference optimization */
525
526             Gbl_ReferenceOptimizationFlag = FALSE;
527             break;
528
529         case 't':
530
531             /* Display compile time(s) */
532
533             Gbl_CompileTimesFlag = TRUE;
534             break;
535
536         default:
537
538             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
539             return (-1);
540         }
541         break;
542
543     case 'P':   /* Preprocessor options */
544
545         switch (AcpiGbl_Optarg[0])
546         {
547         case '^':   /* Proprocess only, emit (.i) file */
548
549             Gbl_PreprocessOnly = TRUE;
550             Gbl_PreprocessorOutputFlag = TRUE;
551             break;
552
553         case 'n':   /* Disable preprocessor */
554
555             Gbl_PreprocessFlag = FALSE;
556             break;
557
558         default:
559
560             printf ("Unknown option: -P%s\n", AcpiGbl_Optarg);
561             return (-1);
562         }
563         break;
564
565     case 'p':   /* Override default AML output filename */
566
567         Gbl_OutputFilenamePrefix = AcpiGbl_Optarg;
568         UtConvertBackslashes (Gbl_OutputFilenamePrefix);
569         Gbl_UseDefaultAmlFilename = FALSE;
570         break;
571
572     case 'r':   /* Override revision found in table header */
573
574         Gbl_RevisionOverride = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
575         break;
576
577     case 's':   /* Create AML in a source code file */
578
579         switch (AcpiGbl_Optarg[0])
580         {
581         case 'a':
582
583             /* Produce assembly code output file */
584
585             Gbl_AsmOutputFlag = TRUE;
586             break;
587
588         case 'c':
589
590             /* Produce C hex output file */
591
592             Gbl_C_OutputFlag = TRUE;
593             break;
594
595         case 'o':
596
597             /* Produce AML offset table in C */
598
599             Gbl_C_OffsetTableFlag = TRUE;
600             break;
601
602         default:
603
604             printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
605             return (-1);
606         }
607         break;
608
609     case 't':   /* Produce hex table output file */
610
611         switch (AcpiGbl_Optarg[0])
612         {
613         case 'a':
614
615             Gbl_HexOutputFlag = HEX_OUTPUT_ASM;
616             break;
617
618         case 'c':
619
620             Gbl_HexOutputFlag = HEX_OUTPUT_C;
621             break;
622
623         case 's':
624
625             Gbl_HexOutputFlag = HEX_OUTPUT_ASL;
626             break;
627
628         default:
629
630             printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
631             return (-1);
632         }
633         break;
634
635     case 'T':   /* Create a ACPI table template file */
636
637         Gbl_DoTemplates = TRUE;
638         Gbl_TemplateSignature = AcpiGbl_Optarg;
639         break;
640
641     case 'v':   /* Version and verbosity settings */
642
643         switch (AcpiGbl_Optarg[0])
644         {
645         case '^':
646
647             printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
648             exit (0);
649
650         case 'a':
651
652             /* Disable all error/warning/remark messages */
653
654             Gbl_NoErrors = TRUE;
655             break;
656
657         case 'e':
658
659             /* Disable all warning/remark messages (errors only) */
660
661             Gbl_DisplayRemarks = FALSE;
662             Gbl_DisplayWarnings = FALSE;
663             break;
664
665         case 'i':
666             /*
667              * Support for integrated development environment(s).
668              *
669              * 1) No compiler signon
670              * 2) Send stderr messages to stdout
671              * 3) Less verbose error messages (single line only for each)
672              * 4) Error/warning messages are formatted appropriately to
673              *    be recognized by MS Visual Studio
674              */
675             Gbl_VerboseErrors = FALSE;
676             Gbl_DoSignon = FALSE;
677             Gbl_Files[ASL_FILE_STDERR].Handle = stdout;
678             break;
679
680         case 'o':
681
682             Gbl_DisplayOptimizations = TRUE;
683             break;
684
685         case 'r':
686
687             Gbl_DisplayRemarks = FALSE;
688             break;
689
690         case 's':
691
692             Gbl_DoSignon = FALSE;
693             break;
694
695         case 't':
696
697             Gbl_VerboseTemplates = TRUE;
698             break;
699
700         case 'w':
701
702             /* Get the required argument */
703
704             if (AcpiGetoptArgument (argc, argv))
705             {
706                 return (-1);
707             }
708
709             Status = AslDisableException (AcpiGbl_Optarg);
710             if (ACPI_FAILURE (Status))
711             {
712                 return (-1);
713             }
714             break;
715
716         default:
717
718             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
719             return (-1);
720         }
721         break;
722
723     case 'w': /* Set warning levels */
724
725         switch (AcpiGbl_Optarg[0])
726         {
727         case '1':
728
729             Gbl_WarningLevel = ASL_WARNING;
730             break;
731
732         case '2':
733
734             Gbl_WarningLevel = ASL_WARNING2;
735             break;
736
737         case '3':
738
739             Gbl_WarningLevel = ASL_WARNING3;
740             break;
741
742         case 'e':
743
744             Gbl_WarningsAsErrors = TRUE;
745             break;
746
747         default:
748
749             printf ("Unknown option: -w%s\n", AcpiGbl_Optarg);
750             return (-1);
751         }
752         break;
753
754     case 'x':   /* Set debug print output level */
755
756         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 16);
757         break;
758
759     case 'z':
760
761         Gbl_UseOriginalCompilerId = TRUE;
762         break;
763
764     default:
765
766         return (-1);
767     }
768
769     return (0);
770 }
771
772
773 /*******************************************************************************
774  *
775  * FUNCTION:    AslMergeOptionTokens
776  *
777  * PARAMETERS:  InBuffer            - Input containing an option string
778  *              OutBuffer           - Merged output buffer
779  *
780  * RETURN:      None
781  *
782  * DESCRIPTION: Remove all whitespace from an option string.
783  *
784  ******************************************************************************/
785
786 static void
787 AslMergeOptionTokens (
788     char                    *InBuffer,
789     char                    *OutBuffer)
790 {
791     char                    *Token;
792
793
794     *OutBuffer = 0;
795
796     Token = strtok (InBuffer, ASL_TOKEN_SEPARATORS);
797     while (Token)
798     {
799         strcat (OutBuffer, Token);
800         Token = strtok (NULL, ASL_TOKEN_SEPARATORS);
801     }
802 }
803
804
805 /*******************************************************************************
806  *
807  * FUNCTION:    AslDoResponseFile
808  *
809  * PARAMETERS:  Filename        - Name of the response file
810  *
811  * RETURN:      Status
812  *
813  * DESCRIPTION: Open a response file and process all options within.
814  *
815  ******************************************************************************/
816
817 static int
818 AslDoResponseFile (
819     char                    *Filename)
820 {
821     char                    *argv = StringBuffer2;
822     FILE                    *ResponseFile;
823     int                     OptStatus = 0;
824     int                     Opterr;
825     int                     Optind;
826
827
828     ResponseFile = fopen (Filename, "r");
829     if (!ResponseFile)
830     {
831         printf ("Could not open command file %s, %s\n",
832             Filename, strerror (errno));
833         return (-1);
834     }
835
836     /* Must save the current GetOpt globals */
837
838     Opterr = AcpiGbl_Opterr;
839     Optind = AcpiGbl_Optind;
840
841     /*
842      * Process all lines in the response file. There must be one complete
843      * option per line
844      */
845     while (fgets (StringBuffer, ASL_MSG_BUFFER_SIZE, ResponseFile))
846     {
847         /* Compress all tokens, allowing us to use a single argv entry */
848
849         AslMergeOptionTokens (StringBuffer, StringBuffer2);
850
851         /* Process the option */
852
853         AcpiGbl_Opterr = 0;
854         AcpiGbl_Optind = 0;
855
856         OptStatus = AslDoOptions (1, &argv, TRUE);
857         if (OptStatus)
858         {
859             printf ("Invalid option in command file %s: %s\n",
860                 Filename, StringBuffer);
861             break;
862         }
863     }
864
865     /* Restore the GetOpt globals */
866
867     AcpiGbl_Opterr = Opterr;
868     AcpiGbl_Optind = Optind;
869
870     fclose (ResponseFile);
871     return (OptStatus);
872 }