1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Use argc and argv in shell handlers

Compare #708.

Now the tokenization of an input line is done by the shell itself. You
may quote arguments with `"..."`. Empty arguments, supplied by `""` are
preserved. Spaces in between arguments are squasheds; spaces inside
quotes are preserved.

You cannot partially quote an argument. You must not use
- `cmd "abc`,
- `cmd abc"def"`, or
- `cmd "abc"def`.
This commit is contained in:
René Kijewski 2014-02-21 19:10:24 +01:00
parent 95e7df7f49
commit c507632e50
15 changed files with 282 additions and 244 deletions

View File

@ -9,6 +9,6 @@
#define __PS_H
void thread_print_all(void);
void _ps_handler(char *);
void _ps_handler(int argc, char **argv);
#endif /* __PS_H */

View File

@ -22,10 +22,12 @@
#define __SHELL_H
#include <stdint.h>
typedef void (*shell_command_handler_t)(int argc, char **argv);
typedef struct shell_command_t {
char *name;
char *desc;
void (*handler)(char *);
shell_command_handler_t handler;
} shell_command_t;
typedef struct shell_t {

View File

@ -23,13 +23,11 @@
#include "cc110x.h"
void _cc110x_get_set_address_handler(char *addr)
void _cc110x_get_set_address_handler(int argc, char **argv)
{
int16_t a;
if (argc > 1) {
int16_t a = atoi(argv[1]);
a = atoi(addr + 5);
if (strlen(addr) > 5) {
printf("[cc110x] Setting address %i ... ", a);
cc1100_set_address((radio_address_t)a);
@ -45,13 +43,11 @@ void _cc110x_get_set_address_handler(char *addr)
}
}
void _cc110x_get_set_channel_handler(char *addr)
void _cc110x_get_set_channel_handler(int argc, char **argv)
{
int16_t a;
if (argc > 1) {
int16_t a = atoi(argv[1]);
a = atoi(addr + 5);
if (strlen(addr) > 5) {
printf("[cc110x] Setting channel %i...", a);
cc1100_set_channel(a);

View File

@ -44,10 +44,12 @@ static inline uint8_t sector_read(unsigned char *read_buf, unsigned long sector,
return 0;
}
void _get_sectorsize(char *unused)
void _get_sectorsize(int argc, char **argv)
{
unsigned short ssize;
(void) argc;
(void) argv;
unsigned short ssize;
if (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK) {
printf("[disk] sector size is %u\n", ssize);
}
@ -56,10 +58,12 @@ void _get_sectorsize(char *unused)
}
}
void _get_blocksize(char *unused)
void _get_blocksize(int argc, char **argv)
{
unsigned long bsize;
(void) argc;
(void) argv;
unsigned long bsize;
if (MCI_ioctl(GET_BLOCK_SIZE, &bsize) == RES_OK) {
printf("[disk] block size is %lu\n", bsize);
}
@ -68,10 +72,12 @@ void _get_blocksize(char *unused)
}
}
void _get_sectorcount(char *unused)
void _get_sectorcount(int argc, char **argv)
{
unsigned long scount;
(void) argc;
(void) argv;
unsigned long scount;
if (MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) {
printf("[disk] sector count is %lu\n", scount);
}
@ -80,14 +86,13 @@ void _get_sectorcount(char *unused)
}
}
void _read_sector(char *sector)
void _read_sector(int argc, char **argv)
{
unsigned long sectornr, scount;
unsigned short ssize;
if (strlen(sector) > strlen(DISK_READ_SECTOR_CMD) + 1) {
sectornr = atol(sector + strlen(DISK_READ_SECTOR_CMD) + 1);
if (argc == 2) {
sectornr = atol(argv[1]);
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
unsigned char read_buf[ssize];
@ -100,65 +105,57 @@ void _read_sector(char *sector)
printf("[disk] Error while reading sector %lu\n", sectornr);
}
else {
printf("[disk] Usage:\n%s <SECTOR>\n", DISK_READ_SECTOR_CMD);
printf("[disk] Usage:\n%s <SECTOR>\n", argv[0]);
return;
}
}
void _read_bytes(char *bytes)
void _read_bytes(int argc, char **argv)
{
unsigned long sector = 1, scount, offset;
unsigned short ssize, length;
char *tok;
/* tokenize user input */
tok = strtok(bytes + strlen(DISK_READ_BYTES_CMD) + 1, " ");
if (argc != 3) {
printf("[disk] Usage:\n%s <OFFSET> <LENGTH>\n", argv[0]);
return;
}
if (tok) {
offset = atol(tok);
tok = strtok(NULL, " ");
offset = atol(argv[1]);
length = atoi(argv[2]);
if (tok) {
length = atoi(tok);
/* get card info */
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
/* calculate sector and offset position */
sector = (offset / ssize) + 1;
offset = (offset % ssize);
/* preapre buffer (size must be a multiple of sector size) */
unsigned char read_buf[((length / ssize) + 1) * 512];
if (length) {
/* get card info */
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
/* calculate sector and offset position */
sector = (offset / ssize) + 1;
offset = (offset % ssize);
/* preapre buffer (size must be a multiple of sector size) */
unsigned char read_buf[((length / ssize) + 1) * 512];
/* read from several sectors */
if (length > (ssize - offset)) {
/* buffer offset */
unsigned long j = 0;
/* chunk from current sector */
unsigned short tmp = ssize - offset;
/* read from several sectors */
if (length > (ssize - offset)) {
/* buffer offset */
unsigned long j = 0;
/* chunk from current sector */
unsigned short tmp = ssize - offset;
while (length) {
sector_read(read_buf + j, sector++, tmp, offset);
/* decrease length and recalculate chunk */
length -= tmp;
tmp = (length >= ssize) ? ssize : length;
}
while (length) {
sector_read(read_buf + j, sector++, tmp, offset);
/* decrease length and recalculate chunk */
length -= tmp;
tmp = (length >= ssize) ? ssize : length;
}
return;
} /* length > (ssize - offset) */
/* read only one sector */
else {
if (sector_read(read_buf, sector, length, offset)) {
return;
}
} /* length < (ssize - offset) */
} /* ioctl */
printf("[disk] Error while reading sector %lu\n", sector);
return;
} /* length > (ssize - offset) */
/* read only one sector */
else {
if (sector_read(read_buf, sector, length, offset)) {
return;
} /* length */
} /* strtok #2 */
} /* strtok #1 */
}
} /* length < (ssize - offset) */
} /* ioctl */
printf("[disk] Error while reading sector %lu\n", sector);
return;
printf("[disk] Usage:\n%s <OFFSET> <LENGTH>\n", DISK_READ_BYTES_CMD);
}

View File

@ -20,8 +20,10 @@
extern void heap_stats(void);
void _heap_handler(char *unused)
void _heap_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
heap_stats();
}

