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 22

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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