Subversion Repositories Kolibri OS

Rev

Rev 1498 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1498 serge 1
 
2
 *
3
 * Module Name: asfile - Main module for the acpi source processor utility
4
 *
5
 *****************************************************************************/
6
7
 
8
 *
9
 * 1. Copyright Notice
10
 *
11
 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
2216 Serge 12
 * All rights reserved.
1498 serge 13
 *
14
 * 2. License
15
 *
16
 * 2.1. This is your license from Intel Corp. under its intellectual property
17
 * rights.  You may have additional license terms from the party that provided
18
 * you this software, covering your right to use that party's intellectual
19
 * property rights.
20
 *
21
 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22
 * copy of the source code appearing in this file ("Covered Code") an
23
 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24
 * base code distributed originally by Intel ("Original Intel Code") to copy,
25
 * make derivatives, distribute, use and display any portion of the Covered
26
 * Code in any form, with the right to sublicense such rights; and
27
 *
28
 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29
 * license (with the right to sublicense), under only those claims of Intel
30
 * patents that are infringed by the Original Intel Code, to make, use, sell,
31
 * offer to sell, and import the Covered Code and derivative works thereof
32
 * solely to the minimum extent necessary to exercise the above copyright
33
 * license, and in no event shall the patent license extend to any additions
34
 * to or modifications of the Original Intel Code.  No other license or right
35
 * is granted directly or by implication, estoppel or otherwise;
36
 *
37
 * The above copyright and patent license is granted only if the following
38
 * conditions are met:
39
 *
40
 * 3. Conditions
41
 *
42
 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43
 * Redistribution of source code of any substantial portion of the Covered
44
 * Code or modification with rights to further distribute source must include
45
 * the above Copyright Notice, the above License, this list of Conditions,
46
 * and the following Disclaimer and Export Compliance provision.  In addition,
47
 * Licensee must cause all Covered Code to which Licensee contributes to
48
 * contain a file documenting the changes Licensee made to create that Covered
49
 * Code and the date of any change.  Licensee must include in that file the
50
 * documentation of any changes made by any predecessor Licensee.  Licensee
51
 * must include a prominent statement that the modification is derived,
52
 * directly or indirectly, from Original Intel Code.
53
 *
54
 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55
 * Redistribution of source code of any substantial portion of the Covered
56
 * Code or modification without rights to further distribute source must
57
 * include the following Disclaimer and Export Compliance provision in the
58
 * documentation and/or other materials provided with distribution.  In
59
 * addition, Licensee may not authorize further sublicense of source of any
60
 * portion of the Covered Code, and must include terms to the effect that the
61
 * license from Licensee to its licensee is limited to the intellectual
62
 * property embodied in the software Licensee provides to its licensee, and
63
 * not to intellectual property embodied in modifications its licensee may
64
 * make.
65
 *
66
 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67
 * substantial portion of the Covered Code or modification must reproduce the
68
 * above Copyright Notice, and the following Disclaimer and Export Compliance
69
 * provision in the documentation and/or other materials provided with the
70
 * distribution.
71
 *
72
 * 3.4. Intel retains all right, title, and interest in and to the Original
73
 * Intel Code.
74
 *
75
 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76
 * Intel shall be used in advertising or otherwise to promote the sale, use or
77
 * other dealings in products derived from or relating to the Covered Code
78
 * without prior written authorization from Intel.
79
 *
80
 * 4. Disclaimer and Export Compliance
81
 *
82
 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83
 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84
 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85
 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86
 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87
 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88
 * PARTICULAR PURPOSE.
89
 *
90
 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91
 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92
 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93
 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94
 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95
 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96
 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97
 * LIMITED REMEDY.
98
 *
99
 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100
 * software or system incorporating such software without first obtaining any
101
 * required license or other approval from the U. S. Department of Commerce or
102
 * any other agency or department of the United States Government.  In the
103
 * event Licensee exports any such software from the United States or
104
 * re-exports any such software from a foreign destination, Licensee shall
105
 * ensure that the distribution and export/re-export of the software is in
106
 * compliance with all laws, regulations, orders, or other restrictions of the
