diff --git a/sys/include/ps.h b/sys/include/ps.h index 2b755dc184..cc63a3d015 100644 --- a/sys/include/ps.h +++ b/sys/include/ps.h @@ -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 */ diff --git a/sys/include/shell.h b/sys/include/shell.h index b3776b98aa..ff346f704b 100644 --- a/sys/include/shell.h +++ b/sys/include/shell.h @@ -22,10 +22,12 @@ #define __SHELL_H #include +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 { diff --git a/sys/shell/commands/sc_cc1100.c b/sys/shell/commands/sc_cc1100.c index cca1a581b3..041ed52a9d 100644 --- a/sys/shell/commands/sc_cc1100.c +++ b/sys/shell/commands/sc_cc1100.c @@ -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); diff --git a/sys/shell/commands/sc_disk.c b/sys/shell/commands/sc_disk.c index cebe67f85b..66fc8b02ae 100644 --- a/sys/shell/commands/sc_disk.c +++ b/sys/shell/commands/sc_disk.c @@ -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 \n", DISK_READ_SECTOR_CMD); + printf("[disk] Usage:\n%s \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 \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 \n", DISK_READ_BYTES_CMD); } diff --git a/sys/shell/commands/sc_heap.c b/sys/shell/commands/sc_heap.c index 233d862a80..ee6156159e 100644 --- a/sys/shell/commands/sc_heap.c +++ b/sys/shell/commands/sc_heap.c @@ -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(); } diff --git a/sys/shell/commands/sc_id.c b/sys/shell/commands/sc_id.c index 8ed7aa627a..36389f192d 100644 --- a/sys/shell/commands/sc_id.c +++ b/sys/shell/commands/sc_id.c @@ -20,16 +20,13 @@ #include #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; diff --git a/sys/shell/commands/sc_ltc4150.c b/sys/shell/commands/sc_ltc4150.c index 7acf7f25ce..0f48d0c2ec 100644 --- a/sys/shell/commands/sc_ltc4150.c +++ b/sys/shell/commands/sc_ltc4150.c @@ -18,16 +18,19 @@ #include #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(); } diff --git a/sys/shell/commands/sc_mersenne.c b/sys/shell/commands/sc_mersenne.c index b6440e9b3f..c21a9a7665 100644 --- a/sys/shell/commands/sc_mersenne.c +++ b/sys/shell/commands/sc_mersenne.c @@ -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()); } diff --git a/sys/shell/commands/sc_ps.c b/sys/shell/commands/sc_ps.c index f076b78f69..226d49ea14 100644 --- a/sys/shell/commands/sc_ps.c +++ b/sys/shell/commands/sc_ps.c @@ -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(); } diff --git a/sys/shell/commands/sc_rtc.c b/sys/shell/commands/sc_rtc.c index 56db40fbc6..7b30ee3348 100644 --- a/sys/shell/commands/sc_rtc.c +++ b/sys/shell/commands/sc_rtc.c @@ -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]); } } diff --git a/sys/shell/commands/sc_sht11.c b/sys/shell/commands/sc_sht11.c index ce088ec562..67620fd7d5 100644 --- a/sys/shell/commands/sc_sht11.c +++ b/sys/shell/commands/sc_sht11.c @@ -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 "); + if (argc != 2) { + printf("Usage: %s \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); } } diff --git a/sys/shell/commands/sc_sys.c b/sys/shell/commands/sc_sys.c index 54ecaec49e..5bb381d3f4 100644 --- a/sys/shell/commands/sc_sys.c +++ b/sys/shell/commands/sc_sys.c @@ -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(); } diff --git a/sys/shell/commands/sc_transceiver.c b/sys/shell/commands/sc_transceiver.c index 6ec76f9fea..6d5ec8c5e6 100644 --- a/sys/shell/commands/sc_transceiver.c +++ b/sys/shell/commands/sc_transceiver.c @@ -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 \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 "); -} - -/* 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 "); - } + 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 \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
\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
"); + printf("Success (added at index %" PRIi16 ").\n", response); } } #endif diff --git a/sys/shell/commands/shell_commands.c b/sys/shell/commands/shell_commands.c index d9bf2c880f..8149785a79 100644 --- a/sys/shell/commands/shell_commands.c +++ b/sys/shell/commands/shell_commands.c @@ -23,34 +23,34 @@ #include #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} }; diff --git a/sys/shell/shell.c b/sys/shell/shell.c index b72709f9bd..732a27223e 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -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."); } } }