acgetline.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /******************************************************************************
  2. *
  3. * Module Name: acgetline - local line editing
  4. *
  5. *****************************************************************************/
  6. /******************************************************************************
  7. *
  8. * 1. Copyright Notice
  9. *
  10. * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
  11. * All rights reserved.
  12. *
  13. * 2. License
  14. *
  15. * 2.1. This is your license from Intel Corp. under its intellectual property
  16. * rights. You may have additional license terms from the party that provided
  17. * you this software, covering your right to use that party's intellectual
  18. * property rights.
  19. *
  20. * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
  21. * copy of the source code appearing in this file ("Covered Code") an
  22. * irrevocable, perpetual, worldwide license under Intel's copyrights in the
  23. * base code distributed originally by Intel ("Original Intel Code") to copy,
  24. * make derivatives, distribute, use and display any portion of the Covered
  25. * Code in any form, with the right to sublicense such rights; and
  26. *
  27. * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
  28. * license (with the right to sublicense), under only those claims of Intel
  29. * patents that are infringed by the Original Intel Code, to make, use, sell,
  30. * offer to sell, and import the Covered Code and derivative works thereof
  31. * solely to the minimum extent necessary to exercise the above copyright
  32. * license, and in no event shall the patent license extend to any additions
  33. * to or modifications of the Original Intel Code. No other license or right
  34. * is granted directly or by implication, estoppel or otherwise;
  35. *
  36. * The above copyright and patent license is granted only if the following
  37. * conditions are met:
  38. *
  39. * 3. Conditions
  40. *
  41. * 3.1. Redistribution of Source with Rights to Further Distribute Source.
  42. * Redistribution of source code of any substantial portion of the Covered
  43. * Code or modification with rights to further distribute source must include
  44. * the above Copyright Notice, the above License, this list of Conditions,
  45. * and the following Disclaimer and Export Compliance provision. In addition,
  46. * Licensee must cause all Covered Code to which Licensee contributes to
  47. * contain a file documenting the changes Licensee made to create that Covered
  48. * Code and the date of any change. Licensee must include in that file the
  49. * documentation of any changes made by any predecessor Licensee. Licensee
  50. * must include a prominent statement that the modification is derived,
  51. * directly or indirectly, from Original Intel Code.
  52. *
  53. * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
  54. * Redistribution of source code of any substantial portion of the Covered
  55. * Code or modification without rights to further distribute source must
  56. * include the following Disclaimer and Export Compliance provision in the
  57. * documentation and/or other materials provided with distribution. In
  58. * addition, Licensee may not authorize further sublicense of source of any
  59. * portion of the Covered Code, and must include terms to the effect that the
  60. * license from Licensee to its licensee is limited to the intellectual
  61. * property embodied in the software Licensee provides to its licensee, and
  62. * not to intellectual property embodied in modifications its licensee may
  63. * make.
  64. *
  65. * 3.3. Redistribution of Executable. Redistribution in executable form of any
  66. * substantial portion of the Covered Code or modification must reproduce the
  67. * above Copyright Notice, and the following Disclaimer and Export Compliance
  68. * provision in the documentation and/or other materials provided with the
  69. * distribution.
  70. *
  71. * 3.4. Intel retains all right, title, and interest in and to the Original
  72. * Intel Code.
  73. *
  74. * 3.5. Neither the name Intel nor any other trademark owned or controlled by
  75. * Intel shall be used in advertising or otherwise to promote the sale, use or
  76. * other dealings in products derived from or relating to the Covered Code
  77. * without prior written authorization from Intel.
  78. *
  79. * 4. Disclaimer and Export Compliance
  80. *
  81. * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
  82. * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
  83. * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
  84. * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
  85. * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
  86. * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
  87. * PARTICULAR PURPOSE.
  88. *
  89. * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
  90. * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
  91. * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
  92. * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
  93. * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
  94. * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
  95. * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
  96. * LIMITED REMEDY.
  97. *
  98. * 4.3. Licensee shall not export, either directly or indirectly, any of this
  99. * software or system incorporating such software without first obtaining any
  100. * required license or other approval from the U. S. Department of Commerce or
  101. * any other agency or department of the United States Government. In the
  102. * event Licensee exports any such software from the United States or
  103. * re-exports any such software from a foreign destination, Licensee shall
  104. * ensure that the distribution and export/re-export of the software is in
  105. * compliance with all laws, regulations, orders, or other restrictions of the
  106. * U.S. Export Administration Regulations. Licensee agrees that neither it nor
  107. * any of its subsidiaries will export/re-export any technical data, process,
  108. * software, or service, directly or indirectly, to any country for which the
  109. * United States government or any agency thereof requires an export license,
  110. * other governmental approval, or letter of assurance, without first obtaining
  111. * such license, approval or letter.
  112. *
  113. *****************************************************************************/
  114. #include "acpi.h"
  115. #include "accommon.h"
  116. #include "amlcode.h"
  117. #include "acparser.h"
  118. #include "acdebug.h"
  119. #include <stdio.h>
  120. /*
  121. * This is an os-independent implementation of line-editing services needed
  122. * by the AcpiExec utility. It uses getchar() and putchar() and the existing
  123. * history support provided by the AML debugger. It assumes that the terminal
  124. * is in the correct line-editing mode such as raw and noecho. The OSL
  125. * interface AcpiOsInitialize should do this. AcpiOsTerminate should put the
  126. * terminal back into the original mode.
  127. */
  128. #define _COMPONENT ACPI_OS_SERVICES
  129. ACPI_MODULE_NAME ("acgetline")
  130. /* Local prototypes */
  131. static void
  132. AcpiAcClearLine (
  133. UINT32 EndOfLine,
  134. UINT32 CursorPosition);
  135. /* Various ASCII constants */
  136. #define _ASCII_NUL 0
  137. #define _ASCII_BACKSPACE 0x08
  138. #define _ASCII_TAB 0x09
  139. #define _ASCII_ESCAPE 0x1B
  140. #define _ASCII_SPACE 0x20
  141. #define _ASCII_LEFT_BRACKET 0x5B
  142. #define _ASCII_DEL 0x7F
  143. #define _ASCII_UP_ARROW 'A'
  144. #define _ASCII_DOWN_ARROW 'B'
  145. #define _ASCII_RIGHT_ARROW 'C'
  146. #define _ASCII_LEFT_ARROW 'D'
  147. #define _ASCII_NEWLINE '\n'
  148. extern UINT32 AcpiGbl_NextCmdNum;
  149. /* Erase a single character on the input command line */
  150. #define ACPI_CLEAR_CHAR() \
  151. putchar (_ASCII_BACKSPACE); \
  152. putchar (_ASCII_SPACE); \
  153. putchar (_ASCII_BACKSPACE);
  154. /* Backup cursor by Count positions */
  155. #define ACPI_BACKUP_CURSOR(i, Count) \
  156. for (i = 0; i < (Count); i++) \
  157. {putchar (_ASCII_BACKSPACE);}
  158. /******************************************************************************
  159. *
  160. * FUNCTION: AcpiAcClearLine
  161. *
  162. * PARAMETERS: EndOfLine - Current end-of-line index
  163. * CursorPosition - Current cursor position within line
  164. *
  165. * RETURN: None
  166. *
  167. * DESCRIPTION: Clear the entire command line the hard way, but probably the
  168. * most portable.
  169. *
  170. *****************************************************************************/
  171. static void
  172. AcpiAcClearLine (
  173. UINT32 EndOfLine,
  174. UINT32 CursorPosition)
  175. {
  176. UINT32 i;
  177. if (CursorPosition < EndOfLine)
  178. {
  179. /* Clear line from current position to end of line */
  180. for (i = 0; i < (EndOfLine - CursorPosition); i++)
  181. {
  182. putchar (' ');
  183. }
  184. }
  185. /* Clear the entire line */
  186. for (; EndOfLine > 0; EndOfLine--)
  187. {
  188. ACPI_CLEAR_CHAR ();
  189. }
  190. }
  191. /******************************************************************************
  192. *
  193. * FUNCTION: AcpiOsGetLine
  194. *
  195. * PARAMETERS: Buffer - Where to return the command line
  196. * BufferLength - Maximum length of Buffer
  197. * BytesRead - Where the actual byte count is returned
  198. *
  199. * RETURN: Status and actual bytes read
  200. *
  201. * DESCRIPTION: Get the next input line from the terminal. NOTE: terminal
  202. * is expected to be in a mode that supports line-editing (raw,
  203. * noecho). This function is intended to be very portable. Also,
  204. * it uses the history support implemented in the AML debugger.
  205. *
  206. *****************************************************************************/
  207. ACPI_STATUS
  208. AcpiOsGetLine (
  209. char *Buffer,
  210. UINT32 BufferLength,
  211. UINT32 *BytesRead)
  212. {
  213. char *NextCommand;
  214. UINT32 MaxCommandIndex = AcpiGbl_NextCmdNum - 1;
  215. UINT32 CurrentCommandIndex = MaxCommandIndex;
  216. UINT32 PreviousCommandIndex = MaxCommandIndex;
  217. int InputChar;
  218. UINT32 CursorPosition = 0;
  219. UINT32 EndOfLine = 0;
  220. UINT32 i;
  221. /* Always clear the line buffer before we read a new line */
  222. memset (Buffer, 0, BufferLength);
  223. /*
  224. * This loop gets one character at a time (except for esc sequences)
  225. * until a newline or error is detected.
  226. *
  227. * Note: Don't attempt to write terminal control ESC sequences, even
  228. * though it makes certain things more difficult.
  229. */
  230. while (1)
  231. {
  232. if (EndOfLine >= (BufferLength - 1))
  233. {
  234. return (AE_BUFFER_OVERFLOW);
  235. }
  236. InputChar = getchar ();
  237. switch (InputChar)
  238. {
  239. default: /* This is the normal character case */
  240. /* Echo the character (at EOL) and copy it to the line buffer */
  241. if (EndOfLine == CursorPosition)
  242. {
  243. putchar (InputChar);
  244. Buffer[EndOfLine] = (char) InputChar;
  245. EndOfLine++;
  246. CursorPosition++;
  247. Buffer[EndOfLine] = 0;
  248. continue;
  249. }
  250. /* Insert character into the middle of the buffer */
  251. memmove (&Buffer[CursorPosition + 1], &Buffer[CursorPosition],
  252. (EndOfLine - CursorPosition + 1));
  253. Buffer [CursorPosition] = (char) InputChar;
  254. Buffer [EndOfLine + 1] = 0;
  255. /* Display the new part of line starting at the new character */
  256. fprintf (stdout, "%s", &Buffer[CursorPosition]);
  257. /* Restore cursor */
  258. ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
  259. CursorPosition++;
  260. EndOfLine++;
  261. continue;
  262. case _ASCII_DEL: /* Backspace key */
  263. if (!EndOfLine) /* Any characters on the command line? */
  264. {
  265. continue;
  266. }
  267. if (EndOfLine == CursorPosition) /* Erase the final character */
  268. {
  269. ACPI_CLEAR_CHAR ();
  270. EndOfLine--;
  271. CursorPosition--;
  272. continue;
  273. }
  274. if (!CursorPosition) /* Do not backup beyond start of line */
  275. {
  276. continue;
  277. }
  278. /* Remove the character from the line */
  279. memmove (&Buffer[CursorPosition - 1], &Buffer[CursorPosition],
  280. (EndOfLine - CursorPosition + 1));
  281. /* Display the new part of line starting at the new character */
  282. putchar (_ASCII_BACKSPACE);
  283. fprintf (stdout, "%s ", &Buffer[CursorPosition - 1]);
  284. /* Restore cursor */
  285. ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition + 1);
  286. EndOfLine--;
  287. if (CursorPosition > 0)
  288. {
  289. CursorPosition--;
  290. }
  291. continue;
  292. case _ASCII_NEWLINE: /* Normal exit case at end of command line */
  293. case _ASCII_NUL:
  294. /* Return the number of bytes in the command line string */
  295. if (BytesRead)
  296. {
  297. *BytesRead = EndOfLine;
  298. }
  299. /* Echo, terminate string buffer, and exit */
  300. putchar (InputChar);
  301. Buffer[EndOfLine] = 0;
  302. return (AE_OK);
  303. case _ASCII_TAB:
  304. /* Ignore */
  305. continue;
  306. case EOF:
  307. return (AE_ERROR);
  308. case _ASCII_ESCAPE:
  309. /* Check for escape sequences of the form "ESC[x" */
  310. InputChar = getchar ();
  311. if (InputChar != _ASCII_LEFT_BRACKET)
  312. {
  313. continue; /* Ignore this ESC, does not have the '[' */
  314. }
  315. /* Get the code following the ESC [ */
  316. InputChar = getchar (); /* Backup one character */
  317. switch (InputChar)
  318. {
  319. case _ASCII_LEFT_ARROW:
  320. if (CursorPosition > 0)
  321. {
  322. putchar (_ASCII_BACKSPACE);
  323. CursorPosition--;
  324. }
  325. continue;
  326. case _ASCII_RIGHT_ARROW:
  327. /*
  328. * Move one character forward. Do this without sending
  329. * ESC sequence to the terminal for max portability.
  330. */
  331. if (CursorPosition < EndOfLine)
  332. {
  333. /* Backup to start of line and print the entire line */
  334. ACPI_BACKUP_CURSOR (i, CursorPosition);
  335. fprintf (stdout, "%s", Buffer);
  336. /* Backup to where the cursor should be */
  337. CursorPosition++;
  338. ACPI_BACKUP_CURSOR (i, EndOfLine - CursorPosition);
  339. }
  340. continue;
  341. case _ASCII_UP_ARROW:
  342. /* If no commands available or at start of history list, ignore */
  343. if (!CurrentCommandIndex)
  344. {
  345. continue;
  346. }
  347. /* Manage our up/down progress */
  348. if (CurrentCommandIndex > PreviousCommandIndex)
  349. {
  350. CurrentCommandIndex = PreviousCommandIndex;
  351. }
  352. /* Get the historical command from the debugger */
  353. NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
  354. if (!NextCommand)
  355. {
  356. return (AE_ERROR);
  357. }
  358. /* Make this the active command and echo it */
  359. AcpiAcClearLine (EndOfLine, CursorPosition);
  360. strcpy (Buffer, NextCommand);
  361. fprintf (stdout, "%s", Buffer);
  362. EndOfLine = CursorPosition = strlen (Buffer);
  363. PreviousCommandIndex = CurrentCommandIndex;
  364. CurrentCommandIndex--;
  365. continue;
  366. case _ASCII_DOWN_ARROW:
  367. if (!MaxCommandIndex) /* Any commands available? */
  368. {
  369. continue;
  370. }
  371. /* Manage our up/down progress */
  372. if (CurrentCommandIndex < PreviousCommandIndex)
  373. {
  374. CurrentCommandIndex = PreviousCommandIndex;
  375. }
  376. /* If we are the end of the history list, output a clear new line */
  377. if ((CurrentCommandIndex + 1) > MaxCommandIndex)
  378. {
  379. AcpiAcClearLine (EndOfLine, CursorPosition);
  380. EndOfLine = CursorPosition = 0;
  381. PreviousCommandIndex = CurrentCommandIndex;
  382. continue;
  383. }
  384. PreviousCommandIndex = CurrentCommandIndex;
  385. CurrentCommandIndex++;
  386. /* Get the historical command from the debugger */
  387. NextCommand = AcpiDbGetHistoryByIndex (CurrentCommandIndex);
  388. if (!NextCommand)
  389. {
  390. return (AE_ERROR);
  391. }
  392. /* Make this the active command and echo it */
  393. AcpiAcClearLine (EndOfLine, CursorPosition);
  394. strcpy (Buffer, NextCommand);
  395. fprintf (stdout, "%s", Buffer);
  396. EndOfLine = CursorPosition = strlen (Buffer);
  397. continue;
  398. case 0x31:
  399. case 0x32:
  400. case 0x33:
  401. case 0x34:
  402. case 0x35:
  403. case 0x36:
  404. /*
  405. * Ignore the various keys like insert/delete/home/end, etc.
  406. * But we must eat the final character of the ESC sequence.
  407. */
  408. InputChar = getchar ();
  409. continue;
  410. default:
  411. /* Ignore random escape sequences that we don't care about */
  412. continue;
  413. }
  414. continue;
  415. }
  416. }
  417. }