2009-07-22 Joel Sherrill <joel.sherrill@oarcorp.com>

* libmisc/Makefile.am, libmisc/shell/main_chmod.c,
	libmisc/shell/main_mdump.c, libmisc/shell/main_medit.c,
	libmisc/shell/main_mfill.c, libmisc/shell/main_mmove.c,
	libmisc/shell/main_msdosfmt.c, libmisc/shell/main_mwdump.c,
	libmisc/shell/main_sleep.c, libmisc/shell/main_umask.c,
	libmisc/shell/shell.h, libmisc/shell/shell_script.c,
	libmisc/stringto/stringto_template.h: Convert all shell code to use
	stringto.h mehods with better error checking.
	* libmisc/shell/str2int.c: Removed.
This commit is contained in:
Joel Sherrill
2009-07-22 15:17:37 +00:00
parent e8d59ca6af
commit 35d09baa84
15 changed files with 252 additions and 184 deletions

View File

@@ -25,6 +25,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
int rtems_shell_main_chmod(
@@ -32,15 +33,30 @@ int rtems_shell_main_chmod(
char *argv[]
)
{
int n;
mode_t mode;
int n;
mode_t mode;
unsigned long tmp;
if (argc > 2) {
mode = rtems_shell_str2int(argv[1]) & 0777;
n = 2;
while (n < argc)
chmod(argv[n++], mode);
if (argc < 2) {
fprintf(stderr,"%s: too few arguments\n", argv[0]);
return -1;
}
/*
* Convert arguments into numbers
*/
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Mode argument (%s) is not a number\n", argv[1] );
return -1;
}
mode = (mode_t) (tmp & 0777);
/*
* Now change the files modes
*/
for (n=2 ; n < argc ; n++)
chmod(argv[n++], mode);
return 0;
}

View File

@@ -23,28 +23,37 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
/*----------------------------------------------------------------------------*
* RAM MEMORY COMMANDS
*----------------------------------------------------------------------------*/
int rtems_shell_main_mdump(
int argc,
char *argv[]
)
{
unsigned char n, m;
unsigned long tmp;
unsigned char n;
unsigned char m;
int max;
int res;
uintptr_t addr = 0;
unsigned char *pb;
if (argc>1)
addr = rtems_shell_str2int(argv[1]);
if (argc > 1) {
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Address argument (%s) is not a number\n", argv[1] );
return -1;
}
addr = (uintptr_t) tmp;
}
if (argc>2) {
max = rtems_shell_str2int(argv[2]);
if ( !rtems_string_to_int(argv[1], &max, NULL, 0) ) {
printf( "Length argument (%s) is not a number\n", argv[1] );
return -1;
}
addr = (uintptr_t) tmp;
if (max <= 0) {
max = 1; /* print 1 item if 0 or neg. */
res = 0;
@@ -66,8 +75,8 @@ int rtems_shell_main_mdump(
}
for (m=0; m<max; m++) {
printf("0x%08" PRIXPTR " ", addr);
pb = (unsigned char*) addr;
printf("%p ", pb);
for (n=0;n<=(m==(max-1)?res:0xf);n++)
printf("%02X%c",pb[n],n==7?'-':' ');
for (;n<=0xf;n++)

View File

@@ -22,6 +22,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
extern int rtems_shell_main_mdump(int, char *);
@@ -31,19 +32,38 @@ int rtems_shell_main_medit(
char *argv[]
)
{
unsigned char * pb;
int n,i;
unsigned long tmp;
unsigned char *pb;
int n;
int i;
if (argc<3) {
if ( argc < 3 ) {
fprintf(stderr,"%s: too few arguments\n", argv[0]);
return -1;
}
pb = (unsigned char*)rtems_shell_str2int(argv[1]);
i = 2;
/*
* Convert arguments into numbers
*/
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Address argument (%s) is not a number\n", argv[1] );
return -1;
}
pb = (unsigned char *) tmp;
/*
* Now edit the memory
*/
n = 0;
while (i<=argc) {
pb[n++] = rtems_shell_str2int(argv[i++]) % 0x100;
for (i=2 ; i<=argc ; i++) {
unsigned char tmpc;
if ( !rtems_string_to_unsigned_char(argv[i], &tmpc, NULL, 0) ) {
printf( "Value (%s) is not a number\n", argv[i] );
continue;
}
pb[n++] = tmpc;
}
return 0;

View File

@@ -22,6 +22,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
int rtems_shell_main_mfill(
@@ -29,6 +30,7 @@ int rtems_shell_main_mfill(
char *argv[]
)
{
unsigned long tmp;
uintptr_t addr;
size_t size;
unsigned char value;
@@ -38,10 +40,30 @@ int rtems_shell_main_mfill(
return -1;
}
addr = rtems_shell_str2int(argv[1]);
size = rtems_shell_str2int(argv[2]);
value = rtems_shell_str2int(argv[3]) % 0x100;
memset((unsigned char*)addr,size,value);
/*
* Convert arguments into numbers
*/
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Address argument (%s) is not a number\n", argv[1] );
return -1;
}
addr = (uintptr_t) tmp;
if ( !rtems_string_to_unsigned_long(argv[2], &tmp, NULL, 0) ) {
printf( "Size argument (%s) is not a number\n", argv[2] );
return -1;
}
size = (size_t) tmp;
if ( !rtems_string_to_unsigned_char(argv[3], &value, NULL, 0) ) {
printf( "Value argument (%s) is not a number\n", argv[3] );
return -1;
}
/*
* Now fill the memory.
*/
memset((unsigned char*)addr, size, value);
return 0;
}

View File

@@ -22,6 +22,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
extern int rtems_shell_main_mdump(int, char *);
@@ -31,19 +32,41 @@ int rtems_shell_main_mmove(
char *argv[]
)
{
uintptr_t src;
uintptr_t dst;
size_t length;
unsigned long tmp;
uintptr_t src;
uintptr_t dst;
size_t length;
if ( argc<4 ) {
fprintf(stderr,"%s: too few arguments\n", argv[0]);
return -1;
}
if ( argc < 4 ) {
fprintf(stderr,"%s: too few arguments\n", argv[0]);
return -1;
}
dst = rtems_shell_str2int(argv[1]);
src = rtems_shell_str2int(argv[2]);
length = rtems_shell_str2int(argv[3]);
memcpy((unsigned char*)dst, (unsigned char*)src, length);
/*
* Convert arguments into numbers
*/
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Destination argument (%s) is not a number\n", argv[1] );
return -1;
}
dst = (uintptr_t) tmp;
if ( !rtems_string_to_unsigned_long(argv[2], &tmp, NULL, 0) ) {
printf( "Source argument (%s) is not a number\n", argv[2] );
return -1;
}
src = (uintptr_t) tmp;
if ( !rtems_string_to_unsigned_long(argv[3], &tmp, NULL, 0) ) {
printf( "Length argument (%s) is not a number\n", argv[3] );
return -1;
}
length = (size_t) tmp;
/*
* Now copy the memory.
*/
memcpy((unsigned char*)dst, (unsigned char*)src, length);
return 0;
}

View File

@@ -1,10 +1,4 @@
/*
* Shell Command Implmentation
*
* Author: Fernando RUIZ CASAS
* Work: fernando.ruiz@ctv.es
* Home: correo@fernando-ruiz.com
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
@@ -23,6 +17,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include <rtems/shellconfig.h>
#include <rtems/dosfs.h>
#include <rtems/fsmount.h>
@@ -46,8 +41,9 @@ int rtems_shell_main_msdos_format(
info_level: 0
};
const char* driver = NULL;
int arg;
unsigned long tmp;
const char* driver = NULL;
int arg;
for (arg = 1; arg < argc; arg++) {
if (argv[arg][0] == '-') {
@@ -67,7 +63,16 @@ int rtems_shell_main_msdos_format(
fprintf (stderr, "error: sectors per cluster count.\n");
return 1;
}
rqdata.sectors_per_cluster = rtems_shell_str2int(argv[arg]);
if ( !rtems_string_to_unsigned_long(argv[arg], &tmp, NULL, 0) ) {
printf(
"sector per cluster argument (%s) is not a number\n",
argv[arg]
);
return -1;
}
rqdata.sectors_per_cluster = (uint32_t) tmp;
break;
case 'r':
@@ -76,7 +81,16 @@ int rtems_shell_main_msdos_format(
fprintf (stderr, "error: no root directory size.\n");
return 1;
}
rqdata.files_per_root_dir = rtems_shell_str2int(argv[arg]);
if ( !rtems_string_to_unsigned_long(argv[arg], &tmp, NULL, 0) ) {
printf(
"root directory size argument (%s) is not a number\n",
argv[arg]
);
return -1;
}
rqdata.files_per_root_dir = (uint32_t) tmp;
break;
case 't':

View File

@@ -23,6 +23,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
int rtems_shell_main_mwdump(
@@ -30,22 +31,32 @@ int rtems_shell_main_mwdump(
char *argv[]
)
{
unsigned char n, m;
unsigned long tmp;
unsigned char n;
unsigned char m;
int max;
int res;
uintptr_t addr = 0;
unsigned char *pb;
if (argc>1)
addr = rtems_shell_str2int(argv[1]);
if ( argc > 1 ) {
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Address argument (%s) is not a number\n", argv[1] );
return -1;
}
addr = (uintptr_t) tmp;
}
if ( argc > 2 ) {
if ( !rtems_string_to_int(argv[2], &max, NULL, 0) ) {
printf( "Address argument (%s) is not a number\n", argv[1] );
return -1;
}
if (argc>2) {
max = rtems_shell_str2int(argv[2]);
if (max <= 0) {
max = 1; /* print 1 item if 0 or neg. */
res = 0;
}
else {
} else {
max--;
res = max & 0xf;/* num bytes in last row */
max >>= 4; /* div by 16 */
@@ -55,15 +66,14 @@ int rtems_shell_main_mwdump(
res = 0xf; /* 16 bytes print in last row */
}
}
}
else {
} else {
max = 20;
res = 0xf;
}
for (m=0;m<max;m++) {
printf("0x%08" PRIXPTR " ",addr);
pb = (unsigned char *) addr;
printf("%p ", pb);
for (n=0;n<=(m==(max-1)?res:0xf);n+=2)
printf("%04X%c",*((unsigned short*)(pb+n)),n==6?'-':' ');
for (;n<=0xf;n+=2)

View File

@@ -20,6 +20,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
int rtems_shell_main_sleep(
@@ -28,23 +29,39 @@ int rtems_shell_main_sleep(
)
{
struct timespec delay;
unsigned long tmp;
if (argc == 2) {
delay.tv_sec = rtems_shell_str2int(argv[1]);
delay.tv_nsec = 0;
nanosleep( &delay, NULL );
return 0;
if ((argc != 2) && (argc != 3)) {
fprintf( stderr, "%s: Usage seconds [nanoseconds]\n", argv[0] );
return -1;
}
/*
* Convert the seconds argument to a number
*/
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Seconds argument (%s) is not a number\n", argv[1] );
return -1;
}
delay.tv_sec = (time_t) tmp;
/*
* If the user specified a nanoseconds argument, convert it
*/
delay.tv_nsec = 0;
if (argc == 3) {
delay.tv_sec = rtems_shell_str2int(argv[1]);
delay.tv_nsec = rtems_shell_str2int(argv[2]);
nanosleep( &delay, NULL );
return 0;
if ( !rtems_string_to_unsigned_long(argv[2], &tmp, NULL, 0) ) {
printf( "Seconds argument (%s) is not a number\n", argv[1] );
return -1;
}
delay.tv_nsec = tmp;
}
fprintf( stderr, "%s: Usage seconds [nanoseconds]\n", argv[0] );
return -1;
/*
* Now sleep as requested.
*/
nanosleep( &delay, NULL );
return 0;
}
rtems_shell_cmd_t rtems_shell_SLEEP_Command = {

View File

@@ -25,6 +25,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
int rtems_shell_main_umask(
@@ -32,10 +33,17 @@ int rtems_shell_main_umask(
char *argv[]
)
{
mode_t msk = umask(0);
unsigned long tmp;
mode_t msk = umask(0);
if (argc == 2)
msk = rtems_shell_str2int(argv[1]);
if (argc == 2) {
if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) {
printf( "Mask argument (%s) is not a number\n", argv[1] );
return -1;
}
msk = (mode_t) tmp;
}
umask(msk);
msk = umask(0);

View File

@@ -183,22 +183,19 @@ rtems_status_code rtems_shell_script(
bool echo
);
/*
* Things that are useful to external entities developing commands and plugging
* them in.
/**
* Private environment associated with each shell instance.
*/
int rtems_shell_str2int(const char * s);
typedef struct {
rtems_name magic; /* 'S','E','N','V': Shell Environment */
/** 'S','E','N','V': Shell Environment */
rtems_name magic;
const char *devname;
const char *taskname;
/* user extensions */
bool exit_shell; /* logout */
bool forever; /* repeat login */
int errorlevel;
bool echo;
char cwd [256];
char cwd[256];
const char *input;
const char *output;
bool output_append;

View File

@@ -30,6 +30,7 @@
#include <rtems.h>
#include <rtems/shell.h>
#include <rtems/stringto.h>
#include "internal.h"
static void rtems_shell_joel_usage(void)
@@ -107,6 +108,7 @@ int rtems_shell_main_joel(
char **argv
)
{
unsigned long tmp;
int option;
int sc;
int verbose = 0;
@@ -124,13 +126,26 @@ int rtems_shell_main_joel(
case 'o':
outputFile = getopt_reent.optarg;
break;
case 'p':
taskPriority =
(rtems_task_priority) rtems_shell_str2int(getopt_reent.optarg);
case 'p': {
const char *s = getopt_reent.optarg;
if ( !rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
printf( "Task Priority argument (%s) is not a number\n", s );
return -1;
}
taskPriority = (rtems_task_priority) tmp;
break;
case 's':
stackSize = (uint32_t) rtems_shell_str2int(getopt_reent.optarg);
}
case 's': {
const char *s = getopt_reent.optarg;
if ( !rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
printf( "Stack size argument (%s) is not a number\n", s );
return -1;
}
stackSize = (uint32_t) tmp;
break;
}
case 't':
taskName = getopt_reent.optarg;
break;

View File

@@ -1,95 +0,0 @@
/*
* Author: Fernando RUIZ CASAS
* Work: fernando.ruiz@ctv.es
* Home: correo@fernando-ruiz.com
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <rtems/shell.h>
#include "internal.h"
/*
* str to int "0xaffe" "0b010010" "0123" "192939"
*/
int rtems_shell_str2int(const char * s) {
int sign=1;
int base=10;
int value=0;
int digit;
if (!s) return 0;
if (*s) {
if (*s=='-') {
sign=-1;
s++;
if (!*s) return 0;
}
if (*s=='0') {
s++;
switch(*s) {
case 'x':
case 'X':
s++;
base=16;
break;
case 'b':
case 'B':
s++;
base=2;
break;
default:
base=8;
break;
}
}
while (*s) {
switch(*s) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digit=*s-'0';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
digit=*s-'A'+10;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
digit=*s-'a'+10;
break;
default:
return value*sign;
}
if (digit>base)
return value*sign;
value=value*base+digit;
s++;
}
}
return value*sign;
}