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 27

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

powered by: WebSVN 2.1.0

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