107
 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108
 * any of its subsidiaries will export/re-export any technical data, process,
109
 * software, or service, directly or indirectly, to any country for which the
110
 * United States government or any agency thereof requires an export license,
111
 * other governmental approval, or letter of assurance, without first obtaining
112
 * such license, approval or letter.
113
 *
114
 *****************************************************************************/
115
116
 
117
118
 
119
120
 
121
AsDoWildcard (
122
    ACPI_CONVERSION_TABLE   *ConversionTable,
123
    char                    *SourcePath,
124
    char                    *TargetPath,
125
    int                     MaxPathLength,
126
    int                     FileType,
127
    char                    *WildcardSpec);
128
129
 
130
AsDetectLoneLineFeeds (
131
    char                    *Filename,
132
    char                    *Buffer);
133
134
 
2216 Serge 135
AsMaxInt (int a, int b)
1498 serge 136
{
137
    return (a > b ? a : b);
138
}
139
140
 
141
 
142
 *
143
 * FUNCTION:    AsDoWildcard
144
 *
145
 * DESCRIPTION: Process files via wildcards
146
 *
147
 ******************************************************************************/
148
149
 
150
AsDoWildcard (
151
    ACPI_CONVERSION_TABLE   *ConversionTable,
152
    char                    *SourcePath,
153
    char                    *TargetPath,
154
    int                     MaxPathLength,
155
    int                     FileType,
156
    char                    *WildcardSpec)
157
{
158
    void                    *DirInfo;
159
    char                    *Filename;
160
    char                    *SourceDirPath;
161
    char                    *TargetDirPath;
162
    char                    RequestedFileType;
163
164
 
165
 
166
    {
167
        RequestedFileType = REQUEST_DIR_ONLY;
168
    }
169
    else
170
    {
171
        RequestedFileType = REQUEST_FILE_ONLY;
172
    }
173
174
 
175
            WildcardSpec, SourcePath));
176
177
 
178
179
 
180
    if (DirInfo)
181
    {
182
        /*
183
         * Get all of the files that match both the
184
         * wildcard and the requested file type
185
         */
186
        while ((Filename = AcpiOsGetNextFilename (DirInfo)))
187
        {
188
            /* Looking for directory files, must check file type */
189
190
 
191
            {
192
            case REQUEST_DIR_ONLY:
193
194
 
195
196
 
197
                        &SourceDirPath, &TargetDirPath))
198
                {
199
                    VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
200
201
 
202
                    free (SourceDirPath);
203
                    free (TargetDirPath);
204
                }
205
                break;
206
207
 
208
209
 
210
211
 
212
213
 
214
                        MaxPathLength, Filename, FileType);
215
                break;
216
217
 
218
                break;
219
            }
220
        }
221
222
 
223
224
 
225
    }
226
}
227
228
 
229
 
230
 *
231
 * FUNCTION:    AsProcessTree
232
 *
233
 * DESCRIPTION: Process the directory tree.  Files with the extension ".C" and
234
 *              ".H" are processed as the tree is traversed.
235
 *
236
 ******************************************************************************/
237
238
 
239
AsProcessTree (
240
    ACPI_CONVERSION_TABLE   *ConversionTable,
241
    char                    *SourcePath,
242
    char                    *TargetPath)
243
{
244
    int                     MaxPathLength;
245
246
 
247
 
248
249
 
250
    {
251
        if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
252
        {
253
            strlwr (TargetPath);
254
        }
255
256
 
257
        if (mkdir (TargetPath))
258
        {
259
            if (errno != EEXIST)
260
            {
261
                printf ("Could not create target directory\n");
262
                return -1;
263
            }
264
        }
265
    }
266
267
 
268
269
 
270
            FILE_TYPE_SOURCE, "*.c");
271
272
 
273
274
 
275
            FILE_TYPE_HEADER, "*.h");
276
277
 
278
279
 
280
            FILE_TYPE_SOURCE, "*.l");
281
282
 
283
284
 
285
            FILE_TYPE_SOURCE, "*.y");
286
287
 
288
289
 
