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