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