summaryrefslogtreecommitdiff
path: root/utility/demo-fw/common/dfm_console.c
blob: 7118a116dbd6bbe321561db10834a2eefdf07ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <string.h>
#include <dbgu/dbgu.h>
#include <stdio.h>

#include "dfm_console.h"
#include "dfm_entry.h"
#include "dfm_cmd.h"

//------------------------------------------------------------------------------
//         Internal variables
//------------------------------------------------------------------------------

///DBGU input buffer
static char gpConsoleBuffer[DBGU_CMDBUFSIZE];

///prompt refresh flag
static unsigned char gDBGUPromptFlag = 0;

///erase sequence	
static char gpEraseSeq[] = "\b \b";
///used to expand TABS
static char   gpTabSeq[] = "        ";

//------------------------------------------------------------------------------
//         Export variables
//------------------------------------------------------------------------------

// global DBGU entry
TInputEntry gDBGUEntry = \
  {0, {NULL,NULL},DBGU_ShowPrompt, DBGU_CommandIsReady, DBGU_GetCommand, NULL};

//------------------------------------------------------------------------------
//         Internal functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
///  output a string to console
///  \param pStr, string to output to DBGU console
//------------------------------------------------------------------------------
static inline void DBGU_puts(const char *pStr)
{
  while(*pStr) {
    DBGU_PutChar(*pStr++);
  }
}

//------------------------------------------------------------------------------
///  remove charaters from buffer positions
//------------------------------------------------------------------------------
static char * DBGU_RemoveCharFromBuf (char *buffer, char *p, int *colp, int *np, int plen)
{
	char *s;

	if (*np == 0) {
		return (p);
	}

	if (*(--p) == '\t') {
		while (*colp > plen) {
			DBGU_puts (gpEraseSeq);
			(*colp)--;
		}
		for (s=buffer; s<p; ++s) {
			if (*s == '\t') {
				DBGU_puts (gpTabSeq+((*colp) & 07));
				*colp += 8 - ((*colp) & 07);
			} else {
				++(*colp);
				DBGU_PutChar (*s);
			}
		}
	} else {
		DBGU_puts (gpEraseSeq);
		(*colp)--;
	}
	(*np)--;
	return (p);
}

//------------------------------------------------------------------------------
///  Check console is ready?
///  \return 0 not ready, other value means ready and command string length
//------------------------------------------------------------------------------
unsigned int DBGU_CommandIsReady()
{
  unsigned char c;
  static char *p = gpConsoleBuffer;
  int     escflag = 0;
  char *  p_buf = gpConsoleBuffer;
  static int	n = 0;				// buffer index
  int	plen = 0;			// prompt length
  static int col=0;				// output column cnt
  unsigned int cmdLen;
  
  plen = strlen(GS_SHELL_PROMPT);
  
  //right beginning of a new command input
  if(col == 0)
    col = plen;
  
Tag_for_CombinedKey:
  
  if(DBGU_IsRxReady()) {
    c = DBGU_GetChar();
    
    //
    // Special character handling
    //
    switch (c) {
    case '\r':				// Enter
    case '\n':
      *p = '\0';
      cmdLen = p - p_buf;
      p = gpConsoleBuffer;
      n = 0; //clear buffer index as 0
      col = 0; // this is set for indicate next time input considered as new command
      gDBGUPromptFlag = 1;//DBGU_puts ("\r\n");
      return cmdLen;
      
    case '\0':				// nul
      return 0;
      
    case 0x03:				// ^C - break
      //p_buf[0] = '\0';	// discard input
      return 0;
      
    case 0x15:				// ^U - erase line
      while (col > plen) {
        DBGU_puts (gpEraseSeq);
        --col;
      }
      p = p_buf;
      n = 0;
      return 0;
      
    case 0x17:				// ^W - erase word
      p=DBGU_RemoveCharFromBuf(p_buf, p, &col, &n, plen);
      while ((n > 0) && (*p != ' ')) {
        p=DBGU_RemoveCharFromBuf(p_buf, p, &col, &n, plen);
      }
      return 0;
      
    case 0x08:				// ^H  - backspace
    case 0x7F:				// DEL - backspace
      p=DBGU_RemoveCharFromBuf(p_buf, p, &col, &n, plen);
      return 0;
      
    case 0x1b:                              // arrow, home flag
      escflag = 1;
      goto Tag_for_CombinedKey;//in window hyperterminal, arrow and home flag
                               // keys are combined 1b + x
      
    case 'D':                               //ignore arrow,home key
    case 'C':
    case 'H':
    case 'A':
    case 'B':
      if(escflag) {
        escflag = 0;
        return 0;
      }
      
      
    default:
      //
      // Must be a normal character then
      //
      if (n < DBGU_CMDBUFSIZE-2) {
        if (c == '\t') {	// expand TABs
          DBGU_puts (gpTabSeq+(col&07));
          col += 8 - (col&07);
        } else {
          ++col;		// echo input
          DBGU_PutChar (c);
        }
        *p++ = c;
        ++n;
      } else {			// Buffer full
        DBGU_PutChar ('\a');
      }
    }    
  }
  
  return 0;
}

//------------------------------------------------------------------------------
///  get a const pointer to global console input buffer
///  \return pointer to global DBGU console input buffer
//------------------------------------------------------------------------------
const char * DBGU_GetCommand() {
  return (const char *)gpConsoleBuffer;
}

//------------------------------------------------------------------------------
///  Show DBGU console prompt information
///  \return 0, success
//------------------------------------------------------------------------------
int DBGU_ShowPrompt()
{
  
#if defined(GS_SHELL_PROMPT)
  // print prompt
  if(gDBGUPromptFlag) {
  DBGU_puts("\n\r");
  DBGU_puts (GS_SHELL_PROMPT);
    gDBGUPromptFlag = 0;
  }
#endif
  
  return 0;    
}
personal git repositories of Harald Welte. Your mileage may vary