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.
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>
51 * This is an os-independent implementation of line-editing services needed
52 * by the AcpiExec utility. It uses getchar() and putchar() and the existing
53 * history support provided by the AML debugger. It assumes that the terminal
54 * is in the correct line-editing mode such as raw and noecho. The OSL
55 * interface AcpiOsInitialize should do this. AcpiOsTerminate should put the
56 * terminal back into the original mode.
58 #define _COMPONENT ACPI_OS_SERVICES
59 ACPI_MODULE_NAME ("acgetline")
62 /* Local prototypes */
67 UINT32 CursorPosition);
69 /* Various ASCII constants */
72 #define _ASCII_BACKSPACE 0x08
73 #define _ASCII_TAB 0x09
74 #define _ASCII_ESCAPE 0x1B
75 #define _ASCII_SPACE 0x20
76 #define _ASCII_LEFT_BRACKET 0x5B
77 #define _ASCII_DEL 0x7F
78 #define _ASCII_UP_ARROW 'A'
79 #define _ASCII_DOWN_ARROW 'B'
80 #define _ASCII_RIGHT_ARROW 'C'
81 #define _ASCII_LEFT_ARROW 'D'
82 #define _ASCII_NEWLINE '\n'
84 extern UINT32 AcpiGbl_NextCmdNum;
86 /* Erase a single character on the input command line */
88 #define ACPI_CLEAR_CHAR() \
89 putchar (_ASCII_BACKSPACE); \
90 putchar (_ASCII_SPACE); \
91 putchar (_ASCII_BACKSPACE);
93 /* Backup cursor by Count positions */
95 #define ACPI_BACKUP_CURSOR(i, Count) \
96 for (i = 0; i < (Count); i++) \
97 {putchar (_ASCII_BACKSPACE);}
100 /******************************************************************************
102 * FUNCTION: AcpiAcClearLine
104 * PARAMETERS: EndOfLine - Current end-of-line index
105 * CursorPosition - Current cursor position within line
109 * DESCRIPTION: Clear the entire command line the hard way, but probably the
112 *****************************************************************************/
117 UINT32 CursorPosition)
122 if (CursorPosition < EndOfLine)
124 /* Clear line from current position to end of line */
126 for (i = 0; i < (EndOfLine - CursorPosition); i++)
132 /* Clear the entire line */
134 for (; EndOfLine > 0; EndOfLine--)
141 /******************************************************************************
143 * FUNCTION: AcpiOsGetLine
145 * PARAMETERS: Buffer - Where to return the command line
146 * BufferLength - Maximum length of Buffer
147 * BytesRead - Where the actual byte count is returned
149 * RETURN: Status and actual bytes read
151 * DESCRIPTION: Get the next input line from the terminal. NOTE: terminal
152 * is expected to be in a mode that supports line-editing (raw,
153 * noecho). This function is intended to be very portable. Also,
154 * it uses the history support implemented in the AML debugger.
156 *****************************************************************************/
165 UINT32 MaxCommandIndex = AcpiGbl_NextCmdNum - 1;
166 UINT32 CurrentCommandIndex = MaxCommandIndex;
167 UINT32 PreviousCommandIndex = MaxCommandIndex;
169 UINT32 CursorPosition = 0;
170 UINT32 EndOfLine = 0;
174 /* Always clear the line buffer before we read a new line */
176 memset (Buffer, 0, BufferLength);
179 * This loop gets one character at a time (except for esc sequences)
180 * until a newline or error is detected.
182 * Note: Don't attempt to write terminal control ESC sequences, even
183 * though it makes certain things more difficult.
187 if (EndOfLine >= (BufferLength - 1))
189 return (AE_BUFFER_OVERFLOW);
192 InputChar = getchar ();
195 default: /* This is the normal character case */
197 /* Echo the character (at EOL) and copy it to the line buffer */
199 if (EndOfLine == CursorPosition)
202 Buffer[EndOfLine] = (char) InputChar;
206 Buffer[EndOfLine] = 0;
210 /* Insert character into the middle of the buffer */
212 memmove (&Buffer[CursorPosition + 1], &Buffer[CursorPosition],
213 (EndOfLine - CursorPosition + 1));
215 Buffer [CursorPosition] = (char) InputChar;
216 Buffer [EndOfLine + 1] = 0;
218 /* Display the new part of line starting at the new character */
220 fprintf (stdout, "%s", &Buffer[CursorPosition]);
224 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
229 case _ASCII_DEL: /* Backspace key */
231 if (!EndOfLine) /* Any characters on the command line? */
236 if (EndOfLine == CursorPosition) /* Erase the final character */
244 if (!CursorPosition) /* Do not backup beyond start of line */
249 /* Remove the character from the line */
251 memmove (&Buffer[CursorPosition - 1], &Buffer[CursorPosition],
252 (EndOfLine - CursorPosition + 1));
254 /* Display the new part of line starting at the new character */
256 putchar (_ASCII_BACKSPACE);
257 fprintf (stdout, "%s ", &Buffer[CursorPosition - 1]);
261 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition + 1);
264 if (CursorPosition > 0)
270 case _ASCII_NEWLINE: /* Normal exit case at end of command line */
273 /* Return the number of bytes in the command line string */
277 *BytesRead = EndOfLine;
280 /* Echo, terminate string buffer, and exit */
283 Buffer[EndOfLine] = 0;
298 /* Check for escape sequences of the form "ESC[x" */
300 InputChar = getchar ();
301 if (InputChar != _ASCII_LEFT_BRACKET)
303 continue; /* Ignore this ESC, does not have the '[' */
306 /* Get the code following the ESC [ */
308 InputChar = getchar (); /* Backup one character */
311 case _ASCII_LEFT_ARROW:
313 if (CursorPosition > 0)
315 putchar (_ASCII_BACKSPACE);
320 case _ASCII_RIGHT_ARROW:
322 * Move one character forward. Do this without sending
323 * ESC sequence to the terminal for max portability.
325 if (CursorPosition < EndOfLine)
327 /* Backup to start of line and print the entire line */
329 ACPI_BACKUP_CURSOR (i, CursorPosition);
330 fprintf (stdout, "%s", Buffer);
332 /* Backup to where the cursor should be */
335 ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
339 case _ASCII_UP_ARROW:
341 /* If no commands available or at start of history list, ignore */
343 if (!CurrentCommandIndex)
348 /* Manage our up/down progress */
350 if (CurrentCommandIndex > PreviousCommandIndex)
352 CurrentCommandIndex = PreviousCommandIndex;
355 /* Get the historical command from the debugger */
357 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
363 /* Make this the active command and echo it */
365 AcpiAcClearLine (EndOfLine, CursorPosition);
366 strcpy (Buffer, NextCommand);
367 fprintf (stdout, "%s", Buffer);
368 EndOfLine = CursorPosition = strlen (Buffer);
370 PreviousCommandIndex = CurrentCommandIndex;
371 CurrentCommandIndex--;
374 case _ASCII_DOWN_ARROW:
376 if (!MaxCommandIndex) /* Any commands available? */
381 /* Manage our up/down progress */
383 if (CurrentCommandIndex < PreviousCommandIndex)
385 CurrentCommandIndex = PreviousCommandIndex;
388 /* If we are the end of the history list, output a clear new line */
390 if ((CurrentCommandIndex + 1) > MaxCommandIndex)
392 AcpiAcClearLine (EndOfLine, CursorPosition);
393 EndOfLine = CursorPosition = 0;
394 PreviousCommandIndex = CurrentCommandIndex;
398 PreviousCommandIndex = CurrentCommandIndex;
399 CurrentCommandIndex++;
401 /* Get the historical command from the debugger */
403 NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
409 /* Make this the active command and echo it */
411 AcpiAcClearLine (EndOfLine, CursorPosition);
412 strcpy (Buffer, NextCommand);
413 fprintf (stdout, "%s", Buffer);
414 EndOfLine = CursorPosition = strlen (Buffer);
424 * Ignore the various keys like insert/delete/home/end, etc.
425 * But we must eat the final character of the ESC sequence.
427 InputChar = getchar ();
432 /* Ignore random escape sequences that we don't care about */