Home | History | Annotate | Line # | Download | only in acpisrc
      1       1.1    jruoho /******************************************************************************
      2       1.1    jruoho  *
      3       1.1    jruoho  * Module Name: asconvrt - Source conversion code
      4       1.1    jruoho  *
      5       1.1    jruoho  *****************************************************************************/
      6       1.1    jruoho 
      7  1.1.1.16  christos /******************************************************************************
      8  1.1.1.16  christos  *
      9  1.1.1.16  christos  * 1. Copyright Notice
     10  1.1.1.16  christos  *
     11  1.1.1.17  christos  * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.
     12       1.1    jruoho  * All rights reserved.
     13       1.1    jruoho  *
     14  1.1.1.16  christos  * 2. License
     15  1.1.1.16  christos  *
     16  1.1.1.16  christos  * 2.1. This is your license from Intel Corp. under its intellectual property
     17  1.1.1.16  christos  * rights. You may have additional license terms from the party that provided
     18  1.1.1.16  christos  * you this software, covering your right to use that party's intellectual
     19  1.1.1.16  christos  * property rights.
     20  1.1.1.16  christos  *
     21  1.1.1.16  christos  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     22  1.1.1.16  christos  * copy of the source code appearing in this file ("Covered Code") an
     23  1.1.1.16  christos  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     24  1.1.1.16  christos  * base code distributed originally by Intel ("Original Intel Code") to copy,
     25  1.1.1.16  christos  * make derivatives, distribute, use and display any portion of the Covered
     26  1.1.1.16  christos  * Code in any form, with the right to sublicense such rights; and
     27  1.1.1.16  christos  *
     28  1.1.1.16  christos  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     29  1.1.1.16  christos  * license (with the right to sublicense), under only those claims of Intel
     30  1.1.1.16  christos  * patents that are infringed by the Original Intel Code, to make, use, sell,
     31  1.1.1.16  christos  * offer to sell, and import the Covered Code and derivative works thereof
     32  1.1.1.16  christos  * solely to the minimum extent necessary to exercise the above copyright
     33  1.1.1.16  christos  * license, and in no event shall the patent license extend to any additions
     34  1.1.1.16  christos  * to or modifications of the Original Intel Code. No other license or right
     35  1.1.1.16  christos  * is granted directly or by implication, estoppel or otherwise;
     36  1.1.1.16  christos  *
     37  1.1.1.16  christos  * The above copyright and patent license is granted only if the following
     38  1.1.1.16  christos  * conditions are met:
     39  1.1.1.16  christos  *
     40  1.1.1.16  christos  * 3. Conditions
     41  1.1.1.16  christos  *
     42  1.1.1.16  christos  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     43  1.1.1.16  christos  * Redistribution of source code of any substantial portion of the Covered
     44  1.1.1.16  christos  * Code or modification with rights to further distribute source must include
     45  1.1.1.16  christos  * the above Copyright Notice, the above License, this list of Conditions,
     46  1.1.1.16  christos  * and the following Disclaimer and Export Compliance provision. In addition,
     47  1.1.1.16  christos  * Licensee must cause all Covered Code to which Licensee contributes to
     48  1.1.1.16  christos  * contain a file documenting the changes Licensee made to create that Covered
     49  1.1.1.16  christos  * Code and the date of any change. Licensee must include in that file the
     50  1.1.1.16  christos  * documentation of any changes made by any predecessor Licensee. Licensee
     51  1.1.1.16  christos  * must include a prominent statement that the modification is derived,
     52  1.1.1.16  christos  * directly or indirectly, from Original Intel Code.
     53  1.1.1.16  christos  *
     54  1.1.1.16  christos  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     55  1.1.1.16  christos  * Redistribution of source code of any substantial portion of the Covered
     56  1.1.1.16  christos  * Code or modification without rights to further distribute source must
     57  1.1.1.16  christos  * include the following Disclaimer and Export Compliance provision in the
     58  1.1.1.16  christos  * documentation and/or other materials provided with distribution. In
     59  1.1.1.16  christos  * addition, Licensee may not authorize further sublicense of source of any
     60  1.1.1.16  christos  * portion of the Covered Code, and must include terms to the effect that the
     61  1.1.1.16  christos  * license from Licensee to its licensee is limited to the intellectual
     62  1.1.1.16  christos  * property embodied in the software Licensee provides to its licensee, and
     63  1.1.1.16  christos  * not to intellectual property embodied in modifications its licensee may
     64  1.1.1.16  christos  * make.
     65  1.1.1.16  christos  *
     66  1.1.1.16  christos  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     67  1.1.1.16  christos  * substantial portion of the Covered Code or modification must reproduce the
     68  1.1.1.16  christos  * above Copyright Notice, and the following Disclaimer and Export Compliance
     69  1.1.1.16  christos  * provision in the documentation and/or other materials provided with the
     70  1.1.1.16  christos  * distribution.
     71  1.1.1.16  christos  *
     72  1.1.1.16  christos  * 3.4. Intel retains all right, title, and interest in and to the Original
     73  1.1.1.16  christos  * Intel Code.
     74  1.1.1.16  christos  *
     75  1.1.1.16  christos  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     76  1.1.1.16  christos  * Intel shall be used in advertising or otherwise to promote the sale, use or
     77  1.1.1.16  christos  * other dealings in products derived from or relating to the Covered Code
     78  1.1.1.16  christos  * without prior written authorization from Intel.
     79  1.1.1.16  christos  *
     80  1.1.1.16  christos  * 4. Disclaimer and Export Compliance
     81  1.1.1.16  christos  *
     82  1.1.1.16  christos  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     83  1.1.1.16  christos  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     84  1.1.1.16  christos  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
     85  1.1.1.16  christos  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
     86  1.1.1.16  christos  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
     87  1.1.1.16  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     88  1.1.1.16  christos  * PARTICULAR PURPOSE.
     89  1.1.1.16  christos  *
     90  1.1.1.16  christos  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     91  1.1.1.16  christos  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     92  1.1.1.16  christos  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     93  1.1.1.16  christos  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     94  1.1.1.16  christos  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     95  1.1.1.16  christos  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
     96  1.1.1.16  christos  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     97  1.1.1.16  christos  * LIMITED REMEDY.
     98  1.1.1.16  christos  *
     99  1.1.1.16  christos  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    100  1.1.1.16  christos  * software or system incorporating such software without first obtaining any
    101  1.1.1.16  christos  * required license or other approval from the U. S. Department of Commerce or
    102  1.1.1.16  christos  * any other agency or department of the United States Government. In the
    103  1.1.1.16  christos  * event Licensee exports any such software from the United States or
    104  1.1.1.16  christos  * re-exports any such software from a foreign destination, Licensee shall
    105  1.1.1.16  christos  * ensure that the distribution and export/re-export of the software is in
    106  1.1.1.16  christos  * compliance with all laws, regulations, orders, or other restrictions of the
    107  1.1.1.16  christos  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    108  1.1.1.16  christos  * any of its subsidiaries will export/re-export any technical data, process,
    109  1.1.1.16  christos  * software, or service, directly or indirectly, to any country for which the
    110  1.1.1.16  christos  * United States government or any agency thereof requires an export license,
    111  1.1.1.16  christos  * other governmental approval, or letter of assurance, without first obtaining
    112  1.1.1.16  christos  * such license, approval or letter.
    113  1.1.1.16  christos  *
    114  1.1.1.16  christos  *****************************************************************************
    115  1.1.1.16  christos  *
    116  1.1.1.16  christos  * Alternatively, you may choose to be licensed under the terms of the
    117  1.1.1.16  christos  * following license:
    118  1.1.1.16  christos  *
    119   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
    120   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
    121   1.1.1.2    jruoho  * are met:
    122   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
    123   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
    124   1.1.1.2    jruoho  *    without modification.
    125   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
    126   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
    127   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
    128   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
    129   1.1.1.2    jruoho  *    binary redistribution.
    130   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
    131   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
    132   1.1.1.2    jruoho  *    from this software without specific prior written permission.
    133   1.1.1.2    jruoho  *
    134   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    135   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    136  1.1.1.13  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    137   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    138  1.1.1.16  christos  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    139  1.1.1.16  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    140  1.1.1.16  christos  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    141  1.1.1.16  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    142  1.1.1.16  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    143  1.1.1.16  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    144  1.1.1.16  christos  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    145  1.1.1.16  christos  *
    146  1.1.1.16  christos  * Alternatively, you may choose to be licensed under the terms of the
    147  1.1.1.16  christos  * GNU General Public License ("GPL") version 2 as published by the Free
    148  1.1.1.16  christos  * Software Foundation.
    149  1.1.1.16  christos  *
    150  1.1.1.16  christos  *****************************************************************************/
    151       1.1    jruoho 
    152       1.1    jruoho #include "acpisrc.h"
    153       1.1    jruoho 
    154   1.1.1.4  christos AS_BRACE_INFO               Gbl_BraceInfo[] =
    155   1.1.1.4  christos {
    156   1.1.1.4  christos     {" if",         3},
    157   1.1.1.4  christos     {" else if",    8},
    158   1.1.1.4  christos     {" else while", 11},
    159   1.1.1.4  christos     {" else",       5},
    160   1.1.1.4  christos     {" do ",        4},
    161   1.1.1.4  christos     {NULL,          0}
    162   1.1.1.4  christos };
    163   1.1.1.4  christos 
    164   1.1.1.4  christos 
    165       1.1    jruoho /* Local prototypes */
    166       1.1    jruoho 
    167   1.1.1.4  christos static char *
    168   1.1.1.4  christos AsMatchValidToken (
    169       1.1    jruoho     char                    *Buffer,
    170   1.1.1.4  christos     char                    *Filename,
    171   1.1.1.4  christos     char                    TargetChar,
    172   1.1.1.4  christos     AS_SCAN_CALLBACK        Callback);
    173   1.1.1.4  christos 
    174   1.1.1.4  christos static char *
    175   1.1.1.4  christos AsCheckBracesCallback (
    176   1.1.1.4  christos     char                    *Buffer,
    177   1.1.1.4  christos     char                    *Filename,
    178   1.1.1.4  christos     UINT32                  LineNumber);
    179       1.1    jruoho 
    180   1.1.1.4  christos static UINT32
    181       1.1    jruoho AsCountLines (
    182       1.1    jruoho     char                    *Buffer,
    183       1.1    jruoho     char                    *Filename);
    184       1.1    jruoho 
    185   1.1.1.4  christos 
    186   1.1.1.9  christos #define MODULE_HEADER_BEGIN "/******************************************************************************\n *\n * Module Name:";
    187   1.1.1.9  christos #define MODULE_HEADER_END   " *****************************************************************************/\n\n"
    188  1.1.1.17  christos #define INTEL_COPYRIGHT     " * Copyright (C) 2000 - 2025, Intel Corp.\n"
    189   1.1.1.9  christos 
    190       1.1    jruoho /* Opening signature of the Intel legal header */
    191       1.1    jruoho 
    192       1.1    jruoho char        *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
    193       1.1    jruoho 
    194   1.1.1.4  christos UINT32      NonAnsiCommentCount;
    195   1.1.1.4  christos 
    196   1.1.1.9  christos char        CopyRightHeaderEnd[] = INTEL_COPYRIGHT " *\n" MODULE_HEADER_END;
    197   1.1.1.4  christos 
    198   1.1.1.4  christos /******************************************************************************
    199   1.1.1.4  christos  *
    200   1.1.1.4  christos  * FUNCTION:    AsCountNonAnsiComments
    201   1.1.1.4  christos  *
    202   1.1.1.4  christos  * DESCRIPTION: Count the number of "//" comments. This type of comment is
    203   1.1.1.4  christos  *              non-ANSI C.
    204   1.1.1.4  christos  *
    205   1.1.1.4  christos  * NOTE: July 2014: Allows // within quoted strings and within normal
    206   1.1.1.4  christos  *       comments. Eliminates extraneous warnings from this utility.
    207   1.1.1.4  christos  *
    208   1.1.1.4  christos  ******************************************************************************/
    209   1.1.1.4  christos 
    210   1.1.1.4  christos void
    211   1.1.1.4  christos AsCountNonAnsiComments (
    212   1.1.1.4  christos     char                    *Buffer,
    213   1.1.1.4  christos     char                    *Filename)
    214   1.1.1.4  christos {
    215   1.1.1.4  christos 
    216   1.1.1.4  christos     AsMatchValidToken (Buffer, Filename, 0, NULL);
    217   1.1.1.4  christos 
    218   1.1.1.4  christos     /* Error if any slash-slash comments found */
    219   1.1.1.4  christos 
    220   1.1.1.4  christos     if (NonAnsiCommentCount)
    221   1.1.1.4  christos     {
    222   1.1.1.4  christos         AsPrint ("Non-ANSI // Comments Found", NonAnsiCommentCount, Filename);
    223   1.1.1.4  christos         Gbl_NonAnsiComments += NonAnsiCommentCount;
    224   1.1.1.4  christos     }
    225   1.1.1.4  christos }
    226   1.1.1.4  christos 
    227   1.1.1.4  christos 
    228   1.1.1.4  christos /******************************************************************************
    229   1.1.1.4  christos  *
    230   1.1.1.4  christos  * FUNCTION:    AsCheckForBraces
    231   1.1.1.4  christos  *
    232   1.1.1.4  christos  * DESCRIPTION: Check for an open brace after each if/else/do (etc.)
    233   1.1.1.4  christos  *              statement
    234   1.1.1.4  christos  *
    235   1.1.1.4  christos  ******************************************************************************/
    236   1.1.1.4  christos 
    237   1.1.1.4  christos void
    238   1.1.1.4  christos AsCheckForBraces (
    239   1.1.1.4  christos     char                    *Buffer,
    240   1.1.1.4  christos     char                    *Filename)
    241   1.1.1.4  christos {
    242   1.1.1.4  christos 
    243   1.1.1.4  christos     AsMatchValidToken (Buffer, Filename, 0, AsCheckBracesCallback);
    244   1.1.1.4  christos }
    245   1.1.1.4  christos 
    246   1.1.1.4  christos 
    247   1.1.1.4  christos /******************************************************************************
    248   1.1.1.4  christos  *
    249   1.1.1.4  christos  * FUNCTION:    AsCheckBracesCallback
    250   1.1.1.4  christos  *
    251   1.1.1.4  christos  * DESCRIPTION: Check if/else/do statements. Ensure that braces
    252   1.1.1.4  christos  *              are always used.
    253   1.1.1.4  christos  *
    254   1.1.1.4  christos  * TBD: Currently, don't check while() statements. The problem is that there
    255   1.1.1.4  christos  * are two forms: do {} while (); and while () {}.
    256   1.1.1.4  christos  *
    257   1.1.1.4  christos  ******************************************************************************/
    258   1.1.1.4  christos 
    259   1.1.1.4  christos static char *
    260   1.1.1.4  christos AsCheckBracesCallback (
    261   1.1.1.4  christos     char                    *Buffer,
    262   1.1.1.4  christos     char                    *Filename,
    263   1.1.1.4  christos     UINT32                  LineNumber)
    264   1.1.1.4  christos {
    265   1.1.1.4  christos     char                    *SubBuffer = Buffer;
    266   1.1.1.4  christos     char                    *NextBrace;
    267   1.1.1.4  christos     char                    *NextSemicolon;
    268   1.1.1.4  christos     AS_BRACE_INFO           *BraceInfo;
    269   1.1.1.4  christos 
    270   1.1.1.4  christos 
    271   1.1.1.4  christos     for (BraceInfo = Gbl_BraceInfo; BraceInfo->Operator; BraceInfo++)
    272   1.1.1.4  christos     {
    273   1.1.1.4  christos         if (!(strncmp (BraceInfo->Operator, SubBuffer, BraceInfo->Length)))
    274   1.1.1.4  christos         {
    275   1.1.1.4  christos             SubBuffer += (BraceInfo->Length - 1);
    276   1.1.1.4  christos 
    277   1.1.1.4  christos             /* Find next brace and the next semicolon */
    278   1.1.1.4  christos 
    279   1.1.1.4  christos             NextBrace = AsMatchValidToken (SubBuffer, Filename, '{', NULL);
    280   1.1.1.4  christos             NextSemicolon = AsMatchValidToken (SubBuffer, Filename, ';', NULL);
    281   1.1.1.4  christos 
    282   1.1.1.4  christos             /* Next brace should appear before next semicolon */
    283   1.1.1.4  christos 
    284   1.1.1.4  christos             if ((!NextBrace) ||
    285   1.1.1.4  christos                (NextSemicolon && (NextBrace > NextSemicolon)))
    286   1.1.1.4  christos             {
    287   1.1.1.4  christos                 Gbl_MissingBraces++;
    288   1.1.1.4  christos 
    289   1.1.1.4  christos                 if (!Gbl_QuietMode)
    290   1.1.1.4  christos                 {
    291   1.1.1.4  christos                     printf ("Missing braces for <%s>, line %u: %s\n",
    292   1.1.1.4  christos                         BraceInfo->Operator + 1, LineNumber, Filename);
    293   1.1.1.4  christos                 }
    294   1.1.1.4  christos             }
    295   1.1.1.4  christos 
    296   1.1.1.4  christos             return (SubBuffer);
    297   1.1.1.4  christos         }
    298   1.1.1.4  christos     }
    299   1.1.1.4  christos 
    300   1.1.1.4  christos     /* No match, just return original buffer */
    301   1.1.1.4  christos 
    302   1.1.1.4  christos     return (Buffer);
    303   1.1.1.4  christos }
    304   1.1.1.4  christos 
    305   1.1.1.4  christos 
    306   1.1.1.4  christos /******************************************************************************
    307   1.1.1.4  christos  *
    308   1.1.1.4  christos  * FUNCTION:    AsMatchValidToken
    309   1.1.1.4  christos  *
    310   1.1.1.4  christos  * DESCRIPTION: Find the next matching token in the input buffer.
    311   1.1.1.4  christos  *
    312   1.1.1.4  christos  ******************************************************************************/
    313   1.1.1.4  christos 
    314   1.1.1.4  christos static char *
    315   1.1.1.4  christos AsMatchValidToken (
    316   1.1.1.4  christos     char                    *Buffer,
    317   1.1.1.4  christos     char                    *Filename,
    318   1.1.1.4  christos     char                    TargetChar,
    319   1.1.1.4  christos     AS_SCAN_CALLBACK        Callback)
    320   1.1.1.4  christos {
    321   1.1.1.4  christos     char                    *SubBuffer = Buffer;
    322   1.1.1.4  christos     char                    *StringStart;
    323   1.1.1.4  christos     UINT32                  TotalLines;
    324   1.1.1.4  christos 
    325   1.1.1.4  christos 
    326   1.1.1.4  christos     TotalLines = 1;
    327   1.1.1.4  christos     NonAnsiCommentCount = 0;
    328   1.1.1.4  christos 
    329   1.1.1.4  christos     /* Scan from current position up to the end if necessary */
    330   1.1.1.4  christos 
    331   1.1.1.4  christos     while (*SubBuffer)
    332   1.1.1.4  christos     {
    333   1.1.1.4  christos         /* Skip normal comments */
    334   1.1.1.4  christos 
    335   1.1.1.4  christos         if ((*SubBuffer == '/') &&
    336   1.1.1.4  christos             (*(SubBuffer + 1) == '*'))
    337   1.1.1.4  christos         {
    338   1.1.1.4  christos             /* Must maintain line count */
    339   1.1.1.4  christos 
    340   1.1.1.4  christos             SubBuffer += 2;
    341   1.1.1.4  christos             while (strncmp ("*/", SubBuffer, 2))
    342   1.1.1.4  christos             {
    343   1.1.1.4  christos                 if (*SubBuffer == '\n')
    344   1.1.1.4  christos                 {
    345   1.1.1.4  christos                     TotalLines++;
    346   1.1.1.4  christos                 }
    347   1.1.1.4  christos                 SubBuffer++;
    348   1.1.1.4  christos             }
    349   1.1.1.4  christos 
    350   1.1.1.4  christos             SubBuffer += 2;
    351   1.1.1.4  christos             continue;
    352   1.1.1.4  christos         }
    353   1.1.1.4  christos 
    354   1.1.1.4  christos         /* Skip single quoted chars */
    355   1.1.1.4  christos 
    356   1.1.1.4  christos         if (*SubBuffer == '\'')
    357   1.1.1.4  christos         {
    358   1.1.1.4  christos             SubBuffer++;
    359   1.1.1.4  christos             if (!(*SubBuffer))
    360   1.1.1.4  christos             {
    361   1.1.1.4  christos                 break;
    362   1.1.1.4  christos             }
    363   1.1.1.4  christos 
    364   1.1.1.4  christos             if (*SubBuffer == '\\')
    365   1.1.1.4  christos             {
    366   1.1.1.4  christos                 SubBuffer++;
    367   1.1.1.4  christos             }
    368   1.1.1.7  christos 
    369   1.1.1.4  christos             SubBuffer++;
    370   1.1.1.4  christos             continue;
    371   1.1.1.4  christos         }
    372   1.1.1.4  christos 
    373   1.1.1.4  christos         /* Skip quoted strings */
    374   1.1.1.4  christos 
    375   1.1.1.4  christos         if (*SubBuffer == '"')
    376   1.1.1.4  christos         {
    377   1.1.1.4  christos             StringStart = SubBuffer;
    378   1.1.1.4  christos             SubBuffer++;
    379   1.1.1.4  christos             if (!(*SubBuffer))
    380   1.1.1.4  christos             {
    381   1.1.1.4  christos                 break;
    382   1.1.1.4  christos             }
    383   1.1.1.4  christos 
    384   1.1.1.4  christos             while (*SubBuffer != '"')
    385   1.1.1.4  christos             {
    386   1.1.1.4  christos                 if ((*SubBuffer == '\n') ||
    387   1.1.1.4  christos                     (!(*SubBuffer)))
    388   1.1.1.4  christos                 {
    389   1.1.1.6  christos                     AsPrint ("Unbalanced quoted string",1, Filename);
    390   1.1.1.6  christos                     printf ("    %.32s (line %u)\n", StringStart, TotalLines);
    391   1.1.1.4  christos                     break;
    392   1.1.1.4  christos                 }
    393   1.1.1.4  christos 
    394   1.1.1.4  christos                 /* Handle escapes within the string */
    395   1.1.1.4  christos 
    396   1.1.1.4  christos                 if (*SubBuffer == '\\')
    397   1.1.1.4  christos                 {
    398   1.1.1.4  christos                     SubBuffer++;
    399   1.1.1.4  christos                 }
    400   1.1.1.7  christos 
    401   1.1.1.4  christos                 SubBuffer++;
    402   1.1.1.4  christos             }
    403   1.1.1.4  christos 
    404   1.1.1.4  christos             SubBuffer++;
    405   1.1.1.4  christos             continue;
    406   1.1.1.4  christos         }
    407   1.1.1.4  christos 
    408   1.1.1.4  christos         /* Now we can check for a slash-slash comment */
    409   1.1.1.4  christos 
    410   1.1.1.4  christos         if ((*SubBuffer == '/') &&
    411   1.1.1.4  christos             (*(SubBuffer + 1) == '/'))
    412   1.1.1.4  christos         {
    413   1.1.1.4  christos             NonAnsiCommentCount++;
    414   1.1.1.4  christos 
    415   1.1.1.4  christos             /* Skip to end-of-line */
    416   1.1.1.4  christos 
    417   1.1.1.4  christos             while ((*SubBuffer != '\n') &&
    418   1.1.1.4  christos                 (*SubBuffer))
    419   1.1.1.4  christos             {
    420   1.1.1.4  christos                 SubBuffer++;
    421   1.1.1.4  christos             }
    422   1.1.1.4  christos 
    423   1.1.1.4  christos             if (!(*SubBuffer))
    424   1.1.1.4  christos             {
    425   1.1.1.4  christos                 break;
    426   1.1.1.4  christos             }
    427   1.1.1.4  christos 
    428   1.1.1.4  christos             if (*SubBuffer == '\n')
    429   1.1.1.4  christos             {
    430   1.1.1.4  christos                 TotalLines++;
    431   1.1.1.4  christos             }
    432   1.1.1.4  christos 
    433   1.1.1.4  christos             SubBuffer++;
    434   1.1.1.4  christos             continue;
    435   1.1.1.4  christos         }
    436   1.1.1.4  christos 
    437   1.1.1.4  christos         /* Finally, check for a newline */
    438   1.1.1.4  christos 
    439   1.1.1.4  christos         if (*SubBuffer == '\n')
    440   1.1.1.4  christos         {
    441   1.1.1.4  christos             TotalLines++;
    442   1.1.1.4  christos             SubBuffer++;
    443   1.1.1.4  christos             continue;
    444   1.1.1.4  christos         }
    445   1.1.1.4  christos 
    446   1.1.1.4  christos         /* Normal character, do the user actions */
    447   1.1.1.4  christos 
    448   1.1.1.4  christos         if (Callback)
    449   1.1.1.4  christos         {
    450   1.1.1.4  christos             SubBuffer = Callback (SubBuffer, Filename, TotalLines);
    451   1.1.1.4  christos         }
    452   1.1.1.4  christos 
    453   1.1.1.4  christos         if (TargetChar && (*SubBuffer == TargetChar))
    454   1.1.1.4  christos         {
    455   1.1.1.4  christos             return (SubBuffer);
    456   1.1.1.4  christos         }
    457   1.1.1.4  christos 
    458   1.1.1.4  christos         SubBuffer++;
    459   1.1.1.4  christos     }
    460   1.1.1.4  christos 
    461   1.1.1.4  christos     return (NULL);
    462   1.1.1.4  christos }
    463   1.1.1.4  christos 
    464       1.1    jruoho 
    465       1.1    jruoho /******************************************************************************
    466       1.1    jruoho  *
    467   1.1.1.3  christos  * FUNCTION:    AsRemoveExtraLines
    468   1.1.1.3  christos  *
    469   1.1.1.3  christos  * DESCRIPTION: Remove all extra lines at the start and end of the file.
    470   1.1.1.3  christos  *
    471   1.1.1.3  christos  ******************************************************************************/
    472   1.1.1.3  christos 
    473   1.1.1.3  christos void
    474   1.1.1.3  christos AsRemoveExtraLines (
    475   1.1.1.3  christos     char                    *FileBuffer,
    476   1.1.1.3  christos     char                    *Filename)
    477   1.1.1.3  christos {
    478   1.1.1.3  christos     char                    *FileEnd;
    479   1.1.1.3  christos     int                     Length;
    480   1.1.1.3  christos 
    481   1.1.1.3  christos 
    482   1.1.1.3  christos     /* Remove any extra lines at the start of the file */
    483   1.1.1.3  christos 
    484   1.1.1.3  christos     while (*FileBuffer == '\n')
    485   1.1.1.3  christos     {
    486   1.1.1.3  christos         printf ("Removing extra line at start of file: %s\n", Filename);
    487   1.1.1.3  christos         AsRemoveData (FileBuffer, FileBuffer + 1);
    488   1.1.1.3  christos     }
    489   1.1.1.3  christos 
    490   1.1.1.3  christos     /* Remove any extra lines at the end of the file */
    491   1.1.1.3  christos 
    492   1.1.1.3  christos     Length = strlen (FileBuffer);
    493   1.1.1.3  christos     FileEnd = FileBuffer + (Length - 2);
    494   1.1.1.3  christos 
    495   1.1.1.3  christos     while (*FileEnd == '\n')
    496   1.1.1.3  christos     {
    497   1.1.1.3  christos         printf ("Removing extra line at end of file: %s\n", Filename);
    498   1.1.1.3  christos         AsRemoveData (FileEnd, FileEnd + 1);
    499   1.1.1.3  christos         FileEnd--;
    500   1.1.1.3  christos     }
    501   1.1.1.3  christos }
    502   1.1.1.3  christos 
    503   1.1.1.3  christos 
    504   1.1.1.3  christos /******************************************************************************
    505   1.1.1.3  christos  *
    506   1.1.1.3  christos  * FUNCTION:    AsRemoveSpacesAfterPeriod
    507   1.1.1.3  christos  *
    508   1.1.1.3  christos  * DESCRIPTION: Remove an extra space after a period.
    509   1.1.1.3  christos  *
    510   1.1.1.3  christos  ******************************************************************************/
    511   1.1.1.3  christos 
    512   1.1.1.3  christos void
    513   1.1.1.3  christos AsRemoveSpacesAfterPeriod (
    514   1.1.1.3  christos     char                    *FileBuffer,
    515   1.1.1.3  christos     char                    *Filename)
    516   1.1.1.3  christos {
    517   1.1.1.3  christos     int                     ReplaceCount = 0;
    518   1.1.1.3  christos     char                    *Possible;
    519   1.1.1.3  christos 
    520   1.1.1.3  christos 
    521   1.1.1.3  christos     Possible = FileBuffer;
    522   1.1.1.3  christos     while (Possible)
    523   1.1.1.3  christos     {
    524   1.1.1.3  christos         Possible = strstr (Possible, ".  ");
    525   1.1.1.3  christos         if (Possible)
    526   1.1.1.3  christos         {
    527   1.1.1.3  christos             if ((*(Possible -1) == '.')  ||
    528   1.1.1.3  christos                 (*(Possible -1) == '\"') ||
    529   1.1.1.3  christos                 (*(Possible -1) == '\n'))
    530   1.1.1.3  christos             {
    531   1.1.1.3  christos                 Possible += 3;
    532   1.1.1.3  christos                 continue;
    533   1.1.1.3  christos             }
    534   1.1.1.3  christos 
    535   1.1.1.3  christos             Possible = AsReplaceData (Possible, 3, ". ", 2);
    536   1.1.1.3  christos             ReplaceCount++;
    537   1.1.1.3  christos         }
    538   1.1.1.3  christos     }
    539   1.1.1.3  christos 
    540   1.1.1.3  christos     if (ReplaceCount)
    541   1.1.1.3  christos     {
    542   1.1.1.3  christos         printf ("Removed %d extra blanks after a period: %s\n",
    543   1.1.1.3  christos             ReplaceCount, Filename);
    544   1.1.1.3  christos     }
    545   1.1.1.3  christos }
    546   1.1.1.3  christos 
    547   1.1.1.3  christos 
    548   1.1.1.3  christos /******************************************************************************
    549   1.1.1.3  christos  *
    550       1.1    jruoho  * FUNCTION:    AsMatchExactWord
    551       1.1    jruoho  *
    552       1.1    jruoho  * DESCRIPTION: Check previous and next characters for whitespace
    553       1.1    jruoho  *
    554       1.1    jruoho  ******************************************************************************/
    555       1.1    jruoho 
    556       1.1    jruoho BOOLEAN
    557       1.1    jruoho AsMatchExactWord (
    558       1.1    jruoho     char                    *Word,
    559       1.1    jruoho     UINT32                  WordLength)
    560       1.1    jruoho {
    561       1.1    jruoho     char                    NextChar;
    562       1.1    jruoho     char                    PrevChar;
    563       1.1    jruoho 
    564       1.1    jruoho 
    565       1.1    jruoho     NextChar = Word[WordLength];
    566       1.1    jruoho     PrevChar = * (Word -1);
    567       1.1    jruoho 
    568       1.1    jruoho     if (isalnum ((int) NextChar) ||
    569       1.1    jruoho         (NextChar == '_')  ||
    570       1.1    jruoho         isalnum ((int) PrevChar) ||
    571       1.1    jruoho         (PrevChar == '_'))
    572       1.1    jruoho     {
    573       1.1    jruoho         return (FALSE);
    574       1.1    jruoho     }
    575       1.1    jruoho 
    576       1.1    jruoho     return (TRUE);
    577       1.1    jruoho }
    578       1.1    jruoho 
    579       1.1    jruoho 
    580       1.1    jruoho /******************************************************************************
    581       1.1    jruoho  *
    582       1.1    jruoho  * FUNCTION:    AsPrint
    583       1.1    jruoho  *
    584       1.1    jruoho  * DESCRIPTION: Common formatted print
    585       1.1    jruoho  *
    586       1.1    jruoho  ******************************************************************************/
    587       1.1    jruoho 
    588       1.1    jruoho void
    589       1.1    jruoho AsPrint (
    590       1.1    jruoho     char                    *Message,
    591       1.1    jruoho     UINT32                  Count,
    592       1.1    jruoho     char                    *Filename)
    593       1.1    jruoho {
    594       1.1    jruoho 
    595       1.1    jruoho     if (Gbl_QuietMode)
    596       1.1    jruoho     {
    597       1.1    jruoho         return;
    598       1.1    jruoho     }
    599       1.1    jruoho 
    600       1.1    jruoho     printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
    601       1.1    jruoho }
    602       1.1    jruoho 
    603       1.1    jruoho 
    604       1.1    jruoho /******************************************************************************
    605       1.1    jruoho  *
    606       1.1    jruoho  * FUNCTION:    AsTrimLines
    607       1.1    jruoho  *
    608   1.1.1.3  christos  * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
    609       1.1    jruoho  *              check for tabs.
    610       1.1    jruoho  *
    611       1.1    jruoho  ******************************************************************************/
    612       1.1    jruoho 
    613       1.1    jruoho void
    614       1.1    jruoho AsTrimLines (
    615       1.1    jruoho     char                    *Buffer,
    616       1.1    jruoho     char                    *Filename)
    617       1.1    jruoho {
    618       1.1    jruoho     char                    *SubBuffer = Buffer;
    619       1.1    jruoho     char                    *StartWhiteSpace = NULL;
    620       1.1    jruoho     UINT32                  SpaceCount = 0;
    621       1.1    jruoho 
    622       1.1    jruoho 
    623       1.1    jruoho     while (*SubBuffer)
    624       1.1    jruoho     {
    625       1.1    jruoho         while (*SubBuffer != '\n')
    626       1.1    jruoho         {
    627       1.1    jruoho             if (!*SubBuffer)
    628       1.1    jruoho             {
    629       1.1    jruoho                 goto Exit;
    630       1.1    jruoho             }
    631       1.1    jruoho 
    632       1.1    jruoho             if (*SubBuffer == ' ')
    633       1.1    jruoho             {
    634       1.1    jruoho                 if (!StartWhiteSpace)
    635       1.1    jruoho                 {
    636       1.1    jruoho                     StartWhiteSpace = SubBuffer;
    637       1.1    jruoho                 }
    638       1.1    jruoho             }
    639       1.1    jruoho             else
    640       1.1    jruoho             {
    641       1.1    jruoho                 StartWhiteSpace = NULL;
    642       1.1    jruoho             }
    643       1.1    jruoho 
    644       1.1    jruoho             SubBuffer++;
    645       1.1    jruoho         }
    646       1.1    jruoho 
    647       1.1    jruoho         if (StartWhiteSpace)
    648       1.1    jruoho         {
    649       1.1    jruoho             SpaceCount += (SubBuffer - StartWhiteSpace);
    650       1.1    jruoho 
    651       1.1    jruoho             /* Remove the spaces */
    652       1.1    jruoho 
    653       1.1    jruoho             SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
    654       1.1    jruoho             StartWhiteSpace = NULL;
    655       1.1    jruoho         }
    656       1.1    jruoho 
    657       1.1    jruoho         SubBuffer++;
    658       1.1    jruoho     }
    659       1.1    jruoho 
    660       1.1    jruoho 
    661       1.1    jruoho Exit:
    662       1.1    jruoho     if (SpaceCount)
    663       1.1    jruoho     {
    664       1.1    jruoho         Gbl_MadeChanges = TRUE;
    665       1.1    jruoho         AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
    666       1.1    jruoho     }
    667       1.1    jruoho }
    668       1.1    jruoho 
    669       1.1    jruoho 
    670       1.1    jruoho /******************************************************************************
    671       1.1    jruoho  *
    672       1.1    jruoho  * FUNCTION:    AsTrimWhitespace
    673       1.1    jruoho  *
    674       1.1    jruoho  * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
    675       1.1    jruoho  *              this can happen during the translation when lines are removed.
    676       1.1    jruoho  *
    677       1.1    jruoho  ******************************************************************************/
    678       1.1    jruoho 
    679       1.1    jruoho void
    680       1.1    jruoho AsTrimWhitespace (
    681       1.1    jruoho     char                    *Buffer)
    682       1.1    jruoho {
    683   1.1.1.4  christos     char                    *SubBuffer;
    684       1.1    jruoho     int                     ReplaceCount = 1;
    685       1.1    jruoho 
    686       1.1    jruoho 
    687       1.1    jruoho     while (ReplaceCount)
    688       1.1    jruoho     {
    689   1.1.1.4  christos         ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n",
    690   1.1.1.4  christos             REPLACE_SUBSTRINGS, Buffer);
    691   1.1.1.4  christos     }
    692   1.1.1.4  christos 
    693   1.1.1.4  christos     /*
    694   1.1.1.4  christos      * Check for exactly one blank line after the copyright header
    695   1.1.1.4  christos      */
    696   1.1.1.4  christos 
    697   1.1.1.4  christos     /* Find the header */
    698   1.1.1.4  christos 
    699   1.1.1.4  christos     SubBuffer = strstr (Buffer, HeaderBegin);
    700   1.1.1.4  christos     if (!SubBuffer)
    701   1.1.1.4  christos     {
    702   1.1.1.4  christos         return;
    703   1.1.1.4  christos     }
    704   1.1.1.4  christos 
    705   1.1.1.4  christos     /* Find the end of the header */
    706   1.1.1.4  christos 
    707   1.1.1.4  christos     SubBuffer = strstr (SubBuffer, "*/");
    708   1.1.1.4  christos     SubBuffer = AsSkipPastChar (SubBuffer, '\n');
    709   1.1.1.4  christos 
    710   1.1.1.4  christos     /* Replace a double blank line with a single */
    711   1.1.1.4  christos 
    712   1.1.1.4  christos     if (!strncmp (SubBuffer, "\n\n", 2))
    713   1.1.1.4  christos     {
    714   1.1.1.5  christos         AsReplaceData (SubBuffer, 2, "\n", 1);
    715   1.1.1.4  christos         AcpiOsPrintf ("Found multiple blank lines after copyright\n");
    716   1.1.1.4  christos     }
    717   1.1.1.4  christos 
    718   1.1.1.4  christos     /* If no blank line after header, insert one */
    719   1.1.1.4  christos 
    720   1.1.1.4  christos     else if (*SubBuffer != '\n')
    721   1.1.1.4  christos     {
    722   1.1.1.4  christos         AsInsertData (SubBuffer, "\n", 1);
    723   1.1.1.4  christos         AcpiOsPrintf ("Inserted blank line after copyright\n");
    724       1.1    jruoho     }
    725       1.1    jruoho }
    726       1.1    jruoho 
    727       1.1    jruoho 
    728       1.1    jruoho /******************************************************************************
    729       1.1    jruoho  *
    730       1.1    jruoho  * FUNCTION:    AsReplaceHeader
    731       1.1    jruoho  *
    732       1.1    jruoho  * DESCRIPTION: Replace the default Intel legal header with a new header
    733       1.1    jruoho  *
    734       1.1    jruoho  ******************************************************************************/
    735       1.1    jruoho 
    736       1.1    jruoho void
    737       1.1    jruoho AsReplaceHeader (
    738       1.1    jruoho     char                    *Buffer,
    739       1.1    jruoho     char                    *NewHeader)
    740       1.1    jruoho {
    741       1.1    jruoho     char                    *SubBuffer;
    742       1.1    jruoho     char                    *TokenEnd;
    743       1.1    jruoho 
    744       1.1    jruoho 
    745       1.1    jruoho     /* Find the original header */
    746       1.1    jruoho 
    747       1.1    jruoho     SubBuffer = strstr (Buffer, HeaderBegin);
    748       1.1    jruoho     if (!SubBuffer)
    749       1.1    jruoho     {
    750       1.1    jruoho         return;
    751       1.1    jruoho     }
    752       1.1    jruoho 
    753       1.1    jruoho     /* Find the end of the original header */
    754       1.1    jruoho 
    755       1.1    jruoho     TokenEnd = strstr (SubBuffer, "*/");
    756       1.1    jruoho     TokenEnd = AsSkipPastChar (TokenEnd, '\n');
    757       1.1    jruoho 
    758       1.1    jruoho     /* Delete old header, insert new one */
    759       1.1    jruoho 
    760   1.1.1.4  christos     AsReplaceData (SubBuffer, TokenEnd - SubBuffer,
    761   1.1.1.4  christos         NewHeader, strlen (NewHeader));
    762       1.1    jruoho }
    763       1.1    jruoho 
    764       1.1    jruoho 
    765       1.1    jruoho /******************************************************************************
    766       1.1    jruoho  *
    767   1.1.1.9  christos  * FUNCTION:    AsDoSpdxHeader
    768   1.1.1.9  christos  *
    769   1.1.1.9  christos  * DESCRIPTION: Replace the default Intel legal header with a new header
    770   1.1.1.9  christos  *
    771   1.1.1.9  christos  ******************************************************************************/
    772   1.1.1.9  christos 
    773   1.1.1.9  christos void
    774   1.1.1.9  christos AsDoSpdxHeader (
    775   1.1.1.9  christos     char                    *Buffer,
    776   1.1.1.9  christos     char                    *SpdxHeader)
    777   1.1.1.9  christos {
    778   1.1.1.9  christos     char                    *SubBuffer;
    779   1.1.1.9  christos 
    780   1.1.1.9  christos 
    781   1.1.1.9  christos     /* Place an SPDX header at the very top */
    782   1.1.1.9  christos 
    783   1.1.1.9  christos     AsReplaceData (Buffer, 0,
    784   1.1.1.9  christos         SpdxHeader, strlen (SpdxHeader));
    785   1.1.1.9  christos 
    786   1.1.1.9  christos     /* Place an Intel copyright notice in the module header */
    787   1.1.1.9  christos 
    788   1.1.1.9  christos     SubBuffer = strstr (Buffer, MODULE_HEADER_END);
    789   1.1.1.9  christos     if (!SubBuffer)
    790   1.1.1.9  christos     {
    791   1.1.1.9  christos         return;
    792   1.1.1.9  christos     }
    793   1.1.1.9  christos 
    794   1.1.1.9  christos     AsReplaceData (SubBuffer, strlen (MODULE_HEADER_END),
    795   1.1.1.9  christos         CopyRightHeaderEnd, strlen (CopyRightHeaderEnd));
    796   1.1.1.9  christos }
    797   1.1.1.9  christos 
    798   1.1.1.9  christos /******************************************************************************
    799   1.1.1.9  christos  *
    800       1.1    jruoho  * FUNCTION:    AsReplaceString
    801       1.1    jruoho  *
    802       1.1    jruoho  * DESCRIPTION: Replace all instances of a target string with a replacement
    803   1.1.1.3  christos  *              string. Returns count of the strings replaced.
    804       1.1    jruoho  *
    805       1.1    jruoho  ******************************************************************************/
    806       1.1    jruoho 
    807       1.1    jruoho int
    808       1.1    jruoho AsReplaceString (
    809       1.1    jruoho     char                    *Target,
    810       1.1    jruoho     char                    *Replacement,
    811       1.1    jruoho     UINT8                   Type,
    812       1.1    jruoho     char                    *Buffer)
    813       1.1    jruoho {
    814       1.1    jruoho     char                    *SubString1;
    815       1.1    jruoho     char                    *SubString2;
    816       1.1    jruoho     char                    *SubBuffer;
    817       1.1    jruoho     int                     TargetLength;
    818       1.1    jruoho     int                     ReplacementLength;
    819       1.1    jruoho     int                     ReplaceCount = 0;
    820       1.1    jruoho 
    821       1.1    jruoho 
    822       1.1    jruoho     TargetLength = strlen (Target);
    823       1.1    jruoho     ReplacementLength = strlen (Replacement);
    824       1.1    jruoho 
    825       1.1    jruoho     SubBuffer = Buffer;
    826       1.1    jruoho     SubString1 = Buffer;
    827       1.1    jruoho 
    828       1.1    jruoho     while (SubString1)
    829       1.1    jruoho     {
    830       1.1    jruoho         /* Find the target string */
    831       1.1    jruoho 
    832       1.1    jruoho         SubString1 = strstr (SubBuffer, Target);
    833       1.1    jruoho         if (!SubString1)
    834       1.1    jruoho         {
    835   1.1.1.3  christos             return (ReplaceCount);
    836       1.1    jruoho         }
    837       1.1    jruoho 
    838       1.1    jruoho         /*
    839       1.1    jruoho          * Check for translation escape string -- means to ignore
    840       1.1    jruoho          * blocks of code while replacing
    841       1.1    jruoho          */
    842   1.1.1.3  christos         if (Gbl_IgnoreTranslationEscapes)
    843   1.1.1.3  christos         {
    844   1.1.1.3  christos             SubString2 = NULL;
    845   1.1.1.3  christos         }
    846   1.1.1.3  christos         else
    847   1.1.1.3  christos         {
    848   1.1.1.3  christos             SubString2 = strstr (SubBuffer, AS_START_IGNORE);
    849   1.1.1.3  christos         }
    850       1.1    jruoho 
    851       1.1    jruoho         if ((SubString2) &&
    852       1.1    jruoho             (SubString2 < SubString1))
    853       1.1    jruoho         {
    854       1.1    jruoho             /* Find end of the escape block starting at "Substring2" */
    855       1.1    jruoho 
    856       1.1    jruoho             SubString2 = strstr (SubString2, AS_STOP_IGNORE);
    857       1.1    jruoho             if (!SubString2)
    858       1.1    jruoho             {
    859       1.1    jruoho                 /* Didn't find terminator */
    860       1.1    jruoho 
    861   1.1.1.3  christos                 return (ReplaceCount);
    862       1.1    jruoho             }
    863       1.1    jruoho 
    864       1.1    jruoho             /* Move buffer to end of escape block and continue */
    865       1.1    jruoho 
    866       1.1    jruoho             SubBuffer = SubString2;
    867       1.1    jruoho         }
    868       1.1    jruoho 
    869       1.1    jruoho         /* Do the actual replace if the target was found */
    870       1.1    jruoho 
    871       1.1    jruoho         else
    872       1.1    jruoho         {
    873       1.1    jruoho             if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
    874       1.1    jruoho             {
    875       1.1    jruoho                 if (!AsMatchExactWord (SubString1, TargetLength))
    876       1.1    jruoho                 {
    877       1.1    jruoho                     SubBuffer = SubString1 + 1;
    878       1.1    jruoho                     continue;
    879       1.1    jruoho                 }
    880       1.1    jruoho             }
    881       1.1    jruoho 
    882   1.1.1.4  christos             SubBuffer = AsReplaceData (SubString1, TargetLength,
    883   1.1.1.4  christos                 Replacement, ReplacementLength);
    884       1.1    jruoho 
    885       1.1    jruoho             if ((Type & EXTRA_INDENT_C) &&
    886       1.1    jruoho                 (!Gbl_StructDefs))
    887       1.1    jruoho             {
    888       1.1    jruoho                 SubBuffer = AsInsertData (SubBuffer, "        ", 8);
    889       1.1    jruoho             }
    890       1.1    jruoho 
    891       1.1    jruoho             ReplaceCount++;
    892       1.1    jruoho         }
    893       1.1    jruoho     }
    894       1.1    jruoho 
    895   1.1.1.3  christos     return (ReplaceCount);
    896       1.1    jruoho }
    897       1.1    jruoho 
    898       1.1    jruoho 
    899       1.1    jruoho /******************************************************************************
    900       1.1    jruoho  *
    901       1.1    jruoho  * FUNCTION:    AsConvertToLineFeeds
    902       1.1    jruoho  *
    903   1.1.1.4  christos  * DESCRIPTION: Convert all CR/LF pairs to LF only.
    904       1.1    jruoho  *
    905       1.1    jruoho  ******************************************************************************/
    906       1.1    jruoho 
    907       1.1    jruoho void
    908       1.1    jruoho AsConvertToLineFeeds (
    909       1.1    jruoho     char                    *Buffer)
    910       1.1    jruoho {
    911       1.1    jruoho     char                    *SubString;
    912       1.1    jruoho     char                    *SubBuffer;
    913       1.1    jruoho 
    914       1.1    jruoho 
    915       1.1    jruoho     SubBuffer = Buffer;
    916       1.1    jruoho     SubString = Buffer;
    917       1.1    jruoho 
    918       1.1    jruoho     while (SubString)
    919       1.1    jruoho     {
    920       1.1    jruoho         /* Find the target string */
    921       1.1    jruoho 
    922       1.1    jruoho         SubString = strstr (SubBuffer, "\r\n");
    923       1.1    jruoho         if (!SubString)
    924       1.1    jruoho         {
    925       1.1    jruoho             return;
    926       1.1    jruoho         }
    927       1.1    jruoho 
    928       1.1    jruoho         SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
    929       1.1    jruoho     }
    930       1.1    jruoho }
    931       1.1    jruoho 
    932       1.1    jruoho 
    933       1.1    jruoho /******************************************************************************
    934       1.1    jruoho  *
    935       1.1    jruoho  * FUNCTION:    AsInsertCarriageReturns
    936       1.1    jruoho  *
    937   1.1.1.4  christos  * DESCRIPTION: Convert lone LFs to CR/LF pairs.
    938       1.1    jruoho  *
    939       1.1    jruoho  ******************************************************************************/
    940       1.1    jruoho 
    941       1.1    jruoho void
    942       1.1    jruoho AsInsertCarriageReturns (
    943       1.1    jruoho     char                    *Buffer)
    944       1.1    jruoho {
    945       1.1    jruoho     char                    *SubString;
    946       1.1    jruoho     char                    *SubBuffer;
    947       1.1    jruoho 
    948       1.1    jruoho 
    949       1.1    jruoho     SubBuffer = Buffer;
    950       1.1    jruoho     SubString = Buffer;
    951       1.1    jruoho 
    952       1.1    jruoho     while (SubString)
    953       1.1    jruoho     {
    954       1.1    jruoho         /* Find the target string */
    955       1.1    jruoho 
    956       1.1    jruoho         SubString = strstr (SubBuffer, "\n");
    957       1.1    jruoho         if (!SubString)
    958       1.1    jruoho         {
    959       1.1    jruoho             return;
    960       1.1    jruoho         }
    961       1.1    jruoho 
    962       1.1    jruoho         SubBuffer = AsInsertData (SubString, "\r", 1);
    963       1.1    jruoho         SubBuffer += 1;
    964       1.1    jruoho     }
    965       1.1    jruoho }
    966       1.1    jruoho 
    967       1.1    jruoho 
    968       1.1    jruoho /******************************************************************************
    969       1.1    jruoho  *
    970       1.1    jruoho  * FUNCTION:    AsBracesOnSameLine
    971       1.1    jruoho  *
    972       1.1    jruoho  * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
    973       1.1    jruoho  *              or while statement (leave function opening brace on separate
    974       1.1    jruoho  *              line).
    975       1.1    jruoho  *
    976       1.1    jruoho  ******************************************************************************/
    977       1.1    jruoho 
    978       1.1    jruoho void
    979       1.1    jruoho AsBracesOnSameLine (
    980       1.1    jruoho     char                    *Buffer)
    981       1.1    jruoho {
    982       1.1    jruoho     char                    *SubBuffer = Buffer;
    983       1.1    jruoho     char                    *Beginning;
    984       1.1    jruoho     char                    *StartOfThisLine;
    985       1.1    jruoho     char                    *Next;
    986       1.1    jruoho     BOOLEAN                 BlockBegin = TRUE;
    987       1.1    jruoho 
    988       1.1    jruoho 
    989       1.1    jruoho     while (*SubBuffer)
    990       1.1    jruoho     {
    991       1.1    jruoho         /* Ignore comments */
    992       1.1    jruoho 
    993       1.1    jruoho         if ((SubBuffer[0] == '/') &&
    994       1.1    jruoho             (SubBuffer[1] == '*'))
    995       1.1    jruoho         {
    996       1.1    jruoho             SubBuffer = strstr (SubBuffer, "*/");
    997       1.1    jruoho             if (!SubBuffer)
    998       1.1    jruoho             {
    999       1.1    jruoho                 return;
   1000       1.1    jruoho             }
   1001       1.1    jruoho 
   1002       1.1    jruoho             SubBuffer += 2;
   1003       1.1    jruoho             continue;
   1004       1.1    jruoho         }
   1005       1.1    jruoho 
   1006       1.1    jruoho         /* Ignore quoted strings */
   1007       1.1    jruoho 
   1008       1.1    jruoho         if (*SubBuffer == '\"')
   1009       1.1    jruoho         {
   1010       1.1    jruoho             SubBuffer++;
   1011       1.1    jruoho             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
   1012       1.1    jruoho             if (!SubBuffer)
   1013       1.1    jruoho             {
   1014       1.1    jruoho                 return;
   1015       1.1    jruoho             }
   1016       1.1    jruoho         }
   1017       1.1    jruoho 
   1018       1.1    jruoho         if (!strncmp ("\n}", SubBuffer, 2))
   1019       1.1    jruoho         {
   1020       1.1    jruoho             /*
   1021       1.1    jruoho              * A newline followed by a closing brace closes a function
   1022       1.1    jruoho              * or struct or initializer block
   1023       1.1    jruoho              */
   1024       1.1    jruoho             BlockBegin = TRUE;
   1025       1.1    jruoho         }
   1026       1.1    jruoho 
   1027       1.1    jruoho         /*
   1028       1.1    jruoho          * Move every standalone brace up to the previous line
   1029       1.1    jruoho          * Check for digit will ignore initializer lists surrounded by braces.
   1030       1.1    jruoho          * This will work until we we need more complex detection.
   1031       1.1    jruoho          */
   1032       1.1    jruoho         if ((*SubBuffer == '{') && !isdigit ((int) SubBuffer[1]))
   1033       1.1    jruoho         {
   1034       1.1    jruoho             if (BlockBegin)
   1035       1.1    jruoho             {
   1036       1.1    jruoho                 BlockBegin = FALSE;
   1037       1.1    jruoho             }
   1038       1.1    jruoho             else
   1039       1.1    jruoho             {
   1040       1.1    jruoho                 /*
   1041       1.1    jruoho                  * Backup to previous non-whitespace
   1042       1.1    jruoho                  */
   1043       1.1    jruoho                 Beginning = SubBuffer - 1;
   1044       1.1    jruoho                 while ((*Beginning == ' ')   ||
   1045       1.1    jruoho                        (*Beginning == '\n'))
   1046       1.1    jruoho                 {
   1047       1.1    jruoho                     Beginning--;
   1048       1.1    jruoho                 }
   1049       1.1    jruoho 
   1050       1.1    jruoho                 StartOfThisLine = Beginning;
   1051       1.1    jruoho                 while (*StartOfThisLine != '\n')
   1052       1.1    jruoho                 {
   1053       1.1    jruoho                     StartOfThisLine--;
   1054       1.1    jruoho                 }
   1055       1.1    jruoho 
   1056       1.1    jruoho                 /*
   1057       1.1    jruoho                  * Move the brace up to the previous line, UNLESS:
   1058       1.1    jruoho                  *
   1059       1.1    jruoho                  * 1) There is a conditional compile on the line (starts with '#')
   1060       1.1    jruoho                  * 2) Previous line ends with an '=' (Start of initializer block)
   1061       1.1    jruoho                  * 3) Previous line ends with a comma (part of an init list)
   1062       1.1    jruoho                  * 4) Previous line ends with a backslash (part of a macro)
   1063       1.1    jruoho                  */
   1064       1.1    jruoho                 if ((StartOfThisLine[1] != '#') &&
   1065       1.1    jruoho                     (*Beginning != '\\') &&
   1066       1.1    jruoho                     (*Beginning != '/') &&
   1067       1.1    jruoho                     (*Beginning != '{') &&
   1068       1.1    jruoho                     (*Beginning != '=') &&
   1069       1.1    jruoho                     (*Beginning != ','))
   1070       1.1    jruoho                 {
   1071       1.1    jruoho                     Beginning++;
   1072       1.1    jruoho                     SubBuffer++;
   1073       1.1    jruoho 
   1074       1.1    jruoho                     Gbl_MadeChanges = TRUE;
   1075       1.1    jruoho 
   1076       1.1    jruoho #ifdef ADD_EXTRA_WHITESPACE
   1077       1.1    jruoho                     AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
   1078       1.1    jruoho #else
   1079       1.1    jruoho                     /* Find non-whitespace start of next line */
   1080       1.1    jruoho 
   1081       1.1    jruoho                     Next = SubBuffer + 1;
   1082       1.1    jruoho                     while ((*Next == ' ')   ||
   1083       1.1    jruoho                            (*Next == '\t'))
   1084       1.1    jruoho                     {
   1085       1.1    jruoho                         Next++;
   1086       1.1    jruoho                     }
   1087       1.1    jruoho 
   1088       1.1    jruoho                     /* Find non-whitespace start of this line */
   1089       1.1    jruoho 
   1090       1.1    jruoho                     StartOfThisLine++;
   1091       1.1    jruoho                     while ((*StartOfThisLine == ' ')   ||
   1092       1.1    jruoho                            (*StartOfThisLine == '\t'))
   1093       1.1    jruoho                     {
   1094       1.1    jruoho                         StartOfThisLine++;
   1095       1.1    jruoho                     }
   1096       1.1    jruoho 
   1097       1.1    jruoho                     /*
   1098       1.1    jruoho                      * Must be a single-line comment to need more whitespace
   1099       1.1    jruoho                      * Even then, we don't need more if the previous statement
   1100       1.1    jruoho                      * is an "else".
   1101       1.1    jruoho                      */
   1102       1.1    jruoho                     if ((Next[0] == '/')  &&
   1103       1.1    jruoho                         (Next[1] == '*')  &&
   1104       1.1    jruoho                         (Next[2] != '\n') &&
   1105       1.1    jruoho 
   1106       1.1    jruoho                         (!strncmp (StartOfThisLine, "else if", 7)     ||
   1107       1.1    jruoho                          !strncmp (StartOfThisLine, "else while", 10) ||
   1108       1.1    jruoho                           strncmp (StartOfThisLine, "else", 4)))
   1109       1.1    jruoho                     {
   1110       1.1    jruoho                         AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
   1111       1.1    jruoho                     }
   1112       1.1    jruoho                     else
   1113       1.1    jruoho                     {
   1114       1.1    jruoho                         AsReplaceData (Beginning, SubBuffer - Beginning, " {", 2);
   1115       1.1    jruoho                     }
   1116       1.1    jruoho #endif
   1117       1.1    jruoho                 }
   1118       1.1    jruoho             }
   1119       1.1    jruoho         }
   1120       1.1    jruoho 
   1121       1.1    jruoho         SubBuffer++;
   1122       1.1    jruoho     }
   1123       1.1    jruoho }
   1124       1.1    jruoho 
   1125       1.1    jruoho 
   1126       1.1    jruoho /******************************************************************************
   1127       1.1    jruoho  *
   1128       1.1    jruoho  * FUNCTION:    AsTabify4
   1129       1.1    jruoho  *
   1130   1.1.1.3  christos  * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
   1131       1.1    jruoho  *              preserved.
   1132       1.1    jruoho  *
   1133       1.1    jruoho  ******************************************************************************/
   1134       1.1    jruoho 
   1135       1.1    jruoho void
   1136       1.1    jruoho AsTabify4 (
   1137       1.1    jruoho     char                    *Buffer)
   1138       1.1    jruoho {
   1139       1.1    jruoho     char                    *SubBuffer = Buffer;
   1140       1.1    jruoho     char                    *NewSubBuffer;
   1141       1.1    jruoho     UINT32                  SpaceCount = 0;
   1142       1.1    jruoho     UINT32                  Column = 0;
   1143       1.1    jruoho 
   1144       1.1    jruoho 
   1145       1.1    jruoho     while (*SubBuffer)
   1146       1.1    jruoho     {
   1147       1.1    jruoho         if (*SubBuffer == '\n')
   1148       1.1    jruoho         {
   1149       1.1    jruoho             Column = 0;
   1150       1.1    jruoho         }
   1151       1.1    jruoho         else
   1152       1.1    jruoho         {
   1153       1.1    jruoho             Column++;
   1154       1.1    jruoho         }
   1155       1.1    jruoho 
   1156       1.1    jruoho         /* Ignore comments */
   1157       1.1    jruoho 
   1158       1.1    jruoho         if ((SubBuffer[0] == '/') &&
   1159       1.1    jruoho             (SubBuffer[1] == '*'))
   1160       1.1    jruoho         {
   1161       1.1    jruoho             SubBuffer = strstr (SubBuffer, "*/");
   1162       1.1    jruoho             if (!SubBuffer)
   1163       1.1    jruoho             {
   1164       1.1    jruoho                 return;
   1165       1.1    jruoho             }
   1166       1.1    jruoho 
   1167       1.1    jruoho             SubBuffer += 2;
   1168       1.1    jruoho             continue;
   1169       1.1    jruoho         }
   1170       1.1    jruoho 
   1171       1.1    jruoho         /* Ignore quoted strings */
   1172       1.1    jruoho 
   1173       1.1    jruoho         if (*SubBuffer == '\"')
   1174       1.1    jruoho         {
   1175       1.1    jruoho             SubBuffer++;
   1176       1.1    jruoho             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
   1177       1.1    jruoho             if (!SubBuffer)
   1178       1.1    jruoho             {
   1179       1.1    jruoho                 return;
   1180       1.1    jruoho             }
   1181       1.1    jruoho             SpaceCount = 0;
   1182       1.1    jruoho         }
   1183       1.1    jruoho 
   1184       1.1    jruoho         if (*SubBuffer == ' ')
   1185       1.1    jruoho         {
   1186       1.1    jruoho             SpaceCount++;
   1187       1.1    jruoho 
   1188       1.1    jruoho             if (SpaceCount >= 4)
   1189       1.1    jruoho             {
   1190       1.1    jruoho                 SpaceCount = 0;
   1191       1.1    jruoho 
   1192       1.1    jruoho                 NewSubBuffer = (SubBuffer + 1) - 4;
   1193       1.1    jruoho                 *NewSubBuffer = '\t';
   1194       1.1    jruoho                 NewSubBuffer++;
   1195       1.1    jruoho 
   1196       1.1    jruoho                 /* Remove the spaces */
   1197       1.1    jruoho 
   1198       1.1    jruoho                 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
   1199       1.1    jruoho             }
   1200       1.1    jruoho 
   1201       1.1    jruoho             if ((Column % 4) == 0)
   1202       1.1    jruoho             {
   1203       1.1    jruoho                 SpaceCount = 0;
   1204       1.1    jruoho             }
   1205       1.1    jruoho         }
   1206       1.1    jruoho         else
   1207       1.1    jruoho         {
   1208       1.1    jruoho             SpaceCount = 0;
   1209       1.1    jruoho         }
   1210       1.1    jruoho 
   1211       1.1    jruoho         SubBuffer++;
   1212       1.1    jruoho     }
   1213       1.1    jruoho }
   1214       1.1    jruoho 
   1215       1.1    jruoho 
   1216       1.1    jruoho /******************************************************************************
   1217       1.1    jruoho  *
   1218       1.1    jruoho  * FUNCTION:    AsTabify8
   1219       1.1    jruoho  *
   1220   1.1.1.3  christos  * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
   1221       1.1    jruoho  *              preserved.
   1222       1.1    jruoho  *
   1223       1.1    jruoho  ******************************************************************************/
   1224       1.1    jruoho 
   1225       1.1    jruoho void
   1226       1.1    jruoho AsTabify8 (
   1227       1.1    jruoho     char                    *Buffer)
   1228       1.1    jruoho {
   1229       1.1    jruoho     char                    *SubBuffer = Buffer;
   1230       1.1    jruoho     char                    *NewSubBuffer;
   1231       1.1    jruoho     char                    *CommentEnd = NULL;
   1232       1.1    jruoho     UINT32                  SpaceCount = 0;
   1233       1.1    jruoho     UINT32                  TabCount = 0;
   1234       1.1    jruoho     UINT32                  LastLineTabCount = 0;
   1235       1.1    jruoho     UINT32                  LastLineColumnStart = 0;
   1236       1.1    jruoho     UINT32                  ThisColumnStart = 0;
   1237       1.1    jruoho     UINT32                  ThisTabCount =  0;
   1238       1.1    jruoho     char                    *FirstNonBlank = NULL;
   1239       1.1    jruoho 
   1240       1.1    jruoho 
   1241       1.1    jruoho     while (*SubBuffer)
   1242       1.1    jruoho     {
   1243       1.1    jruoho         if (*SubBuffer == '\n')
   1244       1.1    jruoho         {
   1245       1.1    jruoho             /* This is a standalone blank line */
   1246       1.1    jruoho 
   1247       1.1    jruoho             FirstNonBlank = NULL;
   1248       1.1    jruoho             SpaceCount = 0;
   1249       1.1    jruoho             TabCount = 0;
   1250       1.1    jruoho             SubBuffer++;
   1251       1.1    jruoho             continue;
   1252       1.1    jruoho         }
   1253       1.1    jruoho 
   1254       1.1    jruoho         if (!FirstNonBlank)
   1255       1.1    jruoho         {
   1256       1.1    jruoho             /* Find the first non-blank character on this line */
   1257       1.1    jruoho 
   1258       1.1    jruoho             FirstNonBlank = SubBuffer;
   1259       1.1    jruoho             while (*FirstNonBlank == ' ')
   1260       1.1    jruoho             {
   1261       1.1    jruoho                 FirstNonBlank++;
   1262       1.1    jruoho             }
   1263       1.1    jruoho 
   1264       1.1    jruoho             /*
   1265       1.1    jruoho              * This mechanism limits the difference in tab counts from
   1266   1.1.1.3  christos              * line to line. It helps avoid the situation where a second
   1267       1.1    jruoho              * continuation line (which was indented correctly for tabs=4) would
   1268       1.1    jruoho              * get indented off the screen if we just blindly converted to tabs.
   1269       1.1    jruoho              */
   1270       1.1    jruoho             ThisColumnStart = FirstNonBlank - SubBuffer;
   1271       1.1    jruoho 
   1272       1.1    jruoho             if (LastLineTabCount == 0)
   1273       1.1    jruoho             {
   1274       1.1    jruoho                 ThisTabCount = 0;
   1275       1.1    jruoho             }
   1276       1.1    jruoho             else if (ThisColumnStart == LastLineColumnStart)
   1277       1.1    jruoho             {
   1278       1.1    jruoho                 ThisTabCount = LastLineTabCount -1;
   1279       1.1    jruoho             }
   1280       1.1    jruoho             else
   1281       1.1    jruoho             {
   1282       1.1    jruoho                 ThisTabCount = LastLineTabCount + 1;
   1283       1.1    jruoho             }
   1284       1.1    jruoho         }
   1285       1.1    jruoho 
   1286       1.1    jruoho         /* Check if we are in a comment */
   1287       1.1    jruoho 
   1288       1.1    jruoho         if ((SubBuffer[0] == '*') &&
   1289       1.1    jruoho             (SubBuffer[1] == '/'))
   1290       1.1    jruoho         {
   1291       1.1    jruoho             SpaceCount = 0;
   1292       1.1    jruoho             SubBuffer += 2;
   1293       1.1    jruoho 
   1294       1.1    jruoho             if (*SubBuffer == '\n')
   1295       1.1    jruoho             {
   1296       1.1    jruoho                 if (TabCount > 0)
   1297       1.1    jruoho                 {
   1298       1.1    jruoho                     LastLineTabCount = TabCount;
   1299       1.1    jruoho                     TabCount = 0;
   1300       1.1    jruoho                 }
   1301   1.1.1.7  christos 
   1302       1.1    jruoho                 FirstNonBlank = NULL;
   1303       1.1    jruoho                 LastLineColumnStart = ThisColumnStart;
   1304       1.1    jruoho                 SubBuffer++;
   1305       1.1    jruoho             }
   1306       1.1    jruoho 
   1307       1.1    jruoho             continue;
   1308       1.1    jruoho         }
   1309       1.1    jruoho 
   1310       1.1    jruoho         /* Check for comment open */
   1311       1.1    jruoho 
   1312       1.1    jruoho         if ((SubBuffer[0] == '/') &&
   1313       1.1    jruoho             (SubBuffer[1] == '*'))
   1314       1.1    jruoho         {
   1315       1.1    jruoho             /* Find the end of the comment, it must exist */
   1316       1.1    jruoho 
   1317       1.1    jruoho             CommentEnd = strstr (SubBuffer, "*/");
   1318       1.1    jruoho             if (!CommentEnd)
   1319       1.1    jruoho             {
   1320       1.1    jruoho                 return;
   1321       1.1    jruoho             }
   1322       1.1    jruoho 
   1323       1.1    jruoho             /* Toss the rest of this line or single-line comment */
   1324       1.1    jruoho 
   1325       1.1    jruoho             while ((SubBuffer < CommentEnd) &&
   1326       1.1    jruoho                    (*SubBuffer != '\n'))
   1327       1.1    jruoho             {
   1328       1.1    jruoho                 SubBuffer++;
   1329       1.1    jruoho             }
   1330       1.1    jruoho 
   1331       1.1    jruoho             if (*SubBuffer == '\n')
   1332       1.1    jruoho             {
   1333       1.1    jruoho                 if (TabCount > 0)
   1334       1.1    jruoho                 {
   1335       1.1    jruoho                     LastLineTabCount = TabCount;
   1336       1.1    jruoho                     TabCount = 0;
   1337       1.1    jruoho                 }
   1338   1.1.1.7  christos 
   1339       1.1    jruoho                 FirstNonBlank = NULL;
   1340       1.1    jruoho                 LastLineColumnStart = ThisColumnStart;
   1341       1.1    jruoho             }
   1342       1.1    jruoho 
   1343       1.1    jruoho             SpaceCount = 0;
   1344       1.1    jruoho             continue;
   1345       1.1    jruoho         }
   1346       1.1    jruoho 
   1347       1.1    jruoho         /* Ignore quoted strings */
   1348       1.1    jruoho 
   1349       1.1    jruoho         if ((!CommentEnd) && (*SubBuffer == '\"'))
   1350       1.1    jruoho         {
   1351       1.1    jruoho             SubBuffer++;
   1352       1.1    jruoho             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
   1353       1.1    jruoho             if (!SubBuffer)
   1354       1.1    jruoho             {
   1355       1.1    jruoho                 return;
   1356       1.1    jruoho             }
   1357   1.1.1.7  christos 
   1358       1.1    jruoho             SpaceCount = 0;
   1359       1.1    jruoho         }
   1360       1.1    jruoho 
   1361       1.1    jruoho         if (*SubBuffer != ' ')
   1362       1.1    jruoho         {
   1363       1.1    jruoho             /* Not a space, skip to end of line */
   1364       1.1    jruoho 
   1365       1.1    jruoho             SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
   1366       1.1    jruoho             if (!SubBuffer)
   1367       1.1    jruoho             {
   1368       1.1    jruoho                 return;
   1369       1.1    jruoho             }
   1370       1.1    jruoho             if (TabCount > 0)
   1371       1.1    jruoho             {
   1372       1.1    jruoho                 LastLineTabCount = TabCount;
   1373       1.1    jruoho                 TabCount = 0;
   1374       1.1    jruoho             }
   1375       1.1    jruoho 
   1376       1.1    jruoho             FirstNonBlank = NULL;
   1377       1.1    jruoho             LastLineColumnStart = ThisColumnStart;
   1378       1.1    jruoho             SpaceCount = 0;
   1379       1.1    jruoho         }
   1380       1.1    jruoho         else
   1381       1.1    jruoho         {
   1382       1.1    jruoho             /* Another space */
   1383       1.1    jruoho 
   1384       1.1    jruoho             SpaceCount++;
   1385       1.1    jruoho 
   1386       1.1    jruoho             if (SpaceCount >= 4)
   1387       1.1    jruoho             {
   1388       1.1    jruoho                 /* Replace this group of spaces with a tab character */
   1389       1.1    jruoho 
   1390       1.1    jruoho                 SpaceCount = 0;
   1391       1.1    jruoho 
   1392       1.1    jruoho                 NewSubBuffer = SubBuffer - 3;
   1393       1.1    jruoho 
   1394       1.1    jruoho                 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
   1395       1.1    jruoho                 {
   1396       1.1    jruoho                     *NewSubBuffer = '\t';
   1397       1.1    jruoho                     NewSubBuffer++;
   1398       1.1    jruoho                     SubBuffer++;
   1399       1.1    jruoho                     TabCount++;
   1400       1.1    jruoho                 }
   1401       1.1    jruoho 
   1402       1.1    jruoho                 /* Remove the spaces */
   1403       1.1    jruoho 
   1404       1.1    jruoho                 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
   1405       1.1    jruoho                 continue;
   1406       1.1    jruoho             }
   1407       1.1    jruoho         }
   1408       1.1    jruoho 
   1409       1.1    jruoho         SubBuffer++;
   1410       1.1    jruoho     }
   1411       1.1    jruoho }
   1412       1.1    jruoho 
   1413       1.1    jruoho 
   1414       1.1    jruoho /******************************************************************************
   1415       1.1    jruoho  *
   1416       1.1    jruoho  * FUNCTION:    AsCountLines
   1417       1.1    jruoho  *
   1418   1.1.1.3  christos  * DESCRIPTION: Count the number of lines in the input buffer. Also count
   1419       1.1    jruoho  *              the number of long lines (lines longer than 80 chars).
   1420       1.1    jruoho  *
   1421       1.1    jruoho  ******************************************************************************/
   1422       1.1    jruoho 
   1423   1.1.1.4  christos static UINT32
   1424       1.1    jruoho AsCountLines (
   1425       1.1    jruoho     char                    *Buffer,
   1426       1.1    jruoho     char                    *Filename)
   1427       1.1    jruoho {
   1428       1.1    jruoho     char                    *SubBuffer = Buffer;
   1429       1.1    jruoho     char                    *EndOfLine;
   1430       1.1    jruoho     UINT32                  LineCount = 0;
   1431       1.1    jruoho     UINT32                  LongLineCount = 0;
   1432       1.1    jruoho 
   1433       1.1    jruoho 
   1434       1.1    jruoho     while (*SubBuffer)
   1435       1.1    jruoho     {
   1436       1.1    jruoho         EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
   1437       1.1    jruoho         if (!EndOfLine)
   1438       1.1    jruoho         {
   1439       1.1    jruoho             Gbl_TotalLines += LineCount;
   1440   1.1.1.3  christos             return (LineCount);
   1441       1.1    jruoho         }
   1442       1.1    jruoho 
   1443       1.1    jruoho         if ((EndOfLine - SubBuffer) > 80)
   1444       1.1    jruoho         {
   1445       1.1    jruoho             LongLineCount++;
   1446       1.1    jruoho             VERBOSE_PRINT (("long: %.80s\n", SubBuffer));
   1447       1.1    jruoho         }
   1448       1.1    jruoho 
   1449       1.1    jruoho         LineCount++;
   1450       1.1    jruoho         SubBuffer = EndOfLine + 1;
   1451       1.1    jruoho     }
   1452       1.1    jruoho 
   1453       1.1    jruoho     if (LongLineCount)
   1454       1.1    jruoho     {
   1455   1.1.1.4  christos         VERBOSE_PRINT (("%u Lines longer than 80 found in %s\n",
   1456   1.1.1.4  christos             LongLineCount, Filename));
   1457   1.1.1.4  christos 
   1458       1.1    jruoho         Gbl_LongLines += LongLineCount;
   1459       1.1    jruoho     }
   1460       1.1    jruoho 
   1461       1.1    jruoho     Gbl_TotalLines += LineCount;
   1462   1.1.1.3  christos     return (LineCount);
   1463       1.1    jruoho }
   1464       1.1    jruoho 
   1465       1.1    jruoho 
   1466       1.1    jruoho /******************************************************************************
   1467       1.1    jruoho  *
   1468       1.1    jruoho  * FUNCTION:    AsCountTabs
   1469       1.1    jruoho  *
   1470       1.1    jruoho  * DESCRIPTION: Simply count the number of tabs in the input file buffer
   1471       1.1    jruoho  *
   1472       1.1    jruoho  ******************************************************************************/
   1473       1.1    jruoho 
   1474       1.1    jruoho void
   1475       1.1    jruoho AsCountTabs (
   1476       1.1    jruoho     char                    *Buffer,
   1477       1.1    jruoho     char                    *Filename)
   1478       1.1    jruoho {
   1479       1.1    jruoho     UINT32                  i;
   1480       1.1    jruoho     UINT32                  TabCount = 0;
   1481       1.1    jruoho 
   1482       1.1    jruoho 
   1483       1.1    jruoho     for (i = 0; Buffer[i]; i++)
   1484       1.1    jruoho     {
   1485       1.1    jruoho         if (Buffer[i] == '\t')
   1486       1.1    jruoho         {
   1487       1.1    jruoho             TabCount++;
   1488       1.1    jruoho         }
   1489       1.1    jruoho     }
   1490       1.1    jruoho 
   1491       1.1    jruoho     if (TabCount)
   1492       1.1    jruoho     {
   1493       1.1    jruoho         AsPrint ("Tabs found", TabCount, Filename);
   1494       1.1    jruoho         Gbl_Tabs += TabCount;
   1495       1.1    jruoho     }
   1496       1.1    jruoho 
   1497       1.1    jruoho     AsCountLines (Buffer, Filename);
   1498       1.1    jruoho }
   1499       1.1    jruoho 
   1500       1.1    jruoho 
   1501       1.1    jruoho /******************************************************************************
   1502       1.1    jruoho  *
   1503       1.1    jruoho  * FUNCTION:    AsCountSourceLines
   1504       1.1    jruoho  *
   1505   1.1.1.3  christos  * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
   1506       1.1    jruoho  *              comment, and 2) not a blank line.
   1507       1.1    jruoho  *
   1508       1.1    jruoho  ******************************************************************************/
   1509       1.1    jruoho 
   1510       1.1    jruoho void
   1511       1.1    jruoho AsCountSourceLines (
   1512       1.1    jruoho     char                    *Buffer,
   1513       1.1    jruoho     char                    *Filename)
   1514       1.1    jruoho {
   1515       1.1    jruoho     char                    *SubBuffer = Buffer;
   1516       1.1    jruoho     UINT32                  LineCount = 0;
   1517       1.1    jruoho     UINT32                  WhiteCount = 0;
   1518       1.1    jruoho     UINT32                  CommentCount = 0;
   1519       1.1    jruoho 
   1520       1.1    jruoho 
   1521       1.1    jruoho     while (*SubBuffer)
   1522       1.1    jruoho     {
   1523       1.1    jruoho         /* Detect comments (// comments are not used, non-ansii) */
   1524       1.1    jruoho 
   1525       1.1    jruoho         if ((SubBuffer[0] == '/') &&
   1526       1.1    jruoho             (SubBuffer[1] == '*'))
   1527       1.1    jruoho         {
   1528       1.1    jruoho             SubBuffer += 2;
   1529       1.1    jruoho 
   1530       1.1    jruoho             /* First line of multi-line comment is often just whitespace */
   1531       1.1    jruoho 
   1532       1.1    jruoho             if (SubBuffer[0] == '\n')
   1533       1.1    jruoho             {
   1534       1.1    jruoho                 WhiteCount++;
   1535       1.1    jruoho                 SubBuffer++;
   1536       1.1    jruoho             }
   1537       1.1    jruoho             else
   1538       1.1    jruoho             {
   1539       1.1    jruoho                 CommentCount++;
   1540       1.1    jruoho             }
   1541       1.1    jruoho 
   1542       1.1    jruoho             /* Find end of comment */
   1543       1.1    jruoho 
   1544       1.1    jruoho             while (SubBuffer[0] && SubBuffer[1] &&
   1545   1.1.1.7  christos                 !(((SubBuffer[0] == '*') &&
   1546   1.1.1.7  christos                     (SubBuffer[1] == '/'))))
   1547       1.1    jruoho             {
   1548       1.1    jruoho                 if (SubBuffer[0] == '\n')
   1549       1.1    jruoho                 {
   1550       1.1    jruoho                     CommentCount++;
   1551       1.1    jruoho                 }
   1552       1.1    jruoho 
   1553       1.1    jruoho                 SubBuffer++;
   1554       1.1    jruoho             }
   1555       1.1    jruoho         }
   1556       1.1    jruoho 
   1557       1.1    jruoho         /* A linefeed followed by a non-linefeed is a valid source line */
   1558       1.1    jruoho 
   1559       1.1    jruoho         else if ((SubBuffer[0] == '\n') &&
   1560       1.1    jruoho                  (SubBuffer[1] != '\n'))
   1561       1.1    jruoho         {
   1562       1.1    jruoho             LineCount++;
   1563       1.1    jruoho         }
   1564       1.1    jruoho 
   1565       1.1    jruoho         /* Two back-to-back linefeeds indicate a whitespace line */
   1566       1.1    jruoho 
   1567       1.1    jruoho         else if ((SubBuffer[0] == '\n') &&
   1568       1.1    jruoho                  (SubBuffer[1] == '\n'))
   1569       1.1    jruoho         {
   1570       1.1    jruoho             WhiteCount++;
   1571       1.1    jruoho         }
   1572       1.1    jruoho 
   1573       1.1    jruoho         SubBuffer++;
   1574       1.1    jruoho     }
   1575       1.1    jruoho 
   1576       1.1    jruoho     /* Adjust comment count for legal header */
   1577       1.1    jruoho 
   1578       1.1    jruoho     if (Gbl_HeaderSize < CommentCount)
   1579       1.1    jruoho     {
   1580       1.1    jruoho         CommentCount -= Gbl_HeaderSize;
   1581       1.1    jruoho         Gbl_HeaderLines += Gbl_HeaderSize;
   1582       1.1    jruoho     }
   1583       1.1    jruoho 
   1584       1.1    jruoho     Gbl_SourceLines += LineCount;
   1585       1.1    jruoho     Gbl_WhiteLines += WhiteCount;
   1586       1.1    jruoho     Gbl_CommentLines += CommentCount;
   1587       1.1    jruoho 
   1588       1.1    jruoho     VERBOSE_PRINT (("%u Comment %u White %u Code %u Lines in %s\n",
   1589   1.1.1.4  christos         CommentCount, WhiteCount, LineCount,
   1590   1.1.1.4  christos         LineCount + WhiteCount + CommentCount, Filename));
   1591       1.1    jruoho }
   1592       1.1    jruoho 
   1593       1.1    jruoho 
   1594       1.1    jruoho /******************************************************************************
   1595       1.1    jruoho  *
   1596       1.1    jruoho  * FUNCTION:    AsInsertPrefix
   1597       1.1    jruoho  *
   1598       1.1    jruoho  * DESCRIPTION: Insert struct or union prefixes
   1599       1.1    jruoho  *
   1600       1.1    jruoho  ******************************************************************************/
   1601       1.1    jruoho 
   1602       1.1    jruoho void
   1603       1.1    jruoho AsInsertPrefix (
   1604       1.1    jruoho     char                    *Buffer,
   1605       1.1    jruoho     char                    *Keyword,
   1606       1.1    jruoho     UINT8                   Type)
   1607       1.1    jruoho {
   1608       1.1    jruoho     char                    *SubString;
   1609       1.1    jruoho     char                    *SubBuffer;
   1610       1.1    jruoho     char                    *EndKeyword;
   1611       1.1    jruoho     int                     InsertLength;
   1612       1.1    jruoho     char                    *InsertString;
   1613       1.1    jruoho     int                     TrailingSpaces;
   1614       1.1    jruoho     char                    LowerKeyword[128];
   1615       1.1    jruoho     int                     KeywordLength;
   1616  1.1.1.15  christos     char                    *LineStart;
   1617  1.1.1.15  christos     BOOLEAN                 FoundPrefix;
   1618       1.1    jruoho 
   1619       1.1    jruoho 
   1620       1.1    jruoho     switch (Type)
   1621       1.1    jruoho     {
   1622       1.1    jruoho     case SRC_TYPE_STRUCT:
   1623   1.1.1.3  christos 
   1624       1.1    jruoho         InsertString = "struct ";
   1625       1.1    jruoho         break;
   1626       1.1    jruoho 
   1627       1.1    jruoho     case SRC_TYPE_UNION:
   1628   1.1.1.3  christos 
   1629       1.1    jruoho         InsertString = "union ";
   1630       1.1    jruoho         break;
   1631       1.1    jruoho 
   1632       1.1    jruoho     default:
   1633   1.1.1.3  christos 
   1634       1.1    jruoho         return;
   1635       1.1    jruoho     }
   1636       1.1    jruoho 
   1637       1.1    jruoho     strcpy (LowerKeyword, Keyword);
   1638   1.1.1.6  christos     AcpiUtStrlwr (LowerKeyword);
   1639       1.1    jruoho 
   1640       1.1    jruoho     SubBuffer = Buffer;
   1641       1.1    jruoho     SubString = Buffer;
   1642       1.1    jruoho     InsertLength = strlen (InsertString);
   1643       1.1    jruoho     KeywordLength = strlen (Keyword);
   1644       1.1    jruoho 
   1645       1.1    jruoho 
   1646       1.1    jruoho     while (SubString)
   1647       1.1    jruoho     {
   1648       1.1    jruoho         /* Find an instance of the keyword */
   1649       1.1    jruoho 
   1650       1.1    jruoho         SubString = strstr (SubBuffer, LowerKeyword);
   1651       1.1    jruoho         if (!SubString)
   1652       1.1    jruoho         {
   1653       1.1    jruoho             return;
   1654       1.1    jruoho         }
   1655       1.1    jruoho 
   1656       1.1    jruoho         SubBuffer = SubString;
   1657       1.1    jruoho 
   1658       1.1    jruoho         /* Must be standalone word, not a substring */
   1659       1.1    jruoho 
   1660       1.1    jruoho         if (AsMatchExactWord (SubString, KeywordLength))
   1661       1.1    jruoho         {
   1662       1.1    jruoho             /* Make sure the keyword isn't already prefixed with the insert */
   1663       1.1    jruoho 
   1664  1.1.1.15  christos             /* We find the beginning of the line and try to find the InsertString
   1665  1.1.1.15  christos              * from LineStart up to SubBuffer (our keyword). If it's not there,
   1666  1.1.1.15  christos              * we assume it doesn't have a prefix; this is a limitation, as having
   1667  1.1.1.15  christos              * a keyword on another line is absolutely valid C.
   1668  1.1.1.15  christos              */
   1669  1.1.1.15  christos 
   1670  1.1.1.15  christos             LineStart = SubString;
   1671  1.1.1.15  christos             FoundPrefix = FALSE;
   1672  1.1.1.15  christos 
   1673  1.1.1.15  christos             /* Find the start of the line */
   1674  1.1.1.15  christos 
   1675  1.1.1.15  christos             while (LineStart > Buffer)
   1676  1.1.1.15  christos             {
   1677  1.1.1.15  christos                 if (*LineStart == '\n')
   1678  1.1.1.15  christos                 {
   1679  1.1.1.15  christos                     LineStart++;
   1680  1.1.1.15  christos                     break;
   1681  1.1.1.15  christos                 }
   1682  1.1.1.15  christos 
   1683  1.1.1.15  christos                 LineStart--;
   1684  1.1.1.15  christos             }
   1685  1.1.1.15  christos 
   1686  1.1.1.15  christos             /* Try to find InsertString from the start of the line up to SubBuffer */
   1687  1.1.1.15  christos             /* Note that this algorithm is a bit naive. */
   1688  1.1.1.15  christos 
   1689  1.1.1.15  christos             while (SubBuffer > LineStart)
   1690  1.1.1.15  christos             {
   1691  1.1.1.15  christos                 if (*LineStart != *InsertString)
   1692  1.1.1.15  christos                 {
   1693  1.1.1.15  christos                     LineStart++;
   1694  1.1.1.15  christos                     continue;
   1695  1.1.1.15  christos                 }
   1696  1.1.1.15  christos 
   1697  1.1.1.15  christos                 if (strncmp (LineStart++, InsertString, InsertLength))
   1698  1.1.1.15  christos                 {
   1699  1.1.1.15  christos                     continue;
   1700  1.1.1.15  christos                 }
   1701  1.1.1.15  christos 
   1702  1.1.1.15  christos                 FoundPrefix = TRUE;
   1703  1.1.1.15  christos                 LineStart += InsertLength - 1;
   1704  1.1.1.15  christos 
   1705  1.1.1.15  christos                 /* Now check if there's non-whitespace between InsertString and SubBuffer, as that
   1706  1.1.1.15  christos                  * means it's not a valid prefix in this case. */
   1707  1.1.1.15  christos 
   1708  1.1.1.15  christos                 while (LineStart != SubBuffer)
   1709  1.1.1.15  christos                 {
   1710  1.1.1.15  christos                     if (!strchr (" \t\r\n", *LineStart))
   1711  1.1.1.15  christos                     {
   1712  1.1.1.15  christos                         /* We found non-whitespace while traversing up to SubBuffer,
   1713  1.1.1.15  christos                          * so this isn't a prefix.
   1714  1.1.1.15  christos                          */
   1715  1.1.1.15  christos                         FoundPrefix = FALSE;
   1716  1.1.1.15  christos                         break;
   1717  1.1.1.15  christos                     }
   1718  1.1.1.15  christos 
   1719  1.1.1.15  christos                     LineStart++;
   1720  1.1.1.15  christos                 }
   1721  1.1.1.15  christos             }
   1722  1.1.1.15  christos 
   1723  1.1.1.15  christos             if (FoundPrefix)
   1724       1.1    jruoho             {
   1725       1.1    jruoho                 /* Add spaces if not already at the end-of-line */
   1726       1.1    jruoho 
   1727       1.1    jruoho                 if (*(SubBuffer + KeywordLength) != '\n')
   1728       1.1    jruoho                 {
   1729       1.1    jruoho                     /* Already present, add spaces after to align structure members */
   1730       1.1    jruoho 
   1731       1.1    jruoho #if 0
   1732       1.1    jruoho /* ONLY FOR C FILES */
   1733       1.1    jruoho                     AsInsertData (SubBuffer + KeywordLength, "        ", 8);
   1734       1.1    jruoho #endif
   1735       1.1    jruoho                 }
   1736       1.1    jruoho                 goto Next;
   1737       1.1    jruoho             }
   1738       1.1    jruoho 
   1739       1.1    jruoho             /* Make sure the keyword isn't at the end of a struct/union */
   1740       1.1    jruoho             /* Note: This code depends on a single space after the brace */
   1741       1.1    jruoho 
   1742       1.1    jruoho             if (*(SubString - 2) == '}')
   1743       1.1    jruoho             {
   1744       1.1    jruoho                 goto Next;
   1745       1.1    jruoho             }
   1746       1.1    jruoho 
   1747       1.1    jruoho             /* Prefix the keyword with the insert string */
   1748       1.1    jruoho 
   1749       1.1    jruoho             Gbl_MadeChanges = TRUE;
   1750       1.1    jruoho 
   1751       1.1    jruoho             /* Is there room for insertion */
   1752       1.1    jruoho 
   1753       1.1    jruoho             EndKeyword = SubString + strlen (LowerKeyword);
   1754       1.1    jruoho 
   1755       1.1    jruoho             TrailingSpaces = 0;
   1756       1.1    jruoho             while (EndKeyword[TrailingSpaces] == ' ')
   1757       1.1    jruoho             {
   1758       1.1    jruoho                 TrailingSpaces++;
   1759       1.1    jruoho             }
   1760       1.1    jruoho 
   1761       1.1    jruoho             /*
   1762       1.1    jruoho              * Use "if (TrailingSpaces > 1)" if we want to ignore casts
   1763       1.1    jruoho              */
   1764       1.1    jruoho             SubBuffer = SubString + InsertLength;
   1765       1.1    jruoho 
   1766       1.1    jruoho             if (TrailingSpaces > InsertLength)
   1767       1.1    jruoho             {
   1768       1.1    jruoho                 /* Insert the keyword */
   1769       1.1    jruoho 
   1770       1.1    jruoho                 memmove (SubBuffer, SubString, KeywordLength);
   1771       1.1    jruoho 
   1772       1.1    jruoho                 /* Insert the keyword */
   1773       1.1    jruoho 
   1774       1.1    jruoho                 memmove (SubString, InsertString, InsertLength);
   1775       1.1    jruoho             }
   1776       1.1    jruoho             else
   1777       1.1    jruoho             {
   1778       1.1    jruoho                 AsInsertData (SubString, InsertString, InsertLength);
   1779       1.1    jruoho             }
   1780       1.1    jruoho         }
   1781       1.1    jruoho 
   1782       1.1    jruoho Next:
   1783       1.1    jruoho         SubBuffer += KeywordLength;
   1784       1.1    jruoho     }
   1785       1.1    jruoho }
   1786       1.1    jruoho 
   1787       1.1    jruoho #ifdef ACPI_FUTURE_IMPLEMENTATION
   1788       1.1    jruoho /******************************************************************************
   1789       1.1    jruoho  *
   1790       1.1    jruoho  * FUNCTION:    AsTrimComments
   1791       1.1    jruoho  *
   1792       1.1    jruoho  * DESCRIPTION: Finds 3-line comments with only a single line of text
   1793       1.1    jruoho  *
   1794       1.1    jruoho  ******************************************************************************/
   1795       1.1    jruoho 
   1796       1.1    jruoho void
   1797       1.1    jruoho AsTrimComments (
   1798       1.1    jruoho     char                    *Buffer,
   1799       1.1    jruoho     char                    *Filename)
   1800       1.1    jruoho {
   1801       1.1    jruoho     char                    *SubBuffer = Buffer;
   1802       1.1    jruoho     char                    *Ptr1;
   1803       1.1    jruoho     char                    *Ptr2;
   1804       1.1    jruoho     UINT32                  LineCount;
   1805       1.1    jruoho     UINT32                  ShortCommentCount = 0;
   1806       1.1    jruoho 
   1807       1.1    jruoho 
   1808       1.1    jruoho     while (1)
   1809       1.1    jruoho     {
   1810       1.1    jruoho         /* Find comment open, within procedure level */
   1811       1.1    jruoho 
   1812       1.1    jruoho         SubBuffer = strstr (SubBuffer, "    /*");
   1813       1.1    jruoho         if (!SubBuffer)
   1814       1.1    jruoho         {
   1815       1.1    jruoho             goto Exit;
   1816       1.1    jruoho         }
   1817       1.1    jruoho 
   1818       1.1    jruoho         /* Find comment terminator */
   1819       1.1    jruoho 
   1820       1.1    jruoho         Ptr1 = strstr (SubBuffer, "*/");
   1821       1.1    jruoho         if (!Ptr1)
   1822       1.1    jruoho         {
   1823       1.1    jruoho             goto Exit;
   1824       1.1    jruoho         }
   1825       1.1    jruoho 
   1826       1.1    jruoho         /* Find next EOL (from original buffer) */
   1827       1.1    jruoho 
   1828       1.1    jruoho         Ptr2 = strstr (SubBuffer, "\n");
   1829       1.1    jruoho         if (!Ptr2)
   1830       1.1    jruoho         {
   1831       1.1    jruoho             goto Exit;
   1832       1.1    jruoho         }
   1833       1.1    jruoho 
   1834       1.1    jruoho         /* Ignore one-line comments */
   1835       1.1    jruoho 
   1836       1.1    jruoho         if (Ptr1 < Ptr2)
   1837       1.1    jruoho         {
   1838       1.1    jruoho             /* Normal comment, ignore and continue; */
   1839       1.1    jruoho 
   1840       1.1    jruoho             SubBuffer = Ptr2;
   1841       1.1    jruoho             continue;
   1842       1.1    jruoho         }
   1843       1.1    jruoho 
   1844       1.1    jruoho         /* Examine multi-line comment */
   1845       1.1    jruoho 
   1846       1.1    jruoho         LineCount = 1;
   1847       1.1    jruoho         while (Ptr1 > Ptr2)
   1848       1.1    jruoho         {
   1849       1.1    jruoho             /* Find next EOL */
   1850       1.1    jruoho 
   1851       1.1    jruoho             Ptr2++;
   1852       1.1    jruoho             Ptr2 = strstr (Ptr2, "\n");
   1853       1.1    jruoho             if (!Ptr2)
   1854       1.1    jruoho             {
   1855       1.1    jruoho                 goto Exit;
   1856       1.1    jruoho             }
   1857       1.1    jruoho 
   1858       1.1    jruoho             LineCount++;
   1859       1.1    jruoho         }
   1860       1.1    jruoho 
   1861       1.1    jruoho         SubBuffer = Ptr1;
   1862       1.1    jruoho 
   1863       1.1    jruoho         if (LineCount <= 3)
   1864       1.1    jruoho         {
   1865       1.1    jruoho             ShortCommentCount++;
   1866       1.1    jruoho         }
   1867       1.1    jruoho     }
   1868       1.1    jruoho 
   1869       1.1    jruoho 
   1870       1.1    jruoho Exit:
   1871       1.1    jruoho 
   1872       1.1    jruoho     if (ShortCommentCount)
   1873       1.1    jruoho     {
   1874       1.1    jruoho         AsPrint ("Short Comments found", ShortCommentCount, Filename);
   1875       1.1    jruoho     }
   1876       1.1    jruoho }
   1877       1.1    jruoho #endif
   1878   1.1.1.4  christos 
   1879   1.1.1.4  christos #ifdef ACPI_UNUSED_FUNCTIONS
   1880   1.1.1.4  christos /******************************************************************************
   1881   1.1.1.4  christos  *
   1882   1.1.1.4  christos  * FUNCTION:    AsCheckAndSkipLiterals
   1883   1.1.1.4  christos  *
   1884   1.1.1.4  christos  * DESCRIPTION: Generic routine to skip comments and quoted string literals.
   1885   1.1.1.4  christos  *              Keeps a line count.
   1886   1.1.1.4  christos  *
   1887   1.1.1.4  christos  ******************************************************************************/
   1888   1.1.1.4  christos 
   1889   1.1.1.4  christos static char *
   1890   1.1.1.4  christos AsCheckAndSkipLiterals (
   1891   1.1.1.4  christos     char                    *Buffer,
   1892   1.1.1.4  christos     UINT32                  *TotalLines);
   1893   1.1.1.4  christos 
   1894   1.1.1.4  christos 
   1895   1.1.1.4  christos static char *
   1896   1.1.1.4  christos AsCheckAndSkipLiterals (
   1897   1.1.1.4  christos     char                    *Buffer,
   1898   1.1.1.4  christos     UINT32                  *TotalLines)
   1899   1.1.1.4  christos {
   1900   1.1.1.4  christos     UINT32                  NewLines = 0;
   1901   1.1.1.4  christos     char                    *SubBuffer = Buffer;
   1902   1.1.1.4  christos     char                    *LiteralEnd;
   1903   1.1.1.4  christos 
   1904   1.1.1.4  christos 
   1905   1.1.1.4  christos     /* Ignore comments */
   1906   1.1.1.4  christos 
   1907   1.1.1.4  christos     if ((SubBuffer[0] == '/') &&
   1908   1.1.1.4  christos         (SubBuffer[1] == '*'))
   1909   1.1.1.4  christos     {
   1910   1.1.1.4  christos         LiteralEnd = strstr (SubBuffer, "*/");
   1911   1.1.1.4  christos         SubBuffer += 2;     /* Get past comment opening */
   1912   1.1.1.4  christos 
   1913   1.1.1.4  christos         if (!LiteralEnd)
   1914   1.1.1.4  christos         {
   1915   1.1.1.4  christos             return (SubBuffer);
   1916   1.1.1.4  christos         }
   1917   1.1.1.4  christos 
   1918   1.1.1.4  christos         while (SubBuffer < LiteralEnd)
   1919   1.1.1.4  christos         {
   1920   1.1.1.4  christos             if (*SubBuffer == '\n')
   1921   1.1.1.4  christos             {
   1922   1.1.1.4  christos                 NewLines++;
   1923   1.1.1.4  christos             }
   1924   1.1.1.4  christos 
   1925   1.1.1.4  christos             SubBuffer++;
   1926   1.1.1.4  christos         }
   1927   1.1.1.4  christos 
   1928   1.1.1.4  christos         SubBuffer += 2;     /* Get past comment close */
   1929   1.1.1.4  christos     }
   1930   1.1.1.4  christos 
   1931   1.1.1.4  christos     /* Ignore quoted strings */
   1932   1.1.1.4  christos 
   1933   1.1.1.4  christos     else if (*SubBuffer == '\"')
   1934   1.1.1.4  christos     {
   1935   1.1.1.4  christos         SubBuffer++;
   1936   1.1.1.4  christos         LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
   1937   1.1.1.4  christos         if (!LiteralEnd)
   1938   1.1.1.4  christos         {
   1939   1.1.1.4  christos             return (SubBuffer);
   1940   1.1.1.4  christos         }
   1941   1.1.1.4  christos     }
   1942   1.1.1.4  christos 
   1943   1.1.1.4  christos     if (TotalLines)
   1944   1.1.1.4  christos     {
   1945   1.1.1.4  christos         (*TotalLines) += NewLines;
   1946   1.1.1.4  christos     }
   1947   1.1.1.4  christos     return (SubBuffer);
   1948   1.1.1.4  christos }
   1949   1.1.1.4  christos #endif
   1950