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