1 /******************************************************************************
3 * Module Name: acgetline - local line editing
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
53 * This is an os-independent implementation of line-editing services needed
54 * by the AcpiExec utility. It uses getchar() and putchar() and the existing
55 * history support provided by the AML debugger. It assumes that the terminal
56 * is in the correct line-editing mode such as raw and noecho. The OSL
57 * interface AcpiOsInitialize should do this. AcpiOsTerminate should put the
58 * terminal back into the original mode.
60 #define _COMPONENT ACPI_OS_SERVICES
61 ACPI_MODULE_NAME ("acgetline")
64 /* Local prototypes */
69 UINT32 CursorPosition);
71 /* Various ASCII constants */
74 #define _ASCII_BACKSPACE 0x08
75 #define _ASCII_TAB 0x09
76 #define _ASCII_ESCAPE 0x1B
77 #define _ASCII_SPACE 0x20
78 #define _ASCII_LEFT_BRACKET 0x5B
79 #define _ASCII_DEL 0x7F
80 #define _ASCII_UP_ARROW 'A'
81 #define _ASCII_DOWN_ARROW 'B'
82 #define _ASCII_RIGHT_ARROW 'C'
83 #define _ASCII_LEFT_ARROW 'D'
84 #define _ASCII_NEWLINE '\n'
86 extern UINT32 AcpiGbl_NextCmdNum;
88 /* Erase a single character on the input command line */
90 #define ACPI_CLEAR_CHAR() \
91 putchar (_ASCII_BACKSPACE); \
92 putchar (_ASCII_SPACE); \
93 putchar (_ASCII_BACKSPACE);
95 /* Backup cursor by Count positions */
97 #define ACPI_BACKUP_CURSOR(i, Count) \
98 for (i = 0; i < (Count); i++) \
99 {putchar (_ASCII_BACKSPACE);}
102 /******************************************************************************
104 * FUNCTION: AcpiAcClearLine
106 * PARAMETERS: EndOfLine - Current end-of-line index
107 * CursorPosition - Current cursor position within line
111 * DESCRIPTION: Clear the entire command line the hard way, but probably the
114 *****************************************************************************/
119 UINT32 CursorPosition)
124 if (CursorPosition < EndOfLine)
126 /* Clear line from current position to end of line */
128 for (i = 0; i < (EndOfLine - CursorPosition); i++)
134 /* Clear the entire line */
136 for (; EndOfLine > 0; EndOfLine--)
143 /******************************************************************************
145 * FUNCTION: AcpiOsGetLine
147 * PARAMETERS: Buffer - Where to return the command line
148 * BufferLength - Maximum length of Buffer
149 * BytesRead - Where the actual byte count is returned
151 * RETURN: Status and actual bytes read
153 * DESCRIPTION: Get the next input line from the terminal. NOTE: terminal
154 * is expected to be in a mode that supports line-editing (raw,
155 * noecho). This function is intended to be very portable. Also,
156 * it uses the history support implemented in the AML debugger.
158 *****************************************************************************/
167 UINT32 MaxCommandIndex = AcpiGbl_NextCmdNum - 1;
168 UINT32 CurrentCommandIndex = MaxCommandIndex;
169 UINT32 PreviousCommandIndex = MaxCommandIndex;
171 UINT32 CursorPosition = 0;
172 UINT32 EndOfLine = 0;
176 /* Always clear the line buffer before we read a new line */
178 memset (Buffer, 0, BufferLength);
181 * This loop gets one character at a time (except for esc sequences)
182 * until a newline or error is detected.
184 * Note: Don't attempt to write terminal control ESC sequences, even
185 * though it makes certain things more difficult.
189 if (EndOfLine >= (BufferLength - 1))
191 return (AE_BUFFER_OVERFLOW);
194 InputChar = getchar ();
197 default: /* This is the normal character case */
199 /* Echo the character (at EOL) and copy it to the line buffer */
201 if (EndOfLine == CursorPosition)
204 Buffer[EndOfLine] = (char) InputChar;
208 Buffer[EndOfLine] = 0;
212 /* Insert character into the middle of the buffer */
214 memmove (&Buffer[CursorPosition + 1], &Buffer[CursorPosition],
215 (EndOfLine - CursorPosition + 1));
217 Buffer [CursorPosition] = (char) InputChar;
218 Buffer [EndOfLine + 1] = 0;
220 /* Display the new part of line starting at the new character */
222 fprintf (stdout, "%s", &Buffer[CursorPosition]);
226 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
231 case _ASCII_DEL: /* Backspace key */
233 if (!EndOfLine) /* Any characters on the command line? */
238 if (EndOfLine == CursorPosition) /* Erase the final character */
246 if (!CursorPosition) /* Do not backup beyond start of line */
251 /* Remove the character from the line */
253 memmove (&Buffer[CursorPosition - 1], &Buffer[CursorPosition],
254 (EndOfLine - CursorPosition + 1));
256 /* Display the new part of line starting at the new character */
258 putchar (_ASCII_BACKSPACE);
259 fprintf (stdout, "%s ", &Buffer[CursorPosition - 1]);
263 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition + 1);
266 if (CursorPosition > 0)
272 case _ASCII_NEWLINE: /* Normal exit case at end of command line */
275 /* Return the number of bytes in the command line string */
279 *BytesRead = EndOfLine;
282 /* Echo, terminate string buffer, and exit */
285 Buffer[EndOfLine] = 0;
300 /* Check for escape sequences of the form "ESC[x" */
302 InputChar = getchar ();
303 if (InputChar != _ASCII_LEFT_BRACKET)
305 continue; /* Ignore this ESC, does not have the '[' */
308 /* Get the code following the ESC [ */
310 InputChar = getchar (); /* Backup one character */
313 case _ASCII_LEFT_ARROW:
315 if (CursorPosition > 0)
317 putchar (_ASCII_BACKSPACE);
322 case _ASCII_RIGHT_ARROW:
324 * Move one character forward. Do this without sending
325 * ESC sequence to the terminal for max portability.
327 if (CursorPosition < EndOfLine)
329 /* Backup to start of line and print the entire line */
331 ACPI_BACKUP_CURSOR (i, CursorPosition);
332 fprintf (stdout, "%s", Buffer);
334 /* Backup to where the cursor should be */
337 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
341 case _ASCII_UP_ARROW:
343 /* If no commands available or at start of history list, ignore */
345 if (!CurrentCommandIndex)
350 /* Manage our up/down progress */
352 if (CurrentCommandIndex > PreviousCommandIndex)
354 CurrentCommandIndex = PreviousCommandIndex;
357 /* Get the historical command from the debugger */
359 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
365 /* Make this the active command and echo it */
367 AcpiAcClearLine (EndOfLine, CursorPosition);
368 strcpy (Buffer, NextCommand);
369 fprintf (stdout, "%s", Buffer);
370 EndOfLine = CursorPosition = strlen (Buffer);
372 PreviousCommandIndex = CurrentCommandIndex;
373 CurrentCommandIndex--;
376 case _ASCII_DOWN_ARROW:
378 if (!MaxCommandIndex) /* Any commands available? */
383 /* Manage our up/down progress */
385 if (CurrentCommandIndex < PreviousCommandIndex)
387 CurrentCommandIndex = PreviousCommandIndex;
390 /* If we are the end of the history list, output a clear new line */
392 if ((CurrentCommandIndex + 1) > MaxCommandIndex)
394 AcpiAcClearLine (EndOfLine, CursorPosition);
395 EndOfLine = CursorPosition = 0;
396 PreviousCommandIndex = CurrentCommandIndex;
400 PreviousCommandIndex = CurrentCommandIndex;
401 CurrentCommandIndex++;
403 /* Get the historical command from the debugger */
405 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
411 /* Make this the active command and echo it */
413 AcpiAcClearLine (EndOfLine, CursorPosition);
414 strcpy (Buffer, NextCommand);
415 fprintf (stdout, "%s", Buffer);
416 EndOfLine = CursorPosition = strlen (Buffer);
426 * Ignore the various keys like insert/delete/home/end, etc.
427 * But we must eat the final character of the ESC sequence.
429 InputChar = getchar ();
434 /* Ignore random escape sequences that we don't care about */