source: rtems/cpukit/libmisc/shell/shell_getchar.c @ 288b1f9

4.104.114.95
Last change on this file since 288b1f9 was 288b1f9, checked in by Joel Sherrill <joel.sherrill@…>, on 05/27/08 at 17:25:45

2008-05-27 Joel Sherrill <joel.sherrill@…>

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