KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
shell.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : shell.c
11  * Created : 8 feb. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdbool.h>
18 
19 #include <util/macro.h>
20 #include <kernel/err.h>
21 
22 #include "cfg_shell.h"
23 #include <util/shell.h>
24 
25 
26 #define SHL_DEFAULT_PROMPT "~>"
27 
28 
29 static const char * _prompt = SHL_DEFAULT_PROMPT;
30 
31 typedef struct {
32  const char * name;
33  const char * help;
34  bool (*cmd_exec)(int argc, const char *args[]);
35 } ShlEntry;
36 
37 #define X(CMD) \
38  bool cmd_ ## CMD ## _exec (int argc, const char *args[]); \
39  extern const char cmd_ ## CMD ## _help[];
40 
42 
43 #undef X
44 
45 #define X(CMD) \
46  { #CMD, &(cmd_ ## CMD ## _help[0]), cmd_ ## CMD ## _exec },
47 
48 
49 static const ShlEntry _entries[] =
50 {
52  { 0, 0, 0 }
53 };
54 
55 #undef X
56 
57 
58 
59 
60 
61 const char cmd_help_help[] = "Shows this help";
62 
63 bool cmd_help_exec(int argc, const char *args[])
64 {
65  puts("Shell Help\n");
66  const ShlEntry * entry = _entries;
67  while (entry->help != 0)
68  {
69  printf(" %-10s - %s\n", entry->name, entry->help);
70  entry++;
71  }
72  puts("");
73  return true;
74 }
75 
76 #define _SHL_MAX_CMD_LEN 80
77 #define _SHL_MAX_TOKENS 8
78 
79 static char _shlCmdBuf[_SHL_MAX_CMD_LEN];
80 static int _shlCursor = 0;
81 static char * tokptr[_SHL_MAX_TOKENS];
82 static bool _first = true;
83 
84 
85 void shellSetPrompt(const char * newPrompt)
86 {
87  if (newPrompt == NULL) {
88  _prompt = SHL_DEFAULT_PROMPT;
89  } else {
90  _prompt = newPrompt;
91  }
92 }
93 
94 static void shellPrompt()
95 {
96  printf("\n%s", _prompt);
97 }
98 
99 
100 static void shellExecCommand()
101 {
102  if (_shlCursor > 1) {
103 
104  int n = 0, i = 0;
105 
106  while(n < _SHL_MAX_TOKENS)
107  {
108  while(_shlCmdBuf[i] == ' ') _shlCmdBuf[i++] = '\0';
109 
110  if(_shlCmdBuf[i] == '\n')
111  break;
112 
113  tokptr [n++] = &(_shlCmdBuf[i]);
114  while(_shlCmdBuf[i] != ' ' && _shlCmdBuf[i] != '\n') i++;
115 
116  if(_shlCmdBuf[i] == '\n')
117  break;
118 
119  }
120  _shlCmdBuf[i] = '\0';
121 
122  if(!n) {
123  _shlCursor = 0;
124  return;
125  }
126 
127  if(*tokptr[0] == '#')
128  {
129  _shlCursor = 0;
130  return;
131  }
132 
133  for(i=0; _entries[i].cmd_exec; i++)
134  {
135  if(!strcmp(_entries[i].name, tokptr[0]))
136  {
137  puts("\n");
138  if (!_entries[i].cmd_exec(n - 1, (const char **) ( tokptr + 1 ) ))
139  {
140  puts("Command failed");
141  } else {
142  puts("OK");
143  }
144  if (errHas()) {
145  errPrint(true);
146  }
147  _shlCursor = 0;
148  return;
149  }
150  }
151 
152  puts("\nUnknown command\n\n");
153  }
154  _shlCursor = 0;
155 }
156 
157 
158 
159 void shellDo()
160 {
161  if (_first) {
162  puts("Type 'help' for more information\n");
163  shellPrompt();
164  _first = false;
165  }
166  int c = _shellChar();
167 
168  while (c != EOF)
169  {
170 
171  if (c >= ' ' && c <= '~' && _shlCursor < (_SHL_MAX_CMD_LEN - 1) )
172  {
173  _shlCmdBuf[_shlCursor++] = c;
174  putchar(c);
175  }
176  else if (c == '\x7f' && _shlCursor > 0)
177  {
178  _shlCursor--;
179  printf("\033[1D\033[1P");
180  }
181  else if (c == '\n' || c == '\r')
182  {
183  _shlCmdBuf[_shlCursor] = '\n';
184  if (_shlCursor > 0) shellExecCommand();
185  shellPrompt();
186  _shlCursor = 0;
187 
188  }
189  c = _shellChar();
190  }
191 
192 }
193 
194 
bool errHas()
Returns whether there is an error pending.
Definition: err.c:52
The shell is a ASCII command interpreter and effort has been made to simplify creation of additional ...
int _shellChar()
Stub function.
Definition: sys.c:174
Configures the shell commands.
Manages the global system error.
#define SHELL_COMMANDS
List of shell commands.
Definition: cfg_shell.h:75
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
void shellSetPrompt(const char *newPrompt)
Sets the prompt for the shell.
Definition: shell.c:85
void shellDo()
Checks if there is any ASCII characters of the UART, and execute any commands issued.
Definition: shell.c:159
Provides common macros.
Definition: shell.c:31