Commits

Alexander Dahl committed 4813fb3

introduced dump command and fixed trailing newline handling

Comments (0)

Files changed (3)

  *  @attention  Beware of MAX_CMD_LENGTH limit!
  */ 
 struct t_cmd commands[NO_OF_COMMANDS] = {
+        { "dump", func_dump },
+        { "quit", func_quit },
         { "help", func_help },
-        { "quit", func_quit },
 };      
 
 int cmd_cmp( const void *e1, const void *e2 ) {
     return strncmp( p_cmd_1->cmd, p_cmd_2->cmd, MAX_CMD_LENGTH );
 }
 
+char func_dump(
+        const unsigned char argc,
+        const char *        argv[] )
+{
+    unsigned char   i;
+
+    (void) printf( "func_dump() called ...\n" );
+    (void) printf( "argc: %u\n", argc );
+    for ( i = 0; i < argc; i++ ) {
+        (void) printf( "argv[%02u] at %p: %s\n", i, argv[i], argv[i] );
+    }
+
+    return EXIT_SUCCESS;
+}
+
 char func_help(
         const unsigned char argc,
         const char *        argv[] )
 
 #define INPUT_LINE_LENGTH   20  /**< maximum length of command line including terminating zero                                          */
 #define MAX_CMD_LENGTH      5   /**< maximum length of command string itself, including terminating zero                                */
-#define NO_OF_COMMANDS      2   /**< number of commands, has to be changed whenever struct t_cmd commands[NO_OF_COMMANDS] is changed    */
+#define NO_OF_COMMANDS      3   /**< number of commands, has to be changed whenever struct t_cmd commands[NO_OF_COMMANDS] is changed    */
 
 /**
  *  Function pointer type for command evaluation.
 extern int cmd_cmp( const void *e1, const void *e2 );
 
 /**
+ *  Dump any memory content. Open for changing.
+ *
+ *  @param[in]  argc    number of arguments
+ *  @param[in]  argv[]  list of null terminated argument strings
+ *
+ *  @return 0 in case of success, error value otherwise
+ */
+extern char func_dump(
+        const unsigned char argc,
+        const char *        argv[]
+);
+
+/**
  *  Print help message, nothing more.
  *
  *  @param[in]  argc    number of arguments
     char *          _argv[( INPUT_LINE_LENGTH + 1 ) / 2] = { 0 };
     char *          cmd_string;
     struct t_cmd    cmd_to_check = { "", NULL };
-    char            input_buffer[INPUT_LINE_LENGTH];
-    char            input_buffer_cpy[INPUT_LINE_LENGTH];
+    char            input_buffer[INPUT_LINE_LENGTH] = { 0 };
+    char            input_buffer_cpy[INPUT_LINE_LENGTH] = { 0 };
     size_t          input_string_length;
     char            last_return_value = EXIT_SUCCESS;
     struct t_cmd *  p_found_cmd;
 
+    /*  test    */
+    /*
+    printf( "Newline:         0x%02X\n", '\n' );
+    printf( "Carriage Return: 0x%02X\n", '\r' );
+    */
+    /*  /test   */
+
     qsort( commands, NO_OF_COMMANDS, sizeof(struct t_cmd), cmd_cmp ); 
 
+    (void) printf( "Sorted commands:" );
+    for ( i = 0; i < NO_OF_COMMANDS; i++ ) {
+        printf( " %s", commands[i].cmd );
+    }
+    (void) printf( "\n\n" );
+
     while( 1 ) {
+        
+        /*  (Re)initialize. */
+        _argc = 0;
+        memset( _argv, 0, ( INPUT_LINE_LENGTH + 1 ) / 2 );
 
         /*  Print prompt and read one line with length INPUT_LINE_LENGTH
          *  from STDIN. Handle trailing newline character for later
          *  processing of command input line.   */
         if ( last_return_value ) {
-            (void) printf( "%d uC> ", last_return_value );
+            (void) printf( "%d > ", last_return_value );
         } else {
-            (void) printf( "uC> " );
+            (void) printf( "> " );
         }
         if ( ! fgets( input_buffer, INPUT_LINE_LENGTH, stdin ) ) {
             (void) fprintf( stderr, "Error reading from STDIN!\n" );
             break;
         }
-        input_string_length = strlen(input_buffer);
-        if ( input_buffer[input_string_length-1] != 0x0A ) {
-            (void) printf( "\n" );
-        } else {
+
+        /* (void) printf( "input_buffer: '%s'\n", input_buffer ); */
+        input_string_length = strlen( input_buffer );
+        if ( input_buffer[input_string_length-1] == '\n' ) {
             input_buffer[input_string_length-1] = ' ';
+            /*
+            fprintf( stderr, 
+                    "Replaced trailing LF with whitespace ...\n" );
+            */
         }
 
+        /**
+          * @todo   flush stdio input buffer to not get input more than
+          *         our buffer size next time. issue warning if
+          *         truncated.
+          */
+
+        /*
+        for ( i = 0; i < INPUT_LINE_LENGTH; i++ ) {
+            (void) printf( "%02X ", input_buffer[i] );        
+        }
+        (void) printf( "\n" );
+        */
+
         /*  Count words on command line and safe into _argc */
         strncpy( input_buffer_cpy, input_buffer, INPUT_LINE_LENGTH );
+        /*
+        for ( i = 0; i < INPUT_LINE_LENGTH; i++ ) {
+            (void) printf( "%02X ", input_buffer_cpy[i] );        
+        }
+        (void) printf( "\n" );
+        return EXIT_SUCCESS;
+        */
         cmd_string = strtok( input_buffer_cpy, " \t" );
         while ( cmd_string != NULL ) {
             _argc++;
             cmd_string = strtok( NULL, " \t" );
         }
+        printf( "_argc: %d\n", _argc );
 
         /*  Parse for command and assemble _argv[]  */
         strncpy( input_buffer_cpy, input_buffer, INPUT_LINE_LENGTH );
         cmd_string = strtok( input_buffer_cpy, " \t" );
         strncpy( cmd_to_check.cmd, cmd_string, MAX_CMD_LENGTH );
+        printf( "parsed command: '%s'\n", cmd_to_check.cmd );
         i = 0;
         do {
             _argv[i++]  = cmd_string;