OpenCores
URL https://opencores.org/ocsvn/qaz_libs/qaz_libs/trunk

Subversion Repositories qaz_libs

[/] [qaz_libs/] [trunk/] [cli/] [cli/] [sys_cli.c] - Blame information for rev 31

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 qaztronic
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// Copyright (C) 2015 Authors and OPENCORES.ORG                 ////
4
////                                                              ////
5
//// This source file may be used and distributed without         ////
6
//// restriction provided that this copyright statement is not    ////
7
//// removed from the file and that any derivative work contains  ////
8
//// the original copyright notice and the associated disclaimer. ////
9
////                                                              ////
10
//// This source file is free software; you can redistribute it   ////
11
//// and/or modify it under the terms of the GNU Lesser General   ////
12
//// Public License as published by the Free Software Foundation; ////
13
//// either version 2.1 of the License, or (at your option) any   ////
14
//// later version.                                               ////
15
////                                                              ////
16
//// This source is distributed in the hope that it will be       ////
17
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
18
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
19
//// PURPOSE.  See the GNU Lesser General Public License for more ////
20
//// details.                                                     ////
21
////                                                              ////
22
//// You should have received a copy of the GNU Lesser General    ////
23
//// Public License along with this source; if not, download it   ////
24
//// from http://www.opencores.org/lgpl.shtml                     ////
25
////                                                              ////
26
//////////////////////////////////////////////////////////////////////
27 22 qaztronic
 
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <unistd.h>
32
 
33
#include "sys_cmd.h"
34
#include "sys_error.h"
35
 
36
 
37
 
38
/*-----------------------------------------------------------*/
39 28 qaztronic
void clear_screen(void)
40
{
41
  PRINTF_MACRO("\033[H\033[J");
42
}
43
 
44
 
45
/*-----------------------------------------------------------*/
46 22 qaztronic
#ifdef ANSI_ESCAPE_CODE
47
#define ASCII_ESC '\x1b'
48
 
49 28 qaztronic
static void send_csi(char c)
50 22 qaztronic
{
51 28 qaztronic
  putchar(ASCII_ESC);
52
  putchar('[');
53
  putchar(c);
54 22 qaztronic
}
55
#else
56 28 qaztronic
static void send_csi(char c) {};
57 22 qaztronic
#endif
58
 
59
 
60
/*-----------------------------------------------------------*/
61 28 qaztronic
static char *cli_edit_buffer(char *in_buffer, char *out_buffer, unsigned int line_length)
62 22 qaztronic
{
63
  static char *out_ptr;
64
  static char *begin_ptr;
65
  static char *end_ptr;
66
 
67
#ifdef ANSI_ESCAPE_CODE
68
  static char prev_char;
69
  static unsigned int csi;
70
#endif
71
 
72 31 qaztronic
#ifdef CLI_ECHO_UART
73
  fwrite(in_buffer,1,line_length,stdout);
74
#endif
75
 
76 22 qaztronic
  unsigned int i;
77
 
78 28 qaztronic
  if(out_buffer != NULL)
79 22 qaztronic
  {
80
    out_ptr   = out_buffer;
81
    begin_ptr = out_buffer;
82
    end_ptr   = out_buffer + INPUT_LINE_LENGTH;
83
 
84
#ifdef ANSI_ESCAPE_CODE
85
    prev_char = 0;
86
    csi = 0;
87
#endif
88
  }
89
 
90 28 qaztronic
  for(i = 0 ; i < line_length ; i++)
91 22 qaztronic
  {
92
 
93 28 qaztronic
    if(out_ptr >= end_ptr)
94 22 qaztronic
    {
95
      *end_ptr = '\0';
96 28 qaztronic
      return(NULL);
97 22 qaztronic
    }
98
 
99 28 qaztronic
    if(out_ptr < begin_ptr)
100
      sys_error_fatal(FATAL_ERROR_CLI);
101 22 qaztronic
 
102 28 qaztronic
    switch(in_buffer[i])
103 22 qaztronic
    {
104
      case '\0':
105 28 qaztronic
        return(NULL);
106 22 qaztronic
        break;
107
 
108
      case '\n':
109
        *out_ptr = '\0';
110 28 qaztronic
        return(NULL);
111 22 qaztronic
        break;
112
 
113
      case '\r':
114
        *out_ptr = '\0';
115 28 qaztronic
        return(NULL);
116 22 qaztronic
        break;
117
 
118
      case '\b':
119 28 qaztronic
        if(out_ptr != begin_ptr)
120 22 qaztronic
        {
121 28 qaztronic
          send_csi('P');
122 22 qaztronic
          out_ptr--;
123
        } else
124
        {
125 28 qaztronic
          putchar(' ');
126
          send_csi('\a');
127 22 qaztronic
        }
128
        break;
129
 
130
#ifdef ANSI_ESCAPE_CODE
131
      case ASCII_ESC:
132
        break;
133
 
134
      case '[':
135 28 qaztronic
        if(prev_char == ASCII_ESC)
136 22 qaztronic
        {
137
          csi = 1;
138
        } else
139
        {
140
          *out_ptr = in_buffer[i];
141
          out_ptr++;
142
        }
143
        break;
144
 
145
      case 'A':
146 28 qaztronic
        if(csi)
147 22 qaztronic
        {
148 28 qaztronic
          send_csi('B');
149
          send_csi('\a');
150 22 qaztronic
 
151
          csi = 0;
152
        } else
153
        {
154
          *out_ptr = in_buffer[i];
155
          out_ptr++;
156
        }
157
        break;
158
 
159
      case 'B':
160 28 qaztronic
        if(csi == 0)
161 22 qaztronic
        {
162
          *out_ptr = in_buffer[i];
163
          out_ptr++;
164
        }
165
        break;
166
 
167
      case 'C':
168 28 qaztronic
        if(csi)
169 22 qaztronic
        {
170 28 qaztronic
          send_csi('D');
171
          send_csi('\a');
172 22 qaztronic
 
173
          csi = 0;
174
        } else
175
        {
176
          *out_ptr = in_buffer[i];
177
          out_ptr++;
178
        }
179
        break;
180
 
181
      case 'D':
182 28 qaztronic
        if(csi)
183 22 qaztronic
        {
184 28 qaztronic
          send_csi('C');
185
          send_csi('\a');
186 22 qaztronic
 
187
          csi = 0;
188
        } else
189
        {
190
          *out_ptr = in_buffer[i];
191
          out_ptr++;
192
        }
193
        break;
194
#endif
195
 
196
      default:
197
        *out_ptr = in_buffer[i];
198
        out_ptr++;
199
        break;
200
    }
201
 
202
#ifdef ANSI_ESCAPE_CODE
203
    prev_char = in_buffer[i];
204
#endif
205
  }
206
 
207 28 qaztronic
  return(out_ptr);
208 22 qaztronic
}
209
 