View File

@ -20,16 +20,13 @@
#include <stdlib.h>
#include "config.h"
void _id_handler(char *id)
void _id_handler(int argc, char **argv)
{
long newid;
newid = atoi(id + 3);
if (strlen(id) < 3) {
if (argc < 2) {
printf("Current id: %u\n", sysconfig.id);
}
else {
long newid = atoi(argv[1]);
printf("Setting new id %lu\n", newid);
sysconfig.id = newid;

View File

@ -18,16 +18,19 @@
#include <stdio.h>
#include "ltc4150.h"
void _get_current_handler(char *unused)
void _get_current_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
printf("Power usage: %.4f mA (%.4f mA avg/ %.4f mAh total / %i usec)\n", ltc4150_get_current_mA(), ltc4150_get_avg_mA(), ltc4150_get_total_mAh(), ltc4150_get_interval());
printf("Power usage: %.4f mA (%.4f mA avg/ %.4f mAh total / %i usec)\n",
ltc4150_get_current_mA(), ltc4150_get_avg_mA(), ltc4150_get_total_mAh(), ltc4150_get_interval());
}
void _reset_current_handler(char *unused)
void _reset_current_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
ltc4150_start();
}

View File

@ -23,27 +23,26 @@
#include "hwtimer.h"
#include "random.h"
void _mersenne_init(char *str)
void _mersenne_init(int argc, char **argv)
{
int initval;
char *toc_str = strtok(str, " ");
toc_str = strtok(NULL, " ");
if (!toc_str) {
if (argc == 1) {
initval = hwtimer_now();
printf("PRNG inizialized to current time: %d\n", initval);
printf("PRNG initialized to current time: %d\n", initval);
}
else {
initval = atoi(toc_str);
printf("PRNG inizialized given value: %d\n", initval);
initval = atoi(argv[1]);
printf("PRNG initialized given value: %d\n", initval);
}
genrand_init(initval);
}
void _mersenne_get(char *str)
void _mersenne_get(int argc, char **argv)
{
(void) str;
(void) argc;
(void) argv;
printf("%" PRIu32 "\n", genrand_uint32());
}

View File

@ -17,9 +17,10 @@
#include "ps.h"
void _ps_handler(char *unused)
void _ps_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
thread_print_all();
}

View File

@ -22,7 +22,7 @@
#ifdef MODULE_RTC
#include "rtc.h"
void _gettime_handler(void)
static void _gettime_handler(void)
{
struct tm now;
rtc_get_localtime(&now);
@ -30,19 +30,19 @@ void _gettime_handler(void)
printf("%s", asctime(&now));
}
void _settime_handler(char *c)
static void _settime_handler(char *c)
{
struct tm now;
int res;
uint16_t month, epoch_year;
res = sscanf(c, "date %hu-%hu-%u %u:%u:%u",
res = sscanf(c, "%hu-%hu-%u %u:%u:%u",
&epoch_year,
&month,
(unsigned int *) & (now.tm_mday),
(unsigned int *) & (now.tm_hour),
(unsigned int *) & (now.tm_min),
(unsigned int *) & (now.tm_sec));
(unsigned int *) &(now.tm_mday),
(unsigned int *) &(now.tm_hour),
(unsigned int *) &(now.tm_min),
(unsigned int *) &(now.tm_sec));
if (res < 6) {
printf("Usage: date YYYY-MM-DD hh:mm:ss\n");
@ -57,13 +57,13 @@ void _settime_handler(char *c)
rtc_set_localtime(&now);
}
void _date_handler(char *c)
void _date_handler(int argc, char **argv)
{
if (strlen(c) == 4) {
if (argc == 1) {
_gettime_handler();
}
else {
_settime_handler(c);
_settime_handler(argv[1]);
}
}

View File

@ -25,8 +25,11 @@
extern float sht11_temperature_offset;
void _get_humidity_handler(char *unused)
void _get_humidity_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, HUMIDITY | TEMPERATURE);
@ -39,8 +42,11 @@ void _get_humidity_handler(char *unused)
(double) sht11_val.relhum, (double) sht11_val.relhum_temp);
}
}
void _get_temperature_handler(char *unused)
void _get_temperature_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, TEMPERATURE);
@ -52,8 +58,11 @@ void _get_temperature_handler(char *unused)
printf("Temperature: %-6.2f°C\n", (double) sht11_val.temperature);
}
}
void _get_weather_handler(char *unused)
void _get_weather_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, HUMIDITY | TEMPERATURE);
@ -68,13 +77,13 @@ void _get_weather_handler(char *unused)
}
}
void _set_offset_handler(char *offset)
void _set_offset_handler(int argc, char **argv)
{
if (strlen(offset) == 6) {
puts("Usage: offset <OFFSET>");
if (argc != 2) {
printf("Usage: %s <OFFSET>\n", argv[0]);
}
else {
sht11_temperature_offset = atoi(offset + 7);
sht11_temperature_offset = atoi(argv[1]);
printf("Temperature offset set to %f\n", (double) sht11_temperature_offset);
}
}

View File

@ -17,8 +17,10 @@
#include "kernel.h"
void _reboot_handler(char *unused)
void _reboot_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
reboot();
}

