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 29

Go to most recent revision | 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
  unsigned int i;
73
 
74 28 qaztronic
  if(out_buffer != NULL)
75 22 qaztronic
  {
76
    out_ptr   = out_buffer;
77
    begin_ptr = out_buffer;
78
    end_ptr   = out_buffer + INPUT_LINE_LENGTH;
79
 
80
#ifdef ANSI_ESCAPE_CODE
81
    prev_char = 0;
82
    csi = 0;
83
#endif
84
  }
85
 
86 28 qaztronic
  for(i = 0 ; i < line_length ; i++)
87 22 qaztronic
  {
88
 
89 28 qaztronic
    if(out_ptr >= end_ptr)
90 22 qaztronic
    {
91
      *end_ptr = '\0';
92 28 qaztronic
      return(NULL);
93 22 qaztronic
    }
94
 
95 28 qaztronic
    if(out_ptr < begin_ptr)
96
      sys_error_fatal(FATAL_ERROR_CLI);
97 22 qaztronic
 
98 28 qaztronic
    switch(in_buffer[i])
99 22 qaztronic
    {
100
      case '\0':
101 28 qaztronic
        return(NULL);
102 22 qaztronic
        break;
103
 
104
      case '\n':
105
        *out_ptr = '\0';
106 28 qaztronic
        return(NULL);
107 22 qaztronic
        break;
108
 
109
      case '\r':
110
        *out_ptr = '\0';
111 28 qaztronic
        return(NULL);
112 22 qaztronic
        break;
113
 
114
      case '\b':
115 28 qaztronic
        if(out_ptr != begin_ptr)
116 22 qaztronic
        {
117 28 qaztronic
          send_csi('P');
118 22 qaztronic
          out_ptr--;
119
        } else
120
        {
121 28 qaztronic
          putchar(' ');
122
          send_csi('\a');
123 22 qaztronic
        }
124
        break;
125
 
126
#ifdef ANSI_ESCAPE_CODE
127
      case ASCII_ESC:
128
        break;
129
 
130
      case '[':
131 28 qaztronic
        if(prev_char == ASCII_ESC)
132 22 qaztronic
        {
133
          csi = 1;
134
        } else
135
        {
136
          *out_ptr = in_buffer[i];
137
          out_ptr++;
138
        }
139
        break;
140
 
141
      case 'A':
142 28 qaztronic
        if(csi)
143 22 qaztronic
        {
144 28 qaztronic
          send_csi('B');
145
          send_csi('\a');
146 22 qaztronic
 
147
          csi = 0;
148
        } else
149
        {
150
          *out_ptr = in_buffer[i];
151
          out_ptr++;
152
        }
153
        break;
154
 
155
      case 'B':
156 28 qaztronic
        if(csi == 0)
157 22 qaztronic
        {
158
          *out_ptr = in_buffer[i];
159
          out_ptr++;
160
        }
161
        break;
162
 
163
      case 'C':
164 28 qaztronic
        if(csi)
165 22 qaztronic
        {
166 28 qaztronic
          send_csi('D');
167
          send_csi('\a');
168 22 qaztronic
 
169
          csi = 0;
170
        } else
171
        {
172
          *out_ptr = in_buffer[i];
173
          out_ptr++;
174
        }
175
        break;
176
 
177
      case 'D':
178 28 qaztronic
        if(csi)
179 22 qaztronic
        {
180 28 qaztronic
          send_csi('C');
181
          send_csi('\a');
182 22 qaztronic
 
183
          csi = 0;
184
        } else
185
        {
186
          *out_ptr = in_buffer[i];
187
          out_ptr++;
188
        }
189
        break;
190
#endif
191
 
192
      default:
193
        *out_ptr = in_buffer[i];
194
        out_ptr++;
195
        break;
196
    }
197
 
198
#ifdef ANSI_ESCAPE_CODE
199
    prev_char = in_buffer[i];
200
#endif
201
  }
202
 
203 28 qaztronic
  return(out_ptr);
204 22 qaztronic
}
205
 
206
 
207
/*-----------------------------------------------------------*/
208 28 qaztronic
void sys_cli_task(void)
209 22 qaztronic
{
210
  char last_return_value = EXIT_SUCCESS;
211
  char in_buffer[INPUT_LINE_LENGTH + 1];
212
  char out_buffer[INPUT_LINE_LENGTH + 1];
213
  char *cli_ptr;
214
  struct cli_cmd_tab_t cmd_to_check = { "", NULL, NULL };
215
  unsigned char cli_argc;
216
  char *cli_argv[MAX_CLI_ARGC];
217
  struct cli_cmd_tab_t *cli_cmd;
218
  unsigned int bytes_read;
219
 
220
  cli_init();
221
 
222 29 qaztronic
  // PRINTF_MACRO("\r\n");
223 22 qaztronic
 
224 28 qaztronic
  for(;;)
225 22 qaztronic
  {
226 29 qaztronic
    // PRINTF_MACRO("%d > ", last_return_value);
227
    PRINTF_MACRO("\r\n# > ");
228 22 qaztronic
 
229
    cli_argc = 0;
230
    last_return_value = EXIT_SUCCESS;
231
 
232 28 qaztronic
    bytes_read = (unsigned int)read(STDIN_FILENO, (void *)in_buffer, sizeof(in_buffer));
233
    cli_ptr = cli_edit_buffer(in_buffer, out_buffer, bytes_read);
234 22 qaztronic
 
235 28 qaztronic
    while(cli_ptr != NULL)
236 22 qaztronic
    {
237 28 qaztronic
      bytes_read = (unsigned int)read(STDIN_FILENO, (void *)in_buffer, sizeof(in_buffer));
238
      cli_ptr = cli_edit_buffer(in_buffer, NULL, bytes_read);
239 22 qaztronic
    }
240
 
241 28 qaztronic
    if(out_buffer[0] == '\0')
242 22 qaztronic
      last_return_value = EXIT_FAILURE;
243
 
244 28 qaztronic
    while(last_return_value != EXIT_FAILURE)
245 22 qaztronic
    {
246 28 qaztronic
      cli_ptr = strtok(out_buffer, " \t");
247 22 qaztronic
 
248 28 qaztronic
      strncpy(cmd_to_check.cmd, out_buffer, MAX_CMD_LENGTH);
249
      cli_cmd = cli_find_command(&cmd_to_check);
250 22 qaztronic
 
251 28 qaztronic
      if (cli_cmd == NULL)
252 22 qaztronic
      {
253 29 qaztronic
        PRINTF_MACRO("\r\n! > Command not found!!!");
254 22 qaztronic
        last_return_value = EXIT_FAILURE;
255
        break;
256
      }
257
 
258 28 qaztronic
      if(cli_ptr == NULL)
259 22 qaztronic
      {
260
        cli_argv[cli_argc] = out_buffer;
261
        cli_argc++;
262
      } else
263
      {
264 28 qaztronic
        while(cli_ptr != NULL)
265 22 qaztronic
        {
266
          cli_argv[cli_argc] = cli_ptr;
267
          cli_argc++;
268
 
269 28 qaztronic
          cli_ptr = strtok(NULL, " \t");
270 22 qaztronic
        }
271
      }
272
 
273 29 qaztronic
      // PRINTF_MACRO("\r\n");
274 22 qaztronic
 
275 28 qaztronic
      last_return_value = cli_cmd->func(cli_argc, (const char **)cli_argv);
276 22 qaztronic
      break;
277
    }
278
 
279
  }
280
}
281
 

powered by: WebSVN 2.1.0

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