1 /******************************************************************************
3 * Module Name: acgetline - local line editing
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/amlcode.h>
47 #include <contrib/dev/acpica/include/acparser.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
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);
265 if (CursorPosition > 0)
271 case _ASCII_NEWLINE: /* Normal exit case at end of command line */
274 /* Return the number of bytes in the command line string */
278 *BytesRead = EndOfLine;
281 /* Echo, terminate string buffer, and exit */
284 Buffer[EndOfLine] = 0;
299 /* Check for escape sequences of the form "ESC[x" */
301 InputChar = getchar ();
302 if (InputChar != _ASCII_LEFT_BRACKET)
304 continue; /* Ignore this ESC, does not have the '[' */
307 /* Get the code following the ESC [ */
309 InputChar = getchar (); /* Backup one character */
312 case _ASCII_LEFT_ARROW:
314 if (CursorPosition > 0)
316 putchar (_ASCII_BACKSPACE);
321 case _ASCII_RIGHT_ARROW:
323 * Move one character forward. Do this without sending
324 * ESC sequence to the terminal for max portability.
326 if (CursorPosition < EndOfLine)
328 /* Backup to start of line and print the entire line */
330 ACPI_BACKUP_CURSOR (i, CursorPosition);
331 fprintf (stdout, "%s", Buffer);
333 /* Backup to where the cursor should be */
336 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
340 case _ASCII_UP_ARROW:
342 /* If no commands available or at start of history list, ignore */
344 if (!CurrentCommandIndex)
349 /* Manage our up/down progress */
351 if (CurrentCommandIndex > PreviousCommandIndex)
353 CurrentCommandIndex = PreviousCommandIndex;
356 /* Get the historical command from the debugger */
358 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
364 /* Make this the active command and echo it */
366 AcpiAcClearLine (EndOfLine, CursorPosition);
367 strcpy (Buffer, NextCommand);
368 fprintf (stdout, "%s", Buffer);
369 EndOfLine = CursorPosition = strlen (Buffer);
371 PreviousCommandIndex = CurrentCommandIndex;
372 CurrentCommandIndex--;
375 case _ASCII_DOWN_ARROW:
377 if (!MaxCommandIndex) /* Any commands available? */
382 /* Manage our up/down progress */
384 if (CurrentCommandIndex < PreviousCommandIndex)
386 CurrentCommandIndex = PreviousCommandIndex;
389 /* If we are the end of the history list, output a clear new line */
391 if ((CurrentCommandIndex + 1) > MaxCommandIndex)
393 AcpiAcClearLine (EndOfLine, CursorPosition);
394 EndOfLine = CursorPosition = 0;
395 PreviousCommandIndex = CurrentCommandIndex;
399 PreviousCommandIndex = CurrentCommandIndex;
400 CurrentCommandIndex++;
402 /* Get the historical command from the debugger */
404 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
410 /* Make this the active command and echo it */
412 AcpiAcClearLine (EndOfLine, CursorPosition);
413 strcpy (Buffer, NextCommand);
414 fprintf (stdout, "%s", Buffer);
415 EndOfLine = CursorPosition = strlen (Buffer);
425 * Ignore the various keys like insert/delete/home/end, etc.
426 * But we must eat the final character of the ESC sequence.
428 InputChar = getchar ();
433 /* Ignore random escape sequences that we don't care about */