290
            FILE_TYPE_HEADER, "*.asl");
291
292
 
293
294
 
295
            FILE_TYPE_DIRECTORY, "*");
296
297
 
298
}
299
300
 
301
 
302
 *
303
 * FUNCTION:    AsDetectLoneLineFeeds
304
 *
305
 * DESCRIPTION: Find LF without CR.
306
 *
307
 ******************************************************************************/
308
309
 
310
AsDetectLoneLineFeeds (
311
    char                    *Filename,
312
    char                    *Buffer)
313
{
314
    UINT32                  i = 1;
315
    UINT32                  LfCount = 0;
316
    UINT32                  LineCount = 0;
317
318
 
319
 
320
    {
321
        return FALSE;
322
    }
323
324
 
325
    {
326
        if (Buffer[i] == 0x0A)
327
        {
328
            if (Buffer[i-1] != 0x0D)
329
            {
330
                LfCount++;
331
            }
332
            LineCount++;
333
        }
334
        i++;
335
    }
336
337
 
338
    {
339
        if (LineCount == LfCount)
340
        {
341
            if (!Gbl_IgnoreLoneLineFeeds)
342
            {
343
                printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
344
                    Filename, LfCount);
345
            }
346
        }
347
        else
348
        {
349
            printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
350
        }
351
        return TRUE;
352
    }
353
354
 
355
}
356
357
 
358
 
359
 *
360
 * FUNCTION:    AsConvertFile
361
 *
362
 * DESCRIPTION: Perform the requested transforms on the file buffer (as
363
 *              determined by the ConversionTable and the FileType).
364
 *
365
 ******************************************************************************/
366
367
 
368
AsConvertFile (
369
    ACPI_CONVERSION_TABLE   *ConversionTable,
370
    char                    *FileBuffer,
371
    char                    *Filename,
372
    ACPI_NATIVE_INT         FileType)
373
{
374
    UINT32                  i;
375
    UINT32                  Functions;
376
    ACPI_STRING_TABLE       *StringTable;
377
    ACPI_IDENTIFIER_TABLE   *ConditionalTable;
378
    ACPI_IDENTIFIER_TABLE   *LineTable;
379
    ACPI_IDENTIFIER_TABLE   *MacroTable;
380
    ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
381
382
 
383
 
384
    {
385
    case FILE_TYPE_SOURCE:
386
        Functions           = ConversionTable->SourceFunctions;
387
        StringTable         = ConversionTable->SourceStringTable;
388
        LineTable           = ConversionTable->SourceLineTable;
389
        ConditionalTable    = ConversionTable->SourceConditionalTable;
390
        MacroTable          = ConversionTable->SourceMacroTable;
391
        StructTable         = ConversionTable->SourceStructTable;
392
       break;
393
394
 
395
        Functions           = ConversionTable->HeaderFunctions;
396
        StringTable         = ConversionTable->HeaderStringTable;
397
        LineTable           = ConversionTable->HeaderLineTable;
398
        ConditionalTable    = ConversionTable->HeaderConditionalTable;
399
        MacroTable          = ConversionTable->HeaderMacroTable;
400
        StructTable         = ConversionTable->HeaderStructTable;
401
        break;
402
403
 
404
        printf ("Unknown file type, cannot process\n");
405
        return;
406
    }
407
408
 
409
 
410
    Gbl_Files++;
411
    VERBOSE_PRINT (("Processing %u bytes\n",
2216 Serge 412
        (unsigned int) strlen (FileBuffer)));
413
1498 serge 414
 
415
    {
416
        for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
417
        {
418
            AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
419
                                FileBuffer);
420
        }
421
    }
422
423
 
424
425
 
426
    {
427
        for (i = 0; StringTable[i].Target; i++)
428
        {
429
            AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
430
                    StringTable[i].Type, FileBuffer);
431
        }
432
    }
433
434
 
435
    {
436
        for (i = 0; LineTable[i].Identifier; i++)
437
        {
438
            AsRemoveLine (FileBuffer, LineTable[i].Identifier);
439
        }
440
    }
441
442
 