210
 
211
/*-----------------------------------------------------------*/
212 28 qaztronic
void sys_cli_task(void)
213 22 qaztronic
{
214
  char last_return_value = EXIT_SUCCESS;
215
  char in_buffer[INPUT_LINE_LENGTH + 1];
216
  char out_buffer[INPUT_LINE_LENGTH + 1];
217
  char *cli_ptr;
218
  struct cli_cmd_tab_t cmd_to_check = { "", NULL, NULL };
219
  unsigned char cli_argc;
220
  char *cli_argv[MAX_CLI_ARGC];
221
  struct cli_cmd_tab_t *cli_cmd;
222
  unsigned int bytes_read;
223
 
224
  cli_init();
225
 
226 28 qaztronic
  for(;;)
227 22 qaztronic
  {
228 29 qaztronic
    PRINTF_MACRO("\r\n# > ");
229 22 qaztronic
 
230
    cli_argc = 0;
231
    last_return_value = EXIT_SUCCESS;
232
 
233 28 qaztronic
    bytes_read = (unsigned int)read(STDIN_FILENO, (void *)in_buffer, sizeof(in_buffer));
234
    cli_ptr = cli_edit_buffer(in_buffer, out_buffer, bytes_read);
235 22 qaztronic
 
236 28 qaztronic
    while(cli_ptr != NULL)
237 22 qaztronic
    {
238 28 qaztronic
      bytes_read = (unsigned int)read(STDIN_FILENO, (void *)in_buffer, sizeof(in_buffer));
239
      cli_ptr = cli_edit_buffer(in_buffer, NULL, bytes_read);
240 22 qaztronic
    }
241
 
242 28 qaztronic
    if(out_buffer[0] == '\0')
243 22 qaztronic
      last_return_value = EXIT_FAILURE;
244
 
245 28 qaztronic
    while(last_return_value != EXIT_FAILURE)
246 22 qaztronic
    {
247 28 qaztronic
      cli_ptr = strtok(out_buffer, " \t");
248 22 qaztronic
 
249 28 qaztronic
      strncpy(cmd_to_check.cmd, out_buffer, MAX_CMD_LENGTH);
250
      cli_cmd = cli_find_command(&cmd_to_check);
251 22 qaztronic
 
252 28 qaztronic
      if (cli_cmd == NULL)
253 22 qaztronic
      {
254 29 qaztronic
        PRINTF_MACRO("\r\n! > Command not found!!!");
255 22 qaztronic
        last_return_value = EXIT_FAILURE;
256
        break;
257
      }
258
 
259 28 qaztronic
      if(cli_ptr == NULL)
260 22 qaztronic
      {
261
        cli_argv[cli_argc] = out_buffer;
262
        cli_argc++;
263
      } else
264
      {
265 28 qaztronic
        while(cli_ptr != NULL)
266 22 qaztronic
        {
267
          cli_argv[cli_argc] = cli_ptr;
268
          cli_argc++;
269
 
270 28 qaztronic
          cli_ptr = strtok(NULL, " \t");
271 22 qaztronic
        }
272
      }
273
 
274 28 qaztronic
      last_return_value = cli_cmd->func(cli_argc, (const char **)cli_argv);
275 22 qaztronic
      break;
276
    }
277
 
278
  }
279
}
280
 

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.