Home | History | Annotate | Line # | Download | only in utilities
utprint.c revision 1.1.1.5.2.1
      1          1.1  christos /******************************************************************************
      2          1.1  christos  *
      3          1.1  christos  * Module Name: utprint - Formatted printing routines
      4          1.1  christos  *
      5          1.1  christos  *****************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8      1.1.1.4  christos  * Copyright (C) 2000 - 2016, Intel Corp.
      9          1.1  christos  * All rights reserved.
     10          1.1  christos  *
     11          1.1  christos  * Redistribution and use in source and binary forms, with or without
     12          1.1  christos  * modification, are permitted provided that the following conditions
     13          1.1  christos  * are met:
     14          1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15          1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16          1.1  christos  *    without modification.
     17          1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18          1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19          1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20          1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21          1.1  christos  *    binary redistribution.
     22          1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23          1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24          1.1  christos  *    from this software without specific prior written permission.
     25          1.1  christos  *
     26          1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27          1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28          1.1  christos  * Software Foundation.
     29          1.1  christos  *
     30          1.1  christos  * NO WARRANTY
     31          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35          1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36          1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37          1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38          1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39          1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40          1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41          1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42          1.1  christos  */
     43          1.1  christos #include "acpi.h"
     44          1.1  christos #include "accommon.h"
     45          1.1  christos 
     46          1.1  christos #define _COMPONENT          ACPI_UTILITIES
     47          1.1  christos         ACPI_MODULE_NAME    ("utprint")
     48          1.1  christos 
     49          1.1  christos 
     50          1.1  christos #define ACPI_FORMAT_SIGN            0x01
     51          1.1  christos #define ACPI_FORMAT_SIGN_PLUS       0x02
     52          1.1  christos #define ACPI_FORMAT_SIGN_PLUS_SPACE 0x04
     53          1.1  christos #define ACPI_FORMAT_ZERO            0x08
     54          1.1  christos #define ACPI_FORMAT_LEFT            0x10
     55          1.1  christos #define ACPI_FORMAT_UPPER           0x20
     56          1.1  christos #define ACPI_FORMAT_PREFIX          0x40
     57          1.1  christos 
     58          1.1  christos 
     59          1.1  christos /* Local prototypes */
     60          1.1  christos 
     61          1.1  christos static ACPI_SIZE
     62          1.1  christos AcpiUtBoundStringLength (
     63          1.1  christos     const char              *String,
     64          1.1  christos     ACPI_SIZE               Count);
     65          1.1  christos 
     66          1.1  christos static char *
     67          1.1  christos AcpiUtBoundStringOutput (
     68          1.1  christos     char                    *String,
     69          1.1  christos     const char              *End,
     70          1.1  christos     char                    c);
     71          1.1  christos 
     72          1.1  christos static char *
     73          1.1  christos AcpiUtFormatNumber (
     74          1.1  christos     char                    *String,
     75          1.1  christos     char                    *End,
     76          1.1  christos     UINT64                  Number,
     77          1.1  christos     UINT8                   Base,
     78          1.1  christos     INT32                   Width,
     79          1.1  christos     INT32                   Precision,
     80          1.1  christos     UINT8                   Type);
     81          1.1  christos 
     82          1.1  christos static char *
     83          1.1  christos AcpiUtPutNumber (
     84          1.1  christos     char                    *String,
     85          1.1  christos     UINT64                  Number,
     86          1.1  christos     UINT8                   Base,
     87          1.1  christos     BOOLEAN                 Upper);
     88          1.1  christos 
     89          1.1  christos 
     90          1.1  christos /*******************************************************************************
     91          1.1  christos  *
     92          1.1  christos  * FUNCTION:    AcpiUtBoundStringLength
     93          1.1  christos  *
     94          1.1  christos  * PARAMETERS:  String              - String with boundary
     95          1.1  christos  *              Count               - Boundary of the string
     96          1.1  christos  *
     97          1.1  christos  * RETURN:      Length of the string. Less than or equal to Count.
     98          1.1  christos  *
     99          1.1  christos  * DESCRIPTION: Calculate the length of a string with boundary.
    100          1.1  christos  *
    101          1.1  christos  ******************************************************************************/
    102          1.1  christos 
    103          1.1  christos static ACPI_SIZE
    104          1.1  christos AcpiUtBoundStringLength (
    105          1.1  christos     const char              *String,
    106          1.1  christos     ACPI_SIZE               Count)
    107          1.1  christos {
    108          1.1  christos     UINT32                  Length = 0;
    109          1.1  christos 
    110          1.1  christos 
    111          1.1  christos     while (*String && Count)
    112          1.1  christos     {
    113          1.1  christos         Length++;
    114          1.1  christos         String++;
    115          1.1  christos         Count--;
    116          1.1  christos     }
    117          1.1  christos 
    118          1.1  christos     return (Length);
    119          1.1  christos }
    120          1.1  christos 
    121          1.1  christos 
    122          1.1  christos /*******************************************************************************
    123          1.1  christos  *
    124          1.1  christos  * FUNCTION:    AcpiUtBoundStringOutput
    125          1.1  christos  *
    126          1.1  christos  * PARAMETERS:  String              - String with boundary
    127          1.1  christos  *              End                 - Boundary of the string
    128          1.1  christos  *              c                   - Character to be output to the string
    129          1.1  christos  *
    130          1.1  christos  * RETURN:      Updated position for next valid character
    131          1.1  christos  *
    132          1.1  christos  * DESCRIPTION: Output a character into a string with boundary check.
    133          1.1  christos  *
    134          1.1  christos  ******************************************************************************/
    135          1.1  christos 
    136          1.1  christos static char *
    137          1.1  christos AcpiUtBoundStringOutput (
    138          1.1  christos     char                    *String,
    139          1.1  christos     const char              *End,
    140          1.1  christos     char                    c)
    141          1.1  christos {
    142          1.1  christos 
    143          1.1  christos     if (String < End)
    144          1.1  christos     {
    145          1.1  christos         *String = c;
    146          1.1  christos     }
    147          1.1  christos 
    148          1.1  christos     ++String;
    149          1.1  christos     return (String);
    150          1.1  christos }
    151          1.1  christos 
    152          1.1  christos 
    153          1.1  christos /*******************************************************************************
    154          1.1  christos  *
    155          1.1  christos  * FUNCTION:    AcpiUtPutNumber
    156          1.1  christos  *
    157          1.1  christos  * PARAMETERS:  String              - Buffer to hold reverse-ordered string
    158          1.1  christos  *              Number              - Integer to be converted
    159          1.1  christos  *              Base                - Base of the integer
    160          1.1  christos  *              Upper               - Whether or not using upper cased digits
    161          1.1  christos  *
    162          1.1  christos  * RETURN:      Updated position for next valid character
    163          1.1  christos  *
    164          1.1  christos  * DESCRIPTION: Convert an integer into a string, note that, the string holds a
    165          1.1  christos  *              reversed ordered number without the trailing zero.
    166          1.1  christos  *
    167          1.1  christos  ******************************************************************************/
    168          1.1  christos 
    169          1.1  christos static char *
    170          1.1  christos AcpiUtPutNumber (
    171          1.1  christos     char                    *String,
    172          1.1  christos     UINT64                  Number,
    173          1.1  christos     UINT8                   Base,
    174          1.1  christos     BOOLEAN                 Upper)
    175          1.1  christos {
    176          1.1  christos     const char              *Digits;
    177          1.1  christos     UINT64                  DigitIndex;
    178          1.1  christos     char                    *Pos;
    179          1.1  christos 
    180          1.1  christos 
    181          1.1  christos     Pos = String;
    182          1.1  christos     Digits = Upper ? AcpiGbl_UpperHexDigits : AcpiGbl_LowerHexDigits;
    183          1.1  christos 
    184          1.1  christos     if (Number == 0)
    185          1.1  christos     {
    186          1.1  christos         *(Pos++) = '0';
    187          1.1  christos     }
    188          1.1  christos     else
    189          1.1  christos     {
    190          1.1  christos         while (Number)
    191          1.1  christos         {
    192          1.1  christos             (void) AcpiUtDivide (Number, Base, &Number, &DigitIndex);
    193          1.1  christos             *(Pos++) = Digits[DigitIndex];
    194          1.1  christos         }
    195          1.1  christos     }
    196          1.1  christos 
    197          1.1  christos     /* *(Pos++) = '0'; */
    198          1.1  christos     return (Pos);
    199          1.1  christos }
    200          1.1  christos 
    201          1.1  christos 
    202          1.1  christos /*******************************************************************************
    203          1.1  christos  *
    204          1.1  christos  * FUNCTION:    AcpiUtScanNumber
    205          1.1  christos  *
    206          1.1  christos  * PARAMETERS:  String              - String buffer
    207          1.1  christos  *              NumberPtr           - Where the number is returned
    208          1.1  christos  *
    209          1.1  christos  * RETURN:      Updated position for next valid character
    210          1.1  christos  *
    211          1.1  christos  * DESCRIPTION: Scan a string for a decimal integer.
    212          1.1  christos  *
    213          1.1  christos  ******************************************************************************/
    214          1.1  christos 
    215          1.1  christos const char *
    216          1.1  christos AcpiUtScanNumber (
    217          1.1  christos     const char              *String,
    218          1.1  christos     UINT64                  *NumberPtr)
    219          1.1  christos {
    220          1.1  christos     UINT64                  Number = 0;
    221          1.1  christos 
    222          1.1  christos 
    223      1.1.1.3  christos     while (isdigit ((int) *String))
    224          1.1  christos     {
    225          1.1  christos         Number *= 10;
    226          1.1  christos         Number += *(String++) - '0';
    227          1.1  christos     }
    228          1.1  christos 
    229          1.1  christos     *NumberPtr = Number;
    230          1.1  christos     return (String);
    231          1.1  christos }
    232          1.1  christos 
    233          1.1  christos 
    234          1.1  christos /*******************************************************************************
    235          1.1  christos  *
    236          1.1  christos  * FUNCTION:    AcpiUtPrintNumber
    237          1.1  christos  *
    238          1.1  christos  * PARAMETERS:  String              - String buffer
    239          1.1  christos  *              Number              - The number to be converted
    240          1.1  christos  *
    241          1.1  christos  * RETURN:      Updated position for next valid character
    242          1.1  christos  *
    243          1.1  christos  * DESCRIPTION: Print a decimal integer into a string.
    244          1.1  christos  *
    245          1.1  christos  ******************************************************************************/
    246          1.1  christos 
    247          1.1  christos const char *
    248          1.1  christos AcpiUtPrintNumber (
    249          1.1  christos     char                    *String,
    250          1.1  christos     UINT64                  Number)
    251          1.1  christos {
    252          1.1  christos     char                    AsciiString[20];
    253          1.1  christos     const char              *Pos1;
    254          1.1  christos     char                    *Pos2;
    255          1.1  christos 
    256          1.1  christos 
    257          1.1  christos     Pos1 = AcpiUtPutNumber (AsciiString, Number, 10, FALSE);
    258          1.1  christos     Pos2 = String;
    259          1.1  christos 
    260          1.1  christos     while (Pos1 != AsciiString)
    261          1.1  christos     {
    262          1.1  christos         *(Pos2++) = *(--Pos1);
    263          1.1  christos     }
    264          1.1  christos 
    265          1.1  christos     *Pos2 = 0;
    266          1.1  christos     return (String);
    267          1.1  christos }
    268          1.1  christos 
    269          1.1  christos 
    270          1.1  christos /*******************************************************************************
    271          1.1  christos  *
    272          1.1  christos  * FUNCTION:    AcpiUtFormatNumber
    273          1.1  christos  *
    274          1.1  christos  * PARAMETERS:  String              - String buffer with boundary
    275          1.1  christos  *              End                 - Boundary of the string
    276          1.1  christos  *              Number              - The number to be converted
    277          1.1  christos  *              Base                - Base of the integer
    278          1.1  christos  *              Width               - Field width
    279          1.1  christos  *              Precision           - Precision of the integer
    280          1.1  christos  *              Type                - Special printing flags
    281          1.1  christos  *
    282          1.1  christos  * RETURN:      Updated position for next valid character
    283          1.1  christos  *
    284          1.1  christos  * DESCRIPTION: Print an integer into a string with any base and any precision.
    285          1.1  christos  *
    286          1.1  christos  ******************************************************************************/
    287          1.1  christos 
    288          1.1  christos static char *
    289          1.1  christos AcpiUtFormatNumber (
    290          1.1  christos     char                    *String,
    291          1.1  christos     char                    *End,
    292          1.1  christos     UINT64                  Number,
    293          1.1  christos     UINT8                   Base,
    294          1.1  christos     INT32                   Width,
    295          1.1  christos     INT32                   Precision,
    296          1.1  christos     UINT8                   Type)
    297          1.1  christos {
    298          1.1  christos     char                    *Pos;
    299          1.1  christos     char                    Sign;
    300          1.1  christos     char                    Zero;
    301          1.1  christos     BOOLEAN                 NeedPrefix;
    302          1.1  christos     BOOLEAN                 Upper;
    303          1.1  christos     INT32                   i;
    304          1.1  christos     char                    ReversedString[66];
    305          1.1  christos 
    306          1.1  christos 
    307          1.1  christos     /* Parameter validation */
    308          1.1  christos 
    309          1.1  christos     if (Base < 2 || Base > 16)
    310          1.1  christos     {
    311          1.1  christos         return (NULL);
    312          1.1  christos     }
    313          1.1  christos 
    314          1.1  christos     if (Type & ACPI_FORMAT_LEFT)
    315          1.1  christos     {
    316          1.1  christos         Type &= ~ACPI_FORMAT_ZERO;
    317          1.1  christos     }
    318          1.1  christos 
    319          1.1  christos     NeedPrefix = ((Type & ACPI_FORMAT_PREFIX) && Base != 10) ? TRUE : FALSE;
    320          1.1  christos     Upper = (Type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
    321          1.1  christos     Zero = (Type & ACPI_FORMAT_ZERO) ? '0' : ' ';
    322          1.1  christos 
    323          1.1  christos     /* Calculate size according to sign and prefix */
    324          1.1  christos 
    325          1.1  christos     Sign = '\0';
    326          1.1  christos     if (Type & ACPI_FORMAT_SIGN)
    327          1.1  christos     {
    328          1.1  christos         if ((INT64) Number < 0)
    329          1.1  christos         {
    330          1.1  christos             Sign = '-';
    331          1.1  christos             Number = - (INT64) Number;
    332          1.1  christos             Width--;
    333          1.1  christos         }
    334          1.1  christos         else if (Type & ACPI_FORMAT_SIGN_PLUS)
    335          1.1  christos         {
    336          1.1  christos             Sign = '+';
    337          1.1  christos             Width--;
    338          1.1  christos         }
    339          1.1  christos         else if (Type & ACPI_FORMAT_SIGN_PLUS_SPACE)
    340          1.1  christos         {
    341          1.1  christos             Sign = ' ';
    342          1.1  christos             Width--;
    343          1.1  christos         }
    344          1.1  christos     }
    345          1.1  christos     if (NeedPrefix)
    346          1.1  christos     {
    347          1.1  christos         Width--;
    348          1.1  christos         if (Base == 16)
    349          1.1  christos         {
    350          1.1  christos             Width--;
    351          1.1  christos         }
    352          1.1  christos     }
    353          1.1  christos 
    354          1.1  christos     /* Generate full string in reverse order */
    355          1.1  christos 
    356          1.1  christos     Pos = AcpiUtPutNumber (ReversedString, Number, Base, Upper);
    357          1.1  christos     i = ACPI_PTR_DIFF (Pos, ReversedString);
    358          1.1  christos 
    359          1.1  christos     /* Printing 100 using %2d gives "100", not "00" */
    360          1.1  christos 
    361          1.1  christos     if (i > Precision)
    362          1.1  christos     {
    363          1.1  christos         Precision = i;
    364          1.1  christos     }
    365          1.1  christos 
    366          1.1  christos     Width -= Precision;
    367          1.1  christos 
    368          1.1  christos     /* Output the string */
    369          1.1  christos 
    370          1.1  christos     if (!(Type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT)))
    371          1.1  christos     {
    372          1.1  christos         while (--Width >= 0)
    373          1.1  christos         {
    374          1.1  christos             String = AcpiUtBoundStringOutput (String, End, ' ');
    375          1.1  christos         }
    376          1.1  christos     }
    377          1.1  christos     if (Sign)
    378          1.1  christos     {
    379          1.1  christos         String = AcpiUtBoundStringOutput (String, End, Sign);
    380          1.1  christos     }
    381          1.1  christos     if (NeedPrefix)
    382          1.1  christos     {
    383          1.1  christos         String = AcpiUtBoundStringOutput (String, End, '0');
    384          1.1  christos         if (Base == 16)
    385          1.1  christos         {
    386      1.1.1.4  christos             String = AcpiUtBoundStringOutput (
    387      1.1.1.4  christos                 String, End, Upper ? 'X' : 'x');
    388          1.1  christos         }
    389          1.1  christos     }
    390          1.1  christos     if (!(Type & ACPI_FORMAT_LEFT))
    391          1.1  christos     {
    392          1.1  christos         while (--Width >= 0)
    393          1.1  christos         {
    394          1.1  christos             String = AcpiUtBoundStringOutput (String, End, Zero);
    395          1.1  christos         }
    396          1.1  christos     }
    397          1.1  christos 
    398          1.1  christos     while (i <= --Precision)
    399          1.1  christos     {
    400          1.1  christos         String = AcpiUtBoundStringOutput (String, End, '0');
    401          1.1  christos     }
    402          1.1  christos     while (--i >= 0)
    403          1.1  christos     {
    404          1.1  christos         String = AcpiUtBoundStringOutput (String, End,
    405          1.1  christos                     ReversedString[i]);
    406          1.1  christos     }
    407          1.1  christos     while (--Width >= 0)
    408          1.1  christos     {
    409          1.1  christos         String = AcpiUtBoundStringOutput (String, End, ' ');
    410          1.1  christos     }
    411          1.1  christos 
    412          1.1  christos     return (String);
    413          1.1  christos }
    414          1.1  christos 
    415  1.1.1.5.2.1  pgoyette #undef sprintf
    416  1.1.1.5.2.1  pgoyette #undef snprintf
    417  1.1.1.5.2.1  pgoyette #undef vsnprintf
    418          1.1  christos /*******************************************************************************
    419          1.1  christos  *
    420  1.1.1.5.2.1  pgoyette  * FUNCTION:    vsnprintf
    421          1.1  christos  *
    422          1.1  christos  * PARAMETERS:  String              - String with boundary
    423          1.1  christos  *              Size                - Boundary of the string
    424          1.1  christos  *              Format              - Standard printf format
    425          1.1  christos  *              Args                - Argument list
    426          1.1  christos  *
    427          1.1  christos  * RETURN:      Number of bytes actually written.
    428          1.1  christos  *
    429          1.1  christos  * DESCRIPTION: Formatted output to a string using argument list pointer.
    430          1.1  christos  *
    431          1.1  christos  ******************************************************************************/
    432          1.1  christos 
    433          1.1  christos int
    434  1.1.1.5.2.1  pgoyette vsnprintf (
    435          1.1  christos     char                    *String,
    436          1.1  christos     ACPI_SIZE               Size,
    437          1.1  christos     const char              *Format,
    438          1.1  christos     va_list                 Args)
    439          1.1  christos {
    440      1.1.1.2  christos     UINT8                   Base;
    441      1.1.1.2  christos     UINT8                   Type;
    442      1.1.1.2  christos     INT32                   Width;
    443      1.1.1.2  christos     INT32                   Precision;
    444      1.1.1.2  christos     char                    Qualifier;
    445          1.1  christos     UINT64                  Number;
    446          1.1  christos     char                    *Pos;
    447          1.1  christos     char                    *End;
    448          1.1  christos     char                    c;
    449          1.1  christos     const char              *s;
    450          1.1  christos     const void              *p;
    451          1.1  christos     INT32                   Length;
    452          1.1  christos     int                     i;
    453          1.1  christos 
    454          1.1  christos 
    455          1.1  christos     Pos = String;
    456          1.1  christos     End = String + Size;
    457          1.1  christos 
    458          1.1  christos     for (; *Format; ++Format)
    459          1.1  christos     {
    460          1.1  christos         if (*Format != '%')
    461          1.1  christos         {
    462          1.1  christos             Pos = AcpiUtBoundStringOutput (Pos, End, *Format);
    463          1.1  christos             continue;
    464          1.1  christos         }
    465          1.1  christos 
    466      1.1.1.2  christos         Type = 0;
    467      1.1.1.2  christos         Base = 10;
    468      1.1.1.2  christos 
    469          1.1  christos         /* Process sign */
    470          1.1  christos 
    471          1.1  christos         do
    472          1.1  christos         {
    473          1.1  christos             ++Format;
    474          1.1  christos             if (*Format == '#')
    475          1.1  christos             {
    476          1.1  christos                 Type |= ACPI_FORMAT_PREFIX;
    477          1.1  christos             }
    478          1.1  christos             else if (*Format == '0')
    479          1.1  christos             {
    480          1.1  christos                 Type |= ACPI_FORMAT_ZERO;
    481          1.1  christos             }
    482          1.1  christos             else if (*Format == '+')
    483          1.1  christos             {
    484          1.1  christos                 Type |= ACPI_FORMAT_SIGN_PLUS;
    485          1.1  christos             }
    486          1.1  christos             else if (*Format == ' ')
    487          1.1  christos             {
    488          1.1  christos                 Type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
    489          1.1  christos             }
    490          1.1  christos             else if (*Format == '-')
    491          1.1  christos             {
    492          1.1  christos                 Type |= ACPI_FORMAT_LEFT;
    493          1.1  christos             }
    494          1.1  christos             else
    495          1.1  christos             {
    496          1.1  christos                 break;
    497          1.1  christos             }
    498      1.1.1.4  christos 
    499          1.1  christos         } while (1);
    500          1.1  christos 
    501          1.1  christos         /* Process width */
    502          1.1  christos 
    503          1.1  christos         Width = -1;
    504      1.1.1.3  christos         if (isdigit ((int) *Format))
    505          1.1  christos         {
    506          1.1  christos             Format = AcpiUtScanNumber (Format, &Number);
    507          1.1  christos             Width = (INT32) Number;
    508          1.1  christos         }
    509          1.1  christos         else if (*Format == '*')
    510          1.1  christos         {
    511          1.1  christos             ++Format;
    512          1.1  christos             Width = va_arg (Args, int);
    513          1.1  christos             if (Width < 0)
    514          1.1  christos             {
    515          1.1  christos                 Width = -Width;
    516          1.1  christos                 Type |= ACPI_FORMAT_LEFT;
    517          1.1  christos             }
    518          1.1  christos         }
    519          1.1  christos 
    520          1.1  christos         /* Process precision */
    521          1.1  christos 
    522          1.1  christos         Precision = -1;
    523          1.1  christos         if (*Format == '.')
    524          1.1  christos         {
    525          1.1  christos             ++Format;
    526      1.1.1.3  christos             if (isdigit ((int) *Format))
    527          1.1  christos             {
    528          1.1  christos                 Format = AcpiUtScanNumber (Format, &Number);
    529          1.1  christos                 Precision = (INT32) Number;
    530          1.1  christos             }
    531          1.1  christos             else if (*Format == '*')
    532          1.1  christos             {
    533          1.1  christos                 ++Format;
    534          1.1  christos                 Precision = va_arg (Args, int);
    535          1.1  christos             }
    536      1.1.1.4  christos 
    537          1.1  christos             if (Precision < 0)
    538          1.1  christos             {
    539          1.1  christos                 Precision = 0;
    540          1.1  christos             }
    541          1.1  christos         }
    542          1.1  christos 
    543          1.1  christos         /* Process qualifier */
    544          1.1  christos 
    545          1.1  christos         Qualifier = -1;
    546          1.1  christos         if (*Format == 'h' || *Format == 'l' || *Format == 'L')
    547          1.1  christos         {
    548          1.1  christos             Qualifier = *Format;
    549          1.1  christos             ++Format;
    550          1.1  christos 
    551          1.1  christos             if (Qualifier == 'l' && *Format == 'l')
    552          1.1  christos             {
    553          1.1  christos                 Qualifier = 'L';
    554          1.1  christos                 ++Format;
    555          1.1  christos             }
    556          1.1  christos         }
    557          1.1  christos 
    558          1.1  christos         switch (*Format)
    559          1.1  christos         {
    560          1.1  christos         case '%':
    561          1.1  christos 
    562          1.1  christos             Pos = AcpiUtBoundStringOutput (Pos, End, '%');
    563          1.1  christos             continue;
    564          1.1  christos 
    565          1.1  christos         case 'c':
    566          1.1  christos 
    567          1.1  christos             if (!(Type & ACPI_FORMAT_LEFT))
    568          1.1  christos             {
    569          1.1  christos                 while (--Width > 0)
    570          1.1  christos                 {
    571          1.1  christos                     Pos = AcpiUtBoundStringOutput (Pos, End, ' ');
    572          1.1  christos                 }
    573          1.1  christos             }
    574          1.1  christos 
    575          1.1  christos             c = (char) va_arg (Args, int);
    576          1.1  christos             Pos = AcpiUtBoundStringOutput (Pos, End, c);
    577          1.1  christos 
    578          1.1  christos             while (--Width > 0)
    579          1.1  christos             {
    580          1.1  christos                 Pos = AcpiUtBoundStringOutput (Pos, End, ' ');
    581          1.1  christos             }
    582          1.1  christos             continue;
    583          1.1  christos 
    584          1.1  christos         case 's':
    585          1.1  christos 
    586          1.1  christos             s = va_arg (Args, char *);
    587          1.1  christos             if (!s)
    588          1.1  christos             {
    589          1.1  christos                 s = "<NULL>";
    590          1.1  christos             }
    591          1.1  christos             Length = AcpiUtBoundStringLength (s, Precision);
    592          1.1  christos             if (!(Type & ACPI_FORMAT_LEFT))
    593          1.1  christos             {
    594          1.1  christos                 while (Length < Width--)
    595          1.1  christos                 {
    596          1.1  christos                     Pos = AcpiUtBoundStringOutput (Pos, End, ' ');
    597          1.1  christos                 }
    598          1.1  christos             }
    599      1.1.1.4  christos 
    600          1.1  christos             for (i = 0; i < Length; ++i)
    601          1.1  christos             {
    602          1.1  christos                 Pos = AcpiUtBoundStringOutput (Pos, End, *s);
    603          1.1  christos                 ++s;
    604          1.1  christos             }
    605      1.1.1.4  christos 
    606          1.1  christos             while (Length < Width--)
    607          1.1  christos             {
    608          1.1  christos                 Pos = AcpiUtBoundStringOutput (Pos, End, ' ');
    609          1.1  christos             }
    610          1.1  christos             continue;
    611          1.1  christos 
    612          1.1  christos         case 'o':
    613          1.1  christos 
    614          1.1  christos             Base = 8;
    615          1.1  christos             break;
    616          1.1  christos 
    617          1.1  christos         case 'X':
    618          1.1  christos 
    619          1.1  christos             Type |= ACPI_FORMAT_UPPER;
    620          1.1  christos 
    621          1.1  christos         case 'x':
    622          1.1  christos 
    623          1.1  christos             Base = 16;
    624          1.1  christos             break;
    625          1.1  christos 
    626          1.1  christos         case 'd':
    627          1.1  christos         case 'i':
    628          1.1  christos 
    629          1.1  christos             Type |= ACPI_FORMAT_SIGN;
    630          1.1  christos 
    631          1.1  christos         case 'u':
    632          1.1  christos 
    633          1.1  christos             break;
    634          1.1  christos 
    635          1.1  christos         case 'p':
    636          1.1  christos 
    637          1.1  christos             if (Width == -1)
    638          1.1  christos             {
    639          1.1  christos                 Width = 2 * sizeof (void *);
    640          1.1  christos                 Type |= ACPI_FORMAT_ZERO;
    641          1.1  christos             }
    642          1.1  christos 
    643          1.1  christos             p = va_arg (Args, void *);
    644      1.1.1.4  christos             Pos = AcpiUtFormatNumber (
    645      1.1.1.4  christos                 Pos, End, ACPI_TO_INTEGER (p), 16, Width, Precision, Type);
    646          1.1  christos             continue;
    647          1.1  christos 
    648          1.1  christos         default:
    649          1.1  christos 
    650          1.1  christos             Pos = AcpiUtBoundStringOutput (Pos, End, '%');
    651          1.1  christos             if (*Format)
    652          1.1  christos             {
    653          1.1  christos                 Pos = AcpiUtBoundStringOutput (Pos, End, *Format);
    654          1.1  christos             }
    655          1.1  christos             else
    656          1.1  christos             {
    657          1.1  christos                 --Format;
    658          1.1  christos             }
    659          1.1  christos             continue;
    660          1.1  christos         }
    661          1.1  christos 
    662          1.1  christos         if (Qualifier == 'L')
    663          1.1  christos         {
    664          1.1  christos             Number = va_arg (Args, UINT64);
    665          1.1  christos             if (Type & ACPI_FORMAT_SIGN)
    666          1.1  christos             {
    667          1.1  christos                 Number = (INT64) Number;
    668          1.1  christos             }
    669          1.1  christos         }
    670          1.1  christos         else if (Qualifier == 'l')
    671          1.1  christos         {
    672          1.1  christos             Number = va_arg (Args, unsigned long);
    673          1.1  christos             if (Type & ACPI_FORMAT_SIGN)
    674          1.1  christos             {
    675          1.1  christos                 Number = (INT32) Number;
    676          1.1  christos             }
    677          1.1  christos         }
    678          1.1  christos         else if (Qualifier == 'h')
    679          1.1  christos         {
    680          1.1  christos             Number = (UINT16) va_arg (Args, int);
    681          1.1  christos             if (Type & ACPI_FORMAT_SIGN)
    682          1.1  christos             {
    683          1.1  christos                 Number = (INT16) Number;
    684          1.1  christos             }
    685          1.1  christos         }
    686          1.1  christos         else
    687          1.1  christos         {
    688          1.1  christos             Number = va_arg (Args, unsigned int);
    689          1.1  christos             if (Type & ACPI_FORMAT_SIGN)
    690          1.1  christos             {
    691          1.1  christos                 Number = (signed int) Number;
    692          1.1  christos             }
    693          1.1  christos         }
    694          1.1  christos 
    695          1.1  christos         Pos = AcpiUtFormatNumber (Pos, End, Number, Base,
    696      1.1.1.4  christos             Width, Precision, Type);
    697          1.1  christos     }
    698          1.1  christos 
    699          1.1  christos     if (Size > 0)
    700          1.1  christos     {
    701          1.1  christos         if (Pos < End)
    702          1.1  christos         {
    703          1.1  christos             *Pos = '\0';
    704          1.1  christos         }
    705          1.1  christos         else
    706          1.1  christos         {
    707          1.1  christos             End[-1] = '\0';
    708          1.1  christos         }
    709          1.1  christos     }
    710          1.1  christos 
    711          1.1  christos     return (ACPI_PTR_DIFF (Pos, String));
    712          1.1  christos }
    713          1.1  christos 
    714          1.1  christos 
    715          1.1  christos /*******************************************************************************
    716          1.1  christos  *
    717  1.1.1.5.2.1  pgoyette  * FUNCTION:    snprintf
    718          1.1  christos  *
    719          1.1  christos  * PARAMETERS:  String              - String with boundary
    720          1.1  christos  *              Size                - Boundary of the string
    721          1.1  christos  *              Format, ...         - Standard printf format
    722          1.1  christos  *
    723          1.1  christos  * RETURN:      Number of bytes actually written.
    724          1.1  christos  *
    725          1.1  christos  * DESCRIPTION: Formatted output to a string.
    726          1.1  christos  *
    727          1.1  christos  ******************************************************************************/
    728          1.1  christos 
    729          1.1  christos int
    730  1.1.1.5.2.1  pgoyette snprintf (
    731          1.1  christos     char                    *String,
    732          1.1  christos     ACPI_SIZE               Size,
    733          1.1  christos     const char              *Format,
    734          1.1  christos     ...)
    735          1.1  christos {
    736          1.1  christos     va_list                 Args;
    737          1.1  christos     int                     Length;
    738          1.1  christos 
    739          1.1  christos 
    740          1.1  christos     va_start (Args, Format);
    741  1.1.1.5.2.1  pgoyette     Length = vsnprintf (String, Size, Format, Args);
    742  1.1.1.5.2.1  pgoyette     va_end (Args);
    743  1.1.1.5.2.1  pgoyette 
    744  1.1.1.5.2.1  pgoyette     return (Length);
    745  1.1.1.5.2.1  pgoyette }
    746  1.1.1.5.2.1  pgoyette 
    747  1.1.1.5.2.1  pgoyette 
    748  1.1.1.5.2.1  pgoyette /*******************************************************************************
    749  1.1.1.5.2.1  pgoyette  *
    750  1.1.1.5.2.1  pgoyette  * FUNCTION:    sprintf
    751  1.1.1.5.2.1  pgoyette  *
    752  1.1.1.5.2.1  pgoyette  * PARAMETERS:  String              - String with boundary
    753  1.1.1.5.2.1  pgoyette  *              Format, ...         - Standard printf format
    754  1.1.1.5.2.1  pgoyette  *
    755  1.1.1.5.2.1  pgoyette  * RETURN:      Number of bytes actually written.
    756  1.1.1.5.2.1  pgoyette  *
    757  1.1.1.5.2.1  pgoyette  * DESCRIPTION: Formatted output to a string.
    758  1.1.1.5.2.1  pgoyette  *
    759  1.1.1.5.2.1  pgoyette  ******************************************************************************/
    760  1.1.1.5.2.1  pgoyette 
    761  1.1.1.5.2.1  pgoyette int
    762  1.1.1.5.2.1  pgoyette sprintf (
    763  1.1.1.5.2.1  pgoyette     char                    *String,
    764  1.1.1.5.2.1  pgoyette     const char              *Format,
    765  1.1.1.5.2.1  pgoyette     ...)
    766  1.1.1.5.2.1  pgoyette {
    767  1.1.1.5.2.1  pgoyette     va_list                 Args;
    768  1.1.1.5.2.1  pgoyette     int                     Length;
    769  1.1.1.5.2.1  pgoyette 
    770  1.1.1.5.2.1  pgoyette 
    771  1.1.1.5.2.1  pgoyette     va_start (Args, Format);
    772  1.1.1.5.2.1  pgoyette     Length = vsnprintf (String, ACPI_UINT32_MAX, Format, Args);
    773          1.1  christos     va_end (Args);
    774          1.1  christos 
    775          1.1  christos     return (Length);
    776          1.1  christos }
    777          1.1  christos 
    778          1.1  christos 
    779          1.1  christos #ifdef ACPI_APPLICATION
    780          1.1  christos /*******************************************************************************
    781          1.1  christos  *
    782  1.1.1.5.2.1  pgoyette  * FUNCTION:    vprintf
    783  1.1.1.5.2.1  pgoyette  *
    784  1.1.1.5.2.1  pgoyette  * PARAMETERS:  Format              - Standard printf format
    785  1.1.1.5.2.1  pgoyette  *              Args                - Argument list
    786  1.1.1.5.2.1  pgoyette  *
    787  1.1.1.5.2.1  pgoyette  * RETURN:      Number of bytes actually written.
    788  1.1.1.5.2.1  pgoyette  *
    789  1.1.1.5.2.1  pgoyette  * DESCRIPTION: Formatted output to stdout using argument list pointer.
    790  1.1.1.5.2.1  pgoyette  *
    791  1.1.1.5.2.1  pgoyette  ******************************************************************************/
    792  1.1.1.5.2.1  pgoyette 
    793  1.1.1.5.2.1  pgoyette int
    794  1.1.1.5.2.1  pgoyette vprintf (
    795  1.1.1.5.2.1  pgoyette     const char              *Format,
    796  1.1.1.5.2.1  pgoyette     va_list                 Args)
    797  1.1.1.5.2.1  pgoyette {
    798  1.1.1.5.2.1  pgoyette     ACPI_CPU_FLAGS          Flags;
    799  1.1.1.5.2.1  pgoyette     int                     Length;
    800  1.1.1.5.2.1  pgoyette 
    801  1.1.1.5.2.1  pgoyette 
    802  1.1.1.5.2.1  pgoyette     Flags = AcpiOsAcquireLock (AcpiGbl_PrintLock);
    803  1.1.1.5.2.1  pgoyette     Length = vsnprintf (AcpiGbl_PrintBuffer,
    804  1.1.1.5.2.1  pgoyette                 sizeof (AcpiGbl_PrintBuffer), Format, Args);
    805  1.1.1.5.2.1  pgoyette 
    806  1.1.1.5.2.1  pgoyette     (void) fwrite (AcpiGbl_PrintBuffer, Length, 1, ACPI_FILE_OUT);
    807  1.1.1.5.2.1  pgoyette     AcpiOsReleaseLock (AcpiGbl_PrintLock, Flags);
    808  1.1.1.5.2.1  pgoyette 
    809  1.1.1.5.2.1  pgoyette     return (Length);
    810  1.1.1.5.2.1  pgoyette }
    811  1.1.1.5.2.1  pgoyette 
    812  1.1.1.5.2.1  pgoyette 
    813  1.1.1.5.2.1  pgoyette /*******************************************************************************
    814  1.1.1.5.2.1  pgoyette  *
    815  1.1.1.5.2.1  pgoyette  * FUNCTION:    printf
    816  1.1.1.5.2.1  pgoyette  *
    817  1.1.1.5.2.1  pgoyette  * PARAMETERS:  Format, ...         - Standard printf format
    818  1.1.1.5.2.1  pgoyette  *
    819  1.1.1.5.2.1  pgoyette  * RETURN:      Number of bytes actually written.
    820  1.1.1.5.2.1  pgoyette  *
    821  1.1.1.5.2.1  pgoyette  * DESCRIPTION: Formatted output to stdout.
    822  1.1.1.5.2.1  pgoyette  *
    823  1.1.1.5.2.1  pgoyette  ******************************************************************************/
    824  1.1.1.5.2.1  pgoyette 
    825  1.1.1.5.2.1  pgoyette int
    826  1.1.1.5.2.1  pgoyette printf (
    827  1.1.1.5.2.1  pgoyette     const char              *Format,
    828  1.1.1.5.2.1  pgoyette     ...)
    829  1.1.1.5.2.1  pgoyette {
    830  1.1.1.5.2.1  pgoyette     va_list                 Args;
    831  1.1.1.5.2.1  pgoyette     int                     Length;
    832  1.1.1.5.2.1  pgoyette 
    833  1.1.1.5.2.1  pgoyette 
    834  1.1.1.5.2.1  pgoyette     va_start (Args, Format);
    835  1.1.1.5.2.1  pgoyette     Length = vprintf (Format, Args);
    836  1.1.1.5.2.1  pgoyette     va_end (Args);
    837  1.1.1.5.2.1  pgoyette 
    838  1.1.1.5.2.1  pgoyette     return (Length);
    839  1.1.1.5.2.1  pgoyette }
    840  1.1.1.5.2.1  pgoyette 
    841  1.1.1.5.2.1  pgoyette 
    842  1.1.1.5.2.1  pgoyette /*******************************************************************************
    843  1.1.1.5.2.1  pgoyette  *
    844  1.1.1.5.2.1  pgoyette  * FUNCTION:    vfprintf
    845          1.1  christos  *
    846          1.1  christos  * PARAMETERS:  File                - File descriptor
    847          1.1  christos  *              Format              - Standard printf format
    848          1.1  christos  *              Args                - Argument list
    849          1.1  christos  *
    850          1.1  christos  * RETURN:      Number of bytes actually written.
    851          1.1  christos  *
    852          1.1  christos  * DESCRIPTION: Formatted output to a file using argument list pointer.
    853          1.1  christos  *
    854          1.1  christos  ******************************************************************************/
    855          1.1  christos 
    856          1.1  christos int
    857  1.1.1.5.2.1  pgoyette vfprintf (
    858  1.1.1.5.2.1  pgoyette     FILE                    *File,
    859          1.1  christos     const char              *Format,
    860          1.1  christos     va_list                 Args)
    861          1.1  christos {
    862          1.1  christos     ACPI_CPU_FLAGS          Flags;
    863          1.1  christos     int                     Length;
    864          1.1  christos 
    865          1.1  christos 
    866          1.1  christos     Flags = AcpiOsAcquireLock (AcpiGbl_PrintLock);
    867  1.1.1.5.2.1  pgoyette     Length = vsnprintf (AcpiGbl_PrintBuffer,
    868      1.1.1.4  christos         sizeof (AcpiGbl_PrintBuffer), Format, Args);
    869          1.1  christos 
    870  1.1.1.5.2.1  pgoyette     (void) fwrite (AcpiGbl_PrintBuffer, Length, 1, File);
    871          1.1  christos     AcpiOsReleaseLock (AcpiGbl_PrintLock, Flags);
    872          1.1  christos 
    873          1.1  christos     return (Length);
    874          1.1  christos }
    875          1.1  christos 
    876          1.1  christos 
    877          1.1  christos /*******************************************************************************
    878          1.1  christos  *
    879  1.1.1.5.2.1  pgoyette  * FUNCTION:    fprintf
    880          1.1  christos  *
    881          1.1  christos  * PARAMETERS:  File                - File descriptor
    882          1.1  christos  *              Format, ...         - Standard printf format
    883          1.1  christos  *
    884          1.1  christos  * RETURN:      Number of bytes actually written.
    885          1.1  christos  *
    886          1.1  christos  * DESCRIPTION: Formatted output to a file.
    887          1.1  christos  *
    888          1.1  christos  ******************************************************************************/
    889          1.1  christos 
    890          1.1  christos int
    891  1.1.1.5.2.1  pgoyette fprintf (
    892  1.1.1.5.2.1  pgoyette     FILE                    *File,
    893          1.1  christos     const char              *Format,
    894          1.1  christos     ...)
    895          1.1  christos {
    896          1.1  christos     va_list                 Args;
    897          1.1  christos     int                     Length;
    898          1.1  christos 
    899          1.1  christos 
    900          1.1  christos     va_start (Args, Format);
    901  1.1.1.5.2.1  pgoyette     Length = vfprintf (File, Format, Args);
    902          1.1  christos     va_end (Args);
    903          1.1  christos 
    904          1.1  christos     return (Length);
    905          1.1  christos }
    906          1.1  christos #endif
    907