443
    {
444
        for (i = 0; ConditionalTable[i].Identifier; i++)
445
        {
446
            AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
447
        }
448
    }
449
450
 
451
    {
452
        for (i = 0; MacroTable[i].Identifier; i++)
453
        {
454
            AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
455
        }
456
    }
457
458
 
459
    {
460
        for (i = 0; StructTable[i].Identifier; i++)
461
        {
462
            AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
463
        }
464
    }
465
466
 
467
468
 
469
    {
470
        /* Decode the function bitmap */
471
472
 
473
        {
474
        case 0:
475
            /* This function not configured */
476
            break;
477
478
 
479
 
480
481
 
482
            break;
483
484
 
485
 
486
487
 
488
            break;
489
490
 
491
 
492
493
 
494
            break;
495
496
 
497
 
498
499
 
500
            break;
501
502
 
503
 
504
505
 
506
            break;
507
508
 
509
 
510
511
 
512
            break;
513
514
 
515
 
516
517
 
518
            break;
519
520
 
521
 
522
523
 
524
            break;
525
526
 
527
 
528
529
 
530
            break;
531
532
 
533
 
534
535
 
536
            break;
537
538
 
539
 
540
541
 
542
            break;
543
544
 
545
 
546
547
 
548
            AsReduceTypedefs (FileBuffer, "typedef struct");
549
            break;
550
551
 
552
 
553
554
 
555
            break;
556
557
 
558
 
559
560
 
561
            break;
562
563
 
564
565
 
566
            AsTrimComments (FileBuffer, Filename);
567
#endif
568
            break;
569
570
 
571
572
 
573
            break;
574
        }
575
    }
576
577
 
578
    {
579
        AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
580
    }
581
}
582
583
 
584
 
585
 *
586
 * FUNCTION:    AsProcessOneFile
587
 *
588
 * DESCRIPTION: Process one source file.  The file is opened, read entirely
589
 *              into a buffer, converted, then written to a new file.
590
 *
591
 ******************************************************************************/
592
593
 
594
AsProcessOneFile (
595
    ACPI_CONVERSION_TABLE   *ConversionTable,
596
    char                    *SourcePath,
597
    char                    *TargetPath,
598
    int                     MaxPathLength,
599
    char                    *Filename,
600
    ACPI_NATIVE_INT         FileType)
601
{
602
    char                    *Pathname;
603
    char                    *OutPathname = NULL;
604
605
 
606
 
607
608
 
609
    if (!Pathname)
610
    {
611
        printf ("Could not allocate buffer for file pathnames\n");
612
        return -1;
613
    }
614
615
 
616
617
 
618
619
 
620
    {
621
        strcpy (Pathname, SourcePath);
622
        strcat (Pathname, "/");
623
    }
624
625
 
626
627
 
628
    {
629
        return -1;
630
    }
631
632
 
633
    if (strstr (Filename, ".asl"))
634
    {
635
        Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
636
    }
637
    else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
638
    {
639
        Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
640
    }
641
    else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
642
    {
643
        Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
644
    }
645
646
 
647
648
 
649
    if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
650
    {
651
        /*
652
         * All lone LFs will be converted to CR/LF
653
         * (when file is written, Windows version only)
654
         */
655
        printf ("Converting lone linefeeds\n");
656
        Gbl_MadeChanges = TRUE;
657
    }
658
659
 
660
661
 
662
    {
663
        if (!(Gbl_Overwrite && !Gbl_MadeChanges))
664
        {
665
            /* Generate the target pathname and write the file */
666
667
 
668
            if (!OutPathname)
669
            {
670
                printf ("Could not allocate buffer for file pathnames\n");
671
                return -1;
672
            }
673
674
 
675
            if (SourcePath)
676
            {
677
                strcat (OutPathname, "/");
678
                strcat (OutPathname, Filename);
679
            }
680
681
 
682
        }
683
    }
684
685
 
686
    free (Pathname);
687
    if (OutPathname)
688
    {
689
        free (OutPathname);
690
    }
691
692
 
693
}
694
695
 
696
 
697
 *
698
 * FUNCTION:    AsCheckForDirectory
