source: rtems/cpukit/libmisc/shell/shell_getchar.c @ 7eada71

4.115
Last change on this file since 7eada71 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 *
3 *  Handle keys for the shell.
4 *
5 *  Author:
6 *
7 *   Chris Johns (chrisj@rtems.org)
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.rtems.org/license/LICENSE.
12 */
13
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdio.h>
19#include <time.h>
20
21#include <rtems.h>
22#include <rtems/error.h>
23#include <rtems/system.h>
24#include <rtems/shell.h>
25#include <rtems/shellconfig.h>
26#include "internal.h"
27
28/*
29 * Taken from the monitor code.
30 */
31
32/*
33 * Translation tables. Not sure if this is the best way to
34 * handle this, how-ever I wish to avoid the overhead of
35 * including a more complete and standard environment such
36 * as ncurses.
37 */
38
39struct translation_table
40{
41  char                     expecting;
42  const struct translation_table *branch;
43  unsigned int             key;
44};
45
46static const struct translation_table trans_one[] =
47{
48  { '\x7e', 0, RTEMS_SHELL_KEYS_HOME },
49  { 0,      0, 0 }
50};
51
52static const struct translation_table trans_two[] =
53{
54  { '~', 0, RTEMS_SHELL_KEYS_INS },
55  { 0,   0, 0 }
56};
57
58static const struct translation_table trans_three[] =
59{
60  { '~', 0, RTEMS_SHELL_KEYS_DEL },
61  { 0,   0, 0 }
62};
63
64static const struct translation_table trans_tab_csi[] =
65{
66  { '1', trans_one,   0 },
67  { '2', trans_two,   0 },
68  { '3', trans_three, 0 },
69  { 'A', 0,           RTEMS_SHELL_KEYS_UARROW },
70  { 'B', 0,           RTEMS_SHELL_KEYS_DARROW },
71  { 'D', 0,           RTEMS_SHELL_KEYS_LARROW },
72  { 'C', 0,           RTEMS_SHELL_KEYS_RARROW },
73  { 'F', 0,           RTEMS_SHELL_KEYS_END },
74  { 'H', 0,           RTEMS_SHELL_KEYS_HOME },
75  { 0,   0,           0 }
76};
77
78static const struct translation_table trans_tab_O[] =
79{
80  { '1', 0, RTEMS_SHELL_KEYS_F1 },
81  { '2', 0, RTEMS_SHELL_KEYS_F2 },
82  { '3', 0, RTEMS_SHELL_KEYS_F3 },
83  { '4', 0, RTEMS_SHELL_KEYS_F4 },
84  { '5', 0, RTEMS_SHELL_KEYS_F5 },
85  { '6', 0, RTEMS_SHELL_KEYS_F6 },
86  { '7', 0, RTEMS_SHELL_KEYS_F7 },
87  { '8', 0, RTEMS_SHELL_KEYS_F8 },
88  { '9', 0, RTEMS_SHELL_KEYS_F9 },
89  { ':', 0, RTEMS_SHELL_KEYS_F10 },
90  { 'F', 0, RTEMS_SHELL_KEYS_END },
91  { 'P', 0, RTEMS_SHELL_KEYS_F1 },
92  { 'Q', 0, RTEMS_SHELL_KEYS_F2 },
93  { 'R', 0, RTEMS_SHELL_KEYS_F3 },
94  { 'S', 0, RTEMS_SHELL_KEYS_F4 },
95  { 'T', 0, RTEMS_SHELL_KEYS_F5 },
96  { 'U', 0, RTEMS_SHELL_KEYS_F6 },
97  { 'V', 0, RTEMS_SHELL_KEYS_F7 },
98  { 'W', 0, RTEMS_SHELL_KEYS_F8 },
99  { 'X', 0, RTEMS_SHELL_KEYS_F9 },
100  { 'Y', 0, RTEMS_SHELL_KEYS_F10 },
101  { 0,   0, 0 }
102};
103
104static const struct translation_table trans_tab[] =
105{
106  { '[', trans_tab_csi, 0 },    /* CSI command sequences */
107  { 'O', trans_tab_O,   0 },    /* O are the fuction keys */
108  { 0,   0,             0 }
109};
110
111/*
112 * Perform a basic tranlation for some ANSI/VT100 key codes.
113 * This code could do with a timeout on the ESC as it is
114 * now lost from the input stream. It is not* used by the
115 * line editor below so considiered not worth the effort.
116 */
117
118unsigned int
119rtems_shell_getchar (FILE *in)
120{
121  const struct translation_table *translation = 0;
122  for (;;)
123  {
124    int c = fgetc (in);
125    if (c == EOF)
126      return EOF;
127    if (c == 27)
128      translation = trans_tab;
129    else
130    {
131      /*
132       * If no translation happing just pass through
133       * and return the key.
134       */
135      if (translation)
136      {
137        /*
138         * Scan the current table for the key, and if found
139         * see if this key is a fork. If so follow it and
140         * wait else return the extended key.
141         */
142        int index    = 0;
143        int branched = 0;
144        while ((translation[index].expecting != '\0') ||
145               (translation[index].key != '\0'))
146        {
147          if (translation[index].expecting == c)
148          {
149            /*
150             * A branch is take if more keys are to come.
151             */
152            if (translation[index].branch == 0)
153              return RTEMS_SHELL_KEYS_EXTENDED | translation[index].key;
154            else
155            {
156              translation = translation[index].branch;
157              branched    = 1;
158              break;
159            }
160          }
161          index++;
162        }
163        /*
164         * Who knows what these keys are, just drop them.
165         */
166        if (!branched)
167          translation = 0;
168      }
169      else
170        return c;
171    }
172  }
173}
174
Note: See TracBrowser for help on using the repository browser.