View File

@ -52,7 +52,7 @@
/* checked for type safety */
void _transceiver_get_set_address_handler(char *addr)
void _transceiver_get_set_address_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
@ -67,8 +67,8 @@ void _transceiver_get_set_address_handler(char *addr)
tcmd.data = &a;
mesg.content.ptr = (char *) &tcmd;
if (strlen(addr) > 5) {
a = atoi(addr + 5);
if (argc > 1) {
a = atoi(argv[1]);
printf("[transceiver] trying to set address %" PRIu16 "\n", a);
mesg.type = SET_ADDRESS;
}
@ -81,7 +81,7 @@ void _transceiver_get_set_address_handler(char *addr)
}
/* checked for type safety */
void _transceiver_get_set_channel_handler(char *chan)
void _transceiver_get_set_channel_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
@ -96,8 +96,8 @@ void _transceiver_get_set_channel_handler(char *chan)
tcmd.data = &c;
mesg.content.ptr = (char *) &tcmd;
if (strlen(chan) > 5) {
c = atoi(chan + 5);
if (argc > 1) {
c = atoi(argv[1]);
printf("[transceiver] Trying to set channel %" PRIi32 "\n", c);
mesg.type = SET_CHANNEL;
}
@ -114,93 +114,85 @@ void _transceiver_get_set_channel_handler(char *chan)
}
}
void _transceiver_send_handler(char *pkt)
void _transceiver_send_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
char text_msg[TEXT_SIZE];
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
if (argc != 3) {
printf("Usage:\t%s <ADDR> <MSG>\n", argv[0]);
return;
}
radio_packet_t p;
int8_t response;
radio_address_t addr;
char *tok;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &p;
tok = strtok(pkt + 7, " ");
char text_msg[TEXT_SIZE];
memset(text_msg, 0, TEXT_SIZE);
strcpy(text_msg, argv[2]);
if (tok) {
addr = atoi(tok);
tok = strtok(NULL, " ");
p.data = (uint8_t *) text_msg;
p.length = strlen(text_msg) + 1;
p.dst = atoi(argv[1]);
if (tok) {
memset(text_msg, 0, TEXT_SIZE);
memcpy(text_msg, tok, strlen(tok));
p.data = (uint8_t *) text_msg;
p.length = strlen(text_msg) + 1;
p.dst = addr;
mesg.type = SND_PKT;
mesg.content.ptr = (char *)&tcmd;
printf("[transceiver] Sending packet of length %" PRIu16 " to %" PRIu16 ": %s\n", p.length, p.dst, (char*) p.data);
msg_send_receive(&mesg, &mesg, transceiver_pid);
response = mesg.content.value;
printf("[transceiver] Packet sent: %" PRIi8 "\n", response);
return;
}
}
puts("Usage:\ttxtsnd <ADDR> <MSG>");
}
/* checked for type safety */
void _transceiver_monitor_handler(char *mode)
{
msg_t mesg;
transceiver_command_t tcmd;
uint8_t m;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
tcmd.transceivers = _TC_TYPE;
tcmd.data = &m;
mesg.type = SND_PKT;
mesg.content.ptr = (char *) &tcmd;
if (strlen(mode) > 8) {
m = atoi(mode + 8);
printf("Setting monitor mode: %" PRIu8 "\n", m);
mesg.type = SET_MONITOR;
msg_send(&mesg, transceiver_pid, 1);
}
else {
puts("Usage:\nmonitor <MODE>");
}
printf("[transceiver] Sending packet of length %" PRIu16 " to %" PRIu16 ": %s\n", p.length, p.dst, (char*) p.data);
msg_send_receive(&mesg, &mesg, transceiver_pid);
int8_t response = mesg.content.value;
printf("[transceiver] Packet sent: %" PRIi8 "\n", response);
}
/* checked for type safety */
void _transceiver_get_set_pan_handler(char *pan) {
transceiver_command_t tcmd;
msg_t mesg;
int32_t p;
void _transceiver_monitor_handler(int argc, char **argv)
{
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
else if (argc != 2) {
printf("Usage:\n%s <MODE>\n", argv[0]);
return;
}
uint8_t m = atoi(argv[1]);
printf("Setting monitor mode: %" PRIu8 "\n", m);
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &m;
msg_t mesg;
mesg.content.ptr = (char *) &tcmd;
mesg.type = SET_MONITOR;
msg_send(&mesg, transceiver_pid, 1);
}
/* checked for type safety */
void _transceiver_get_set_pan_handler(int argc, char **argv)
{
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
int32_t p;
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &p;
msg_t mesg;
mesg.content.ptr = (char*) &tcmd;
if (strlen(pan) > 4) {
p = atoi(pan+4);
if (argc > 1) {
p = atoi(argv[1]);
printf("[transceiver] Trying to set pan %" PRIi32 "\n", p);
mesg.type = SET_PAN;
}
@ -218,37 +210,37 @@ void _transceiver_get_set_pan_handler(char *pan) {
/* checked for type safety */
#ifdef DBG_IGNORE
void _transceiver_set_ignore_handler(char *addr)
void _transceiver_set_ignore_handler(int argc, char **argv)
{
transceiver_command_t tcmd;
msg_t mesg;
radio_address_t a;
int16_t response;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
else if (argc != 2) {
printf("Usage:\n%s <address>\n", argv[1]);
}
radio_address_t a;
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &a;
msg_t mesg;
mesg.content.ptr = (char*) &tcmd;
if (strlen(addr) > 4) {
a = atoi(addr + 4);
printf("[transceiver] trying to add address %" PRIu16 " to the ignore list \n", a);
mesg.type = DBG_IGN;
msg_send_receive(&mesg, &mesg, transceiver_pid);
response = a;
if (response == -1) {
printf("Error: ignore list full\n");
}
else {
printf("Success (added at index %" PRIi16 ").\n", response);
}
a = atoi(addr + 4);
printf("[transceiver] trying to add address %" PRIu16 " to the ignore list \n", a);
mesg.type = DBG_IGN;
msg_send_receive(&mesg, &mesg, transceiver_pid);
int16_t response = a;
if (response == -1) {
printf("Error: ignore list full\n");
}
else {
puts("Usage:\nign <address>");
printf("Success (added at index %" PRIi16 ").\n", response);
}
}
#endif

View File

@ -23,34 +23,34 @@
#include <stdlib.h>
#include "shell_commands.h"
extern void _reboot_handler(char *unused);
extern void _reboot_handler(int argc, char **argv);
#ifdef MODULE_CONFIG
extern void _id_handler(char *id);
extern void _id_handler(int argc, char **argv);
#endif
#ifdef MODULE_LPC_COMMON
extern void _heap_handler(char *unused);
extern void _heap_handler(int argc, char **argv);
#endif
#ifdef MODULE_PS
extern void _ps_handler(char *unused);
extern void _ps_handler(int argc, char **argv);
#endif
#ifdef MODULE_RTC
extern void _date_handler(char *now);
extern void _date_handler(int argc, char **argv);
#endif
#ifdef MODULE_SHT11
extern void _get_temperature_handler(char *unused);
extern void _get_humidity_handler(char *unused);
extern void _get_weather_handler(char *unused);
extern void _set_offset_handler(char *offset);
extern void _get_temperature_handler(int argc, char **argv);
extern void _get_humidity_handler(int argc, char **argv);
extern void _get_weather_handler(int argc, char **argv);
extern void _set_offset_handler(int argc, char **argv);
#endif
#ifdef MODULE_LTC4150
extern void _get_current_handler(char *unused);
extern void _reset_current_handler(char *unused);
extern void _get_current_handler(int argc, char **argv);
extern void _reset_current_handler(int argc, char **argv);
#endif
@ -70,43 +70,43 @@ extern void _reset_current_handler(char *unused);
#endif
#else /* WITHOUT MODULE_TRANSCEIVER */
#ifdef MODULE_CC110X
extern void _cc110x_get_set_address_handler(char *addr);
extern void _cc110x_get_set_channel_handler(char *addr);
extern void _cc110x_get_set_address_handler(int argc, char **argv);
extern void _cc110x_get_set_channel_handler(int argc, char **argv);
#endif
#endif
#ifdef MODULE_TRANSCEIVER
#ifdef _TC_ADDR
extern void _transceiver_get_set_address_handler(char *addr);
extern void _transceiver_get_set_address_handler(int argc, char **argv);
#endif
#ifdef _TC_CHAN
extern void _transceiver_get_set_channel_handler(char *chan);
extern void _transceiver_get_set_channel_handler(int argc, char **argv);
#endif
#ifdef _TC_SEND
extern void _transceiver_send_handler(char *pkt);
extern void _transceiver_send_handler(int argc, char **argv);
#endif
#ifdef _TC_MON
extern void _transceiver_monitor_handler(char *mode);
extern void _transceiver_monitor_handler(int argc, char **argv);
#endif
#ifdef _TC_PAN
extern void _transceiver_get_set_pan_handler(char *chan);
extern void _transceiver_get_set_pan_handler(int argc, char **argv);
#endif
#ifdef _TC_IGN
extern void _transceiver_set_ignore_handler(char *addr);
extern void _transceiver_set_ignore_handler(int argc, char **argv);
#endif
#endif
#ifdef MODULE_MCI
extern void _get_sectorsize(char *unused);
extern void _get_blocksize(char *unused);
extern void _get_sectorcount(char *unused);
extern void _read_sector(char *sector);
extern void _read_bytes(char *bytes);
extern void _get_sectorsize(int argc, char **argv);
extern void _get_blocksize(int argc, char **argv);
extern void _get_sectorcount(int argc, char **argv);
extern void _read_sector(int argc, char **argv);
extern void _read_bytes(int argc, char **argv);
#endif
#ifdef MODULE_RANDOM
extern void _mersenne_init(char *str);
extern void _mersenne_get(char *str);
extern void _mersenne_init(int argc, char **argv);
extern void _mersenne_get(int argc, char **argv);
#endif
const shell_command_t _shell_command_list[] = {
@ -134,7 +134,6 @@ const shell_command_t _shell_command_list[] = {
{"rstcur", "Resets coulomb counter.", _reset_current_handler},
#endif
#ifdef MODULE_TRANSCEIVER
#ifdef _TC_ADDR
{"addr", "Gets or sets the address for the transceiver", _transceiver_get_set_address_handler},
@ -161,7 +160,6 @@ const shell_command_t _shell_command_list[] = {
#endif
#endif
#ifdef MODULE_MCI
{DISK_READ_SECTOR_CMD, "Reads the specified sector of inserted memory card", _read_sector},
{DISK_READ_BYTES_CMD, "Reads the specified bytes from inserted memory card", _read_bytes},
@ -173,5 +171,6 @@ const shell_command_t _shell_command_list[] = {
{ "mersenne_init", "initializes the PRNG", _mersenne_init },
{ "mersenne_get", "returns 32 bit of pseudo randomness", _mersenne_get },
#endif
{NULL, NULL, NULL}
};

View File

@ -33,7 +33,7 @@
#include "shell.h"
#include "shell_commands.h"
static void(*find_handler(const shell_command_t *command_list, char *command))(char *)
static shell_command_handler_t find_handler(const shell_command_t *command_list, char *command)
{
const shell_command_t *command_lists[] = {
command_list,
@ -90,25 +90,64 @@ static void print_help(const shell_command_t *command_list)
static void handle_input_line(shell_t *shell, char *line)
{
char *saveptr;
char *command = strtok_r(line, " ", &saveptr);
void (*handler)(char *) = NULL;
if (command) {
handler = find_handler(shell->command_list, command);
if (handler != NULL) {
line[strlen(command)] = ' ';
handler(line);
/* first we need to calculate the number of arguments */
unsigned argc = 0;
char *pos;
for (pos = line; *pos; ++pos) {
if (*pos <= ' ') {
*pos = 0;
continue;
}
else if (*pos == '"') {
do {
++pos;
if (!*pos) {
puts("shell: incorrect quoting");
return;
}
} while (*pos != '"');
}
else {
if (strcmp("help", command) == 0) {
print_help(shell->command_list);
}
else {
puts("shell: command not found.");
}
do {
++pos;
} while (*pos > ' ');
}
++argc;
*pos = 0;
}
if (!argc) {
return;
}
/* then we fill the argv array */
char *argv[argc + 1];
argv[argc] = NULL;
pos = line;
for (unsigned i = 0; i < argc; ++i) {
while (!*pos) {
++pos;
}
if (*pos == '"') {
++pos;
}
argv[i] = pos;
while (*pos) {
++pos;
}
}
/* then we call the appropriate handler */
shell_command_handler_t handler = find_handler(shell->command_list, argv[0]);
if (handler != NULL) {
handler(argc, argv);
}
else {
if (strcmp("help", argv[0]) == 0) {
print_help(shell->command_list);
}
else {
puts("shell: command not found.");
}
}
}