Home | History | Annotate | Line # | Download | only in common
acgetline.c revision 1.1.1.12
      1       1.1  christos /******************************************************************************
      2       1.1  christos  *
      3       1.1  christos  * Module Name: acgetline - local line editing
      4       1.1  christos  *
      5       1.1  christos  *****************************************************************************/
      6       1.1  christos 
      7       1.1  christos /*
      8  1.1.1.12  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.1.1.12  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.1  christos 
     44       1.1  christos #include "acpi.h"
     45       1.1  christos #include "accommon.h"
     46       1.1  christos #include "amlcode.h"
     47       1.1  christos #include "acparser.h"
     48       1.1  christos #include "acdebug.h"
     49       1.1  christos 
     50       1.1  christos /*
     51       1.1  christos  * This is an os-independent implementation of line-editing services needed
     52       1.1  christos  * by the AcpiExec utility. It uses getchar() and putchar() and the existing
     53       1.1  christos  * history support provided by the AML debugger. It assumes that the terminal
     54       1.1  christos  * is in the correct line-editing mode such as raw and noecho. The OSL
     55       1.1  christos  * interface AcpiOsInitialize should do this. AcpiOsTerminate should put the
     56       1.1  christos  * terminal back into the original mode.
     57       1.1  christos  */
     58       1.1  christos #define _COMPONENT          ACPI_OS_SERVICES
     59       1.1  christos         ACPI_MODULE_NAME    ("acgetline")
     60       1.1  christos 
     61       1.1  christos 
     62       1.1  christos /* Local prototypes */
     63       1.1  christos 
     64       1.1  christos static void
     65       1.1  christos AcpiAcClearLine (
     66       1.1  christos     UINT32                  EndOfLine,
     67       1.1  christos     UINT32                  CursorPosition);
     68       1.1  christos 
     69       1.1  christos /* Various ASCII constants */
     70       1.1  christos 
     71       1.1  christos #define _ASCII_NUL                  0
     72       1.1  christos #define _ASCII_BACKSPACE            0x08
     73       1.1  christos #define _ASCII_TAB                  0x09
     74       1.1  christos #define _ASCII_ESCAPE               0x1B
     75       1.1  christos #define _ASCII_SPACE                0x20
     76       1.1  christos #define _ASCII_LEFT_BRACKET         0x5B
     77       1.1  christos #define _ASCII_DEL                  0x7F
     78       1.1  christos #define _ASCII_UP_ARROW             'A'
     79       1.1  christos #define _ASCII_DOWN_ARROW           'B'
     80       1.1  christos #define _ASCII_RIGHT_ARROW          'C'
     81       1.1  christos #define _ASCII_LEFT_ARROW           'D'
     82       1.1  christos #define _ASCII_NEWLINE              '\n'
     83       1.1  christos 
     84       1.1  christos /* Erase a single character on the input command line */
     85       1.1  christos 
     86       1.1  christos #define ACPI_CLEAR_CHAR() \
     87       1.1  christos     putchar (_ASCII_BACKSPACE); \
     88       1.1  christos     putchar (_ASCII_SPACE); \
     89       1.1  christos     putchar (_ASCII_BACKSPACE);
     90       1.1  christos 
     91       1.1  christos /* Backup cursor by Count positions */
     92       1.1  christos 
     93       1.1  christos #define ACPI_BACKUP_CURSOR(i, Count) \
     94       1.1  christos     for (i = 0; i < (Count); i++) \
     95       1.1  christos         {putchar (_ASCII_BACKSPACE);}
     96       1.1  christos 
     97       1.1  christos 
     98       1.1  christos /******************************************************************************
     99       1.1  christos  *
    100       1.1  christos  * FUNCTION:    AcpiAcClearLine
    101       1.1  christos  *
    102       1.1  christos  * PARAMETERS:  EndOfLine           - Current end-of-line index
    103       1.1  christos  *              CursorPosition      - Current cursor position within line
    104       1.1  christos  *
    105       1.1  christos  * RETURN:      None
    106       1.1  christos  *
    107       1.1  christos  * DESCRIPTION: Clear the entire command line the hard way, but probably the
    108       1.1  christos  *              most portable.
    109       1.1  christos  *
    110       1.1  christos  *****************************************************************************/
    111       1.1  christos 
    112       1.1  christos static void
    113       1.1  christos AcpiAcClearLine (
    114       1.1  christos     UINT32                  EndOfLine,
    115       1.1  christos     UINT32                  CursorPosition)
    116       1.1  christos {
    117       1.1  christos     UINT32                  i;
    118       1.1  christos 
    119       1.1  christos 
    120       1.1  christos     if (CursorPosition < EndOfLine)
    121       1.1  christos     {
    122       1.1  christos         /* Clear line from current position to end of line */
    123       1.1  christos 
    124       1.1  christos         for (i = 0; i < (EndOfLine - CursorPosition); i++)
    125       1.1  christos         {
    126       1.1  christos             putchar (' ');
    127       1.1  christos         }
    128       1.1  christos     }
    129       1.1  christos 
    130       1.1  christos     /* Clear the entire line */
    131       1.1  christos 
    132       1.1  christos     for (; EndOfLine > 0; EndOfLine--)
    133       1.1  christos     {
    134       1.1  christos         ACPI_CLEAR_CHAR ();
    135       1.1  christos     }
    136       1.1  christos }
    137       1.1  christos 
    138       1.1  christos 
    139       1.1  christos /******************************************************************************
    140       1.1  christos  *
    141       1.1  christos  * FUNCTION:    AcpiOsGetLine
    142       1.1  christos  *
    143       1.1  christos  * PARAMETERS:  Buffer              - Where to return the command line
    144       1.1  christos  *              BufferLength        - Maximum length of Buffer
    145       1.1  christos  *              BytesRead           - Where the actual byte count is returned
    146       1.1  christos  *
    147       1.1  christos  * RETURN:      Status and actual bytes read
    148       1.1  christos  *
    149       1.1  christos  * DESCRIPTION: Get the next input line from the terminal. NOTE: terminal
    150       1.1  christos  *              is expected to be in a mode that supports line-editing (raw,
    151       1.1  christos  *              noecho). This function is intended to be very portable. Also,
    152       1.1  christos  *              it uses the history support implemented in the AML debugger.
    153       1.1  christos  *
    154       1.1  christos  *****************************************************************************/
    155       1.1  christos 
    156       1.1  christos ACPI_STATUS
    157       1.1  christos AcpiOsGetLine (
    158       1.1  christos     char                    *Buffer,
    159       1.1  christos     UINT32                  BufferLength,
    160       1.1  christos     UINT32                  *BytesRead)
    161       1.1  christos {
    162       1.1  christos     char                    *NextCommand;
    163       1.1  christos     UINT32                  MaxCommandIndex = AcpiGbl_NextCmdNum - 1;
    164       1.1  christos     UINT32                  CurrentCommandIndex = MaxCommandIndex;
    165       1.1  christos     UINT32                  PreviousCommandIndex = MaxCommandIndex;
    166       1.1  christos     int                     InputChar;
    167       1.1  christos     UINT32                  CursorPosition = 0;
    168       1.1  christos     UINT32                  EndOfLine = 0;
    169       1.1  christos     UINT32                  i;
    170       1.1  christos 
    171       1.1  christos 
    172       1.1  christos     /* Always clear the line buffer before we read a new line */
    173       1.1  christos 
    174       1.1  christos     memset (Buffer, 0, BufferLength);
    175       1.1  christos 
    176       1.1  christos     /*
    177       1.1  christos      * This loop gets one character at a time (except for esc sequences)
    178       1.1  christos      * until a newline or error is detected.
    179       1.1  christos      *
    180       1.1  christos      * Note: Don't attempt to write terminal control ESC sequences, even
    181       1.1  christos      * though it makes certain things more difficult.
    182       1.1  christos      */
    183       1.1  christos     while (1)
    184       1.1  christos     {
    185       1.1  christos         if (EndOfLine >= (BufferLength - 1))
    186       1.1  christos         {
    187       1.1  christos             return (AE_BUFFER_OVERFLOW);
    188       1.1  christos         }
    189       1.1  christos 
    190       1.1  christos         InputChar = getchar ();
    191       1.1  christos         switch (InputChar)
    192       1.1  christos         {
    193       1.1  christos         default: /* This is the normal character case */
    194       1.1  christos 
    195       1.1  christos             /* Echo the character (at EOL) and copy it to the line buffer */
    196       1.1  christos 
    197       1.1  christos             if (EndOfLine == CursorPosition)
    198       1.1  christos             {
    199       1.1  christos                 putchar (InputChar);
    200       1.1  christos                 Buffer[EndOfLine] = (char) InputChar;
    201       1.1  christos 
    202       1.1  christos                 EndOfLine++;
    203       1.1  christos                 CursorPosition++;
    204       1.1  christos                 Buffer[EndOfLine] = 0;
    205       1.1  christos                 continue;
    206       1.1  christos             }
    207       1.1  christos 
    208       1.1  christos             /* Insert character into the middle of the buffer */
    209       1.1  christos 
    210       1.1  christos             memmove (&Buffer[CursorPosition + 1], &Buffer[CursorPosition],
    211       1.1  christos                 (EndOfLine - CursorPosition + 1));
    212       1.1  christos 
    213       1.1  christos             Buffer [CursorPosition] = (char) InputChar;
    214       1.1  christos             Buffer [EndOfLine + 1] = 0;
    215       1.1  christos 
    216       1.1  christos             /* Display the new part of line starting at the new character */
    217       1.1  christos 
    218       1.1  christos             fprintf (stdout, "%s", &Buffer[CursorPosition]);
    219       1.1  christos 
    220       1.1  christos             /* Restore cursor */
    221       1.1  christos 
    222       1.1  christos             ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
    223       1.1  christos             CursorPosition++;
    224       1.1  christos             EndOfLine++;
    225       1.1  christos             continue;
    226       1.1  christos 
    227       1.1  christos         case _ASCII_DEL: /* Backspace key */
    228       1.1  christos 
    229       1.1  christos             if (!EndOfLine) /* Any characters on the command line? */
    230       1.1  christos             {
    231       1.1  christos                 continue;
    232       1.1  christos             }
    233       1.1  christos 
    234       1.1  christos             if (EndOfLine == CursorPosition) /* Erase the final character */
    235       1.1  christos             {
    236       1.1  christos                 ACPI_CLEAR_CHAR ();
    237       1.1  christos                 EndOfLine--;
    238       1.1  christos                 CursorPosition--;
    239       1.1  christos                 continue;
    240       1.1  christos             }
    241       1.1  christos 
    242       1.1  christos             if (!CursorPosition) /* Do not backup beyond start of line */
    243       1.1  christos             {
    244       1.1  christos                 continue;
    245       1.1  christos             }
    246       1.1  christos 
    247       1.1  christos             /* Remove the character from the line */
    248       1.1  christos 
    249       1.1  christos             memmove (&Buffer[CursorPosition - 1], &Buffer[CursorPosition],
    250       1.1  christos                 (EndOfLine - CursorPosition + 1));
    251       1.1  christos 
    252       1.1  christos             /* Display the new part of line starting at the new character */
    253       1.1  christos 
    254       1.1  christos             putchar (_ASCII_BACKSPACE);
    255       1.1  christos             fprintf (stdout, "%s ", &Buffer[CursorPosition - 1]);
    256       1.1  christos 
    257       1.1  christos             /* Restore cursor */
    258       1.1  christos 
    259       1.1  christos             ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition + 1);
    260       1.1  christos             EndOfLine--;
    261   1.1.1.4  christos 
    262       1.1  christos             if (CursorPosition > 0)
    263       1.1  christos             {
    264       1.1  christos                 CursorPosition--;
    265       1.1  christos             }
    266       1.1  christos             continue;
    267       1.1  christos 
    268       1.1  christos         case _ASCII_NEWLINE: /* Normal exit case at end of command line */
    269       1.1  christos         case _ASCII_NUL:
    270       1.1  christos 
    271       1.1  christos             /* Return the number of bytes in the command line string */
    272       1.1  christos 
    273       1.1  christos             if (BytesRead)
    274       1.1  christos             {
    275       1.1  christos                 *BytesRead = EndOfLine;
    276       1.1  christos             }
    277       1.1  christos 
    278       1.1  christos             /* Echo, terminate string buffer, and exit */
    279       1.1  christos 
    280       1.1  christos             putchar (InputChar);
    281       1.1  christos             Buffer[EndOfLine] = 0;
    282       1.1  christos             return (AE_OK);
    283       1.1  christos 
    284       1.1  christos         case _ASCII_TAB:
    285       1.1  christos 
    286       1.1  christos             /* Ignore */
    287       1.1  christos 
    288       1.1  christos             continue;
    289       1.1  christos 
    290       1.1  christos         case EOF:
    291       1.1  christos 
    292       1.1  christos             return (AE_ERROR);
    293       1.1  christos 
    294       1.1  christos         case _ASCII_ESCAPE:
    295       1.1  christos 
    296       1.1  christos             /* Check for escape sequences of the form "ESC[x" */
    297       1.1  christos 
    298       1.1  christos             InputChar = getchar ();
    299       1.1  christos             if (InputChar != _ASCII_LEFT_BRACKET)
    300       1.1  christos             {
    301       1.1  christos                 continue; /* Ignore this ESC, does not have the '[' */
    302       1.1  christos             }
    303       1.1  christos 
    304       1.1  christos             /* Get the code following the ESC [ */
    305       1.1  christos 
    306       1.1  christos             InputChar = getchar (); /* Backup one character */
    307       1.1  christos             switch (InputChar)
    308       1.1  christos             {
    309       1.1  christos             case _ASCII_LEFT_ARROW:
    310       1.1  christos 
    311       1.1  christos                 if (CursorPosition > 0)
    312       1.1  christos                 {
    313       1.1  christos                     putchar (_ASCII_BACKSPACE);
    314       1.1  christos                     CursorPosition--;
    315       1.1  christos                 }
    316       1.1  christos                 continue;
    317       1.1  christos 
    318       1.1  christos             case _ASCII_RIGHT_ARROW:
    319       1.1  christos                 /*
    320       1.1  christos                  * Move one character forward. Do this without sending
    321       1.1  christos                  * ESC sequence to the terminal for max portability.
    322       1.1  christos                  */
    323       1.1  christos                 if (CursorPosition < EndOfLine)
    324       1.1  christos                 {
    325       1.1  christos                     /* Backup to start of line and print the entire line */
    326       1.1  christos 
    327       1.1  christos                     ACPI_BACKUP_CURSOR (i, CursorPosition);
    328       1.1  christos                     fprintf (stdout, "%s", Buffer);
    329       1.1  christos 
    330       1.1  christos                     /* Backup to where the cursor should be */
    331       1.1  christos 
    332       1.1  christos                     CursorPosition++;
    333       1.1  christos                     ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
    334       1.1  christos                 }
    335       1.1  christos                 continue;
    336       1.1  christos 
    337       1.1  christos             case _ASCII_UP_ARROW:
    338       1.1  christos 
    339       1.1  christos                 /* If no commands available or at start of history list, ignore */
    340       1.1  christos 
    341       1.1  christos                 if (!CurrentCommandIndex)
    342       1.1  christos                 {
    343       1.1  christos                     continue;
    344       1.1  christos                 }
    345       1.1  christos 
    346       1.1  christos                 /* Manage our up/down progress */
    347       1.1  christos 
    348       1.1  christos                 if (CurrentCommandIndex > PreviousCommandIndex)
    349       1.1  christos                 {
    350       1.1  christos                     CurrentCommandIndex = PreviousCommandIndex;
    351       1.1  christos                 }
    352       1.1  christos 
    353       1.1  christos                 /* Get the historical command from the debugger */
    354       1.1  christos 
    355       1.1  christos                 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
    356       1.1  christos                 if (!NextCommand)
    357       1.1  christos                 {
    358       1.1  christos                     return (AE_ERROR);
    359       1.1  christos                 }
    360       1.1  christos 
    361       1.1  christos                 /* Make this the active command and echo it */
    362       1.1  christos 
    363       1.1  christos                 AcpiAcClearLine (EndOfLine, CursorPosition);
    364       1.1  christos                 strcpy (Buffer, NextCommand);
    365       1.1  christos                 fprintf (stdout, "%s", Buffer);
    366       1.1  christos                 EndOfLine = CursorPosition = strlen (Buffer);
    367       1.1  christos 
    368       1.1  christos                 PreviousCommandIndex = CurrentCommandIndex;
    369       1.1  christos                 CurrentCommandIndex--;
    370       1.1  christos                 continue;
    371       1.1  christos 
    372       1.1  christos             case _ASCII_DOWN_ARROW:
    373       1.1  christos 
    374       1.1  christos                 if (!MaxCommandIndex) /* Any commands available? */
    375       1.1  christos                 {
    376       1.1  christos                     continue;
    377       1.1  christos                 }
    378       1.1  christos 
    379       1.1  christos                 /* Manage our up/down progress */
    380       1.1  christos 
    381       1.1  christos                 if (CurrentCommandIndex < PreviousCommandIndex)
    382       1.1  christos                 {
    383       1.1  christos                     CurrentCommandIndex = PreviousCommandIndex;
    384       1.1  christos                 }
    385       1.1  christos 
    386       1.1  christos                 /* If we are the end of the history list, output a clear new line */
    387       1.1  christos 
    388       1.1  christos                 if ((CurrentCommandIndex + 1) > MaxCommandIndex)
    389       1.1  christos                 {
    390       1.1  christos                     AcpiAcClearLine (EndOfLine, CursorPosition);
    391       1.1  christos                     EndOfLine = CursorPosition = 0;
    392       1.1  christos                     PreviousCommandIndex = CurrentCommandIndex;
    393       1.1  christos                     continue;
    394       1.1  christos                 }
    395       1.1  christos 
    396       1.1  christos                 PreviousCommandIndex = CurrentCommandIndex;
    397       1.1  christos                 CurrentCommandIndex++;
    398       1.1  christos 
    399       1.1  christos                  /* Get the historical command from the debugger */
    400       1.1  christos 
    401       1.1  christos                 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
    402       1.1  christos                 if (!NextCommand)
    403       1.1  christos                 {
    404       1.1  christos                     return (AE_ERROR);
    405       1.1  christos                 }
    406       1.1  christos 
    407       1.1  christos                 /* Make this the active command and echo it */
    408       1.1  christos 
    409       1.1  christos                 AcpiAcClearLine (EndOfLine, CursorPosition);
    410       1.1  christos                 strcpy (Buffer, NextCommand);
    411       1.1  christos                 fprintf (stdout, "%s", Buffer);
    412       1.1  christos                 EndOfLine = CursorPosition = strlen (Buffer);
    413       1.1  christos                 continue;
    414       1.1  christos 
    415       1.1  christos             case 0x31:
    416       1.1  christos             case 0x32:
    417       1.1  christos             case 0x33:
    418       1.1  christos             case 0x34:
    419       1.1  christos             case 0x35:
    420       1.1  christos             case 0x36:
    421       1.1  christos                 /*
    422       1.1  christos                  * Ignore the various keys like insert/delete/home/end, etc.
    423       1.1  christos                  * But we must eat the final character of the ESC sequence.
    424       1.1  christos                  */
    425   1.1.1.9  christos                 (void) getchar ();
    426       1.1  christos                 continue;
    427       1.1  christos 
    428       1.1  christos             default:
    429       1.1  christos 
    430       1.1  christos                 /* Ignore random escape sequences that we don't care about */
    431       1.1  christos 
    432       1.1  christos                 continue;
    433       1.1  christos             }
    434       1.1  christos             continue;
    435       1.1  christos         }
    436       1.1  christos     }
    437       1.1  christos }
    438