699
 *
700
 * DESCRIPTION: Check if the current file is a valid directory.  If not,
701
 *              construct the full pathname for the source and target paths.
702
 *              Checks for the dot and dot-dot files (they are ignored)
703
 *
704
 ******************************************************************************/
705
706
 
707
AsCheckForDirectory (
708
    char                    *SourceDirPath,
709
    char                    *TargetDirPath,
710
    char                    *Filename,
711
    char                    **SourcePath,
712
    char                    **TargetPath)
713
{
714
    char                    *SrcPath;
715
    char                    *TgtPath;
716
717
 
718
 
719
        !(strcmp (Filename, "..")))
720
    {
721
        return -1;
722
    }
723
724
 
725
    if (!SrcPath)
726
    {
727
        printf ("Could not allocate buffer for directory source pathname\n");
728
        return -1;
729
    }
730
731
 
732
    if (!TgtPath)
733
    {
734
        printf ("Could not allocate buffer for directory target pathname\n");
735
        free (SrcPath);
736
        return -1;
737
    }
738
739
 
740
    strcat (SrcPath, "/");
741
    strcat (SrcPath, Filename);
742
743
 
744
    strcat (TgtPath, "/");
745
    strcat (TgtPath, Filename);
746
747
 
748
    *TargetPath = TgtPath;
749
    return 0;
750
}
751
752
 
753
 
754
 *
755
 * FUNCTION:    AsGetFile
756
 *
757
 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
758
 *
759
 ******************************************************************************/
760
761
 
762
AsGetFile (
763
    char                    *Filename,
764
    char                    **FileBuffer,
765
    UINT32                  *FileSize)
766
{
767
768
 
769
    UINT32                  Size;
770
    char                    *Buffer;
771
772
 
773
 
774
775
 
776
    if (!FileHandle)
777
    {
778
        printf ("Could not open %s\n", Filename);
779
        return -1;
780
    }
781
782
 
783
    {
784
        printf ("Could not get file status for %s\n", Filename);
785
        goto ErrorExit;
786
    }
787
788
 
789
     * Create a buffer for the entire file
790
     * Add plenty extra buffer to accomodate string replacements
791
     */
792
    Size = Gbl_StatBuf.st_size;
793
    Gbl_TotalSize += Size;
794
795
 
796
    if (!Buffer)
797
    {
798
        printf ("Could not allocate buffer of size %u\n", Size * 2);
799
        goto ErrorExit;
800
    }
801
802
 
803
804
 
805
    if (Size == -1)
806
    {
807
        printf ("Could not read the input file %s\n", Filename);
808
        goto ErrorExit;
809
    }
810
811
 
812
    close (FileHandle);
813
814
 
815
816
 
817
818
 
819
     * Convert all CR/LF pairs to LF only.  We do this locally so that
820
     * this code is portable across operating systems.
821
     */
822
    AsConvertToLineFeeds (Buffer);
823
824
 
825
    *FileSize = Size;
826
827
 
828
829
 
830
 
831
832
 
833
    return -1;
834
}
835
836
 
837
 
838
 *
839
 * FUNCTION:    AsPutFile
840
 *
841
 * DESCRIPTION: Create a new output file and write the entire contents of the
842
 *              buffer to the new file.  Buffer must be a zero terminated string
843
 *
844
 ******************************************************************************/
845
846
 
847
AsPutFile (
848
    char                    *Pathname,
849
    char                    *FileBuffer,
850
    UINT32                  SystemFlags)
851
{
852
    UINT32                  FileSize;
853
    int                     DestHandle;
854
    int                     OpenFlags;
855
856
 
857
 
858
859
 
860
861
 
862
    {
863
        /* Put back the CR before each LF */
864
865
 
866
    }
867
868
 
869
    if (DestHandle == -1)
870
    {
871
        perror ("Could not create destination file");
872
        printf ("Could not create destination file \"%s\"\n", Pathname);
873
        return -1;
874
    }
875
876
 
877
878
 
879
    write (DestHandle, FileBuffer, FileSize);
880
881
 
882
883
 
884
}
885