From 35d09baa84f3e029646e3a2f2f0e9b348de57517 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Wed, 22 Jul 2009 15:17:37 +0000 Subject: 2009-07-22 Joel Sherrill * 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. --- cpukit/ChangeLog | 12 ++++ cpukit/libmisc/Makefile.am | 2 +- cpukit/libmisc/shell/main_chmod.c | 32 +++++++--- cpukit/libmisc/shell/main_mdump.c | 27 +++++--- cpukit/libmisc/shell/main_medit.c | 34 ++++++++--- cpukit/libmisc/shell/main_mfill.c | 30 +++++++-- cpukit/libmisc/shell/main_mmove.c | 49 +++++++++++---- cpukit/libmisc/shell/main_msdosfmt.c | 34 ++++++++--- cpukit/libmisc/shell/main_mwdump.c | 30 ++++++--- cpukit/libmisc/shell/main_sleep.c | 43 +++++++++---- cpukit/libmisc/shell/main_umask.c | 14 ++++- cpukit/libmisc/shell/shell.h | 13 ++-- cpukit/libmisc/shell/shell_script.c | 25 ++++++-- cpukit/libmisc/shell/str2int.c | 95 ----------------------------- cpukit/libmisc/stringto/stringto_template.h | 2 +- 15 files changed, 255 insertions(+), 187 deletions(-) delete mode 100644 cpukit/libmisc/shell/str2int.c diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index caee9546da..d240a34dfc 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,15 @@ +2009-07-22 Joel Sherrill + + * 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. + 2009-07-22 Joel Sherrill * Makefile.am, preinstall.am, libmisc/Makefile.am, wrapup/Makefile.am: diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index a84699f5bd..871d9abcb3 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -80,7 +80,7 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/shell.c shell/shell_cmdset.c shell/shell_getchar.c \ shell/shell_getprompt.c shell/shellconfig.c \ shell/shellconfig.h shell/shell.h shell/shell_makeargs.c \ - shell/str2int.c shell/filemode.c shell/pwcache.c shell/print-ls.c\ + shell/filemode.c shell/pwcache.c shell/print-ls.c \ shell/write_file.c shell/utils-cp.c shell/utils-ls.c \ shell/err.c shell/errx.c shell/verr.c shell/vis.c \ shell/verrx.c shell/vwarn.c shell/vwarnx.c shell/warn.c shell/warnx.c \ diff --git a/cpukit/libmisc/shell/main_chmod.c b/cpukit/libmisc/shell/main_chmod.c index 50ad4d9ba3..14e744255c 100644 --- a/cpukit/libmisc/shell/main_chmod.c +++ b/cpukit/libmisc/shell/main_chmod.c @@ -25,6 +25,7 @@ #include #include +#include #include "internal.h" int rtems_shell_main_chmod( @@ -32,15 +33,30 @@ int rtems_shell_main_chmod( char *argv[] ) { - int n; - mode_t mode; - - if (argc > 2) { - mode = rtems_shell_str2int(argv[1]) & 0777; - n = 2; - while (n < argc) - chmod(argv[n++], mode); + int n; + mode_t mode; + unsigned long tmp; + + 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; } diff --git a/cpukit/libmisc/shell/main_mdump.c b/cpukit/libmisc/shell/main_mdump.c index 721b3a5658..78b585280d 100644 --- a/cpukit/libmisc/shell/main_mdump.c +++ b/cpukit/libmisc/shell/main_mdump.c @@ -23,28 +23,37 @@ #include #include +#include #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 #include +#include #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; diff --git a/cpukit/libmisc/shell/main_mfill.c b/cpukit/libmisc/shell/main_mfill.c index 605cd58580..1fb027ae4f 100644 --- a/cpukit/libmisc/shell/main_mfill.c +++ b/cpukit/libmisc/shell/main_mfill.c @@ -22,6 +22,7 @@ #include #include +#include #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; } diff --git a/cpukit/libmisc/shell/main_mmove.c b/cpukit/libmisc/shell/main_mmove.c index a97f0ebfa5..f68f4a2ab6 100644 --- a/cpukit/libmisc/shell/main_mmove.c +++ b/cpukit/libmisc/shell/main_mmove.c @@ -22,6 +22,7 @@ #include #include +#include #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; - - 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); + 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; + } + + /* + * 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; } diff --git a/cpukit/libmisc/shell/main_msdosfmt.c b/cpukit/libmisc/shell/main_msdosfmt.c index 58a116aa3c..d682728b9d 100644 --- a/cpukit/libmisc/shell/main_msdosfmt.c +++ b/cpukit/libmisc/shell/main_msdosfmt.c @@ -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 #include +#include #include #include #include @@ -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': diff --git a/cpukit/libmisc/shell/main_mwdump.c b/cpukit/libmisc/shell/main_mwdump.c index 1105656814..3a4f4c6ddd 100644 --- a/cpukit/libmisc/shell/main_mwdump.c +++ b/cpukit/libmisc/shell/main_mwdump.c @@ -23,6 +23,7 @@ #include #include +#include #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 #include +#include #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 = { diff --git a/cpukit/libmisc/shell/main_umask.c b/cpukit/libmisc/shell/main_umask.c index d6a9b7ae17..30996f187f 100644 --- a/cpukit/libmisc/shell/main_umask.c +++ b/cpukit/libmisc/shell/main_umask.c @@ -25,6 +25,7 @@ #include #include +#include #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); diff --git a/cpukit/libmisc/shell/shell.h b/cpukit/libmisc/shell/shell.h index a9782b846b..759bf71589 100644 --- a/cpukit/libmisc/shell/shell.h +++ b/cpukit/libmisc/shell/shell.h @@ -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; diff --git a/cpukit/libmisc/shell/shell_script.c b/cpukit/libmisc/shell/shell_script.c index 34be6cd0ec..c31ed051d3 100644 --- a/cpukit/libmisc/shell/shell_script.c +++ b/cpukit/libmisc/shell/shell_script.c @@ -30,6 +30,7 @@ #include #include +#include #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; diff --git a/cpukit/libmisc/shell/str2int.c b/cpukit/libmisc/shell/str2int.c deleted file mode 100644 index ee02f74bf3..0000000000 --- a/cpukit/libmisc/shell/str2int.c +++ /dev/null @@ -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 -#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; -} diff --git a/cpukit/libmisc/stringto/stringto_template.h b/cpukit/libmisc/stringto/stringto_template.h index b1890e81d0..501f4393f8 100644 --- a/cpukit/libmisc/stringto/stringto_template.h +++ b/cpukit/libmisc/stringto/stringto_template.h @@ -101,7 +101,7 @@ bool STRING_TO_NAME ( return false; #endif - *n = result; + *n = (STRING_TO_TYPE) result; return true; } -- cgit v1.2.3