Commits

Alexander Dahl committed e358041

solve input line flushing problem, hopefully. introduced function 'dummy' and completed help texts

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 },
+        { "dump",   func_dump },
+        { "quit",   func_quit },
+        { "help",   func_help },
+        { "dummy",  func_dummy },
 };      
 
 int cmd_cmp( const void *e1, const void *e2 ) {
     return strncmp( p_cmd_1->cmd, p_cmd_2->cmd, MAX_CMD_LENGTH );
 }
 
+char func_dummy(
+        const unsigned char argc,
+        const char *        argv[] )
+{
+    (void) puts( "Never mind, no real errors happened!" );
+    return EXIT_FAILURE;
+}
+
 char func_dump(
         const unsigned char argc,
         const char *        argv[] )
     (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] );
+        (void) printf( "argv[%02u] at %p: '%s'\n", i, argv[i], argv[i] );
     }
 
     return EXIT_SUCCESS;
     (void) printf( "Usage: cmd <arg1> <arg2> <arg3> ...\n" );
     (void) printf( "\n" );
     (void) printf( "Commands:\n" );
+    (void) printf( "    dummy   return with 1 from function\n" );
+    (void) printf( "    dump    show all args to dump function\n" );
     (void) printf( "    help    print this help message\n" );
     (void) printf( "    quit    quit application\n" );
         
 #ifndef _CMD_H_
 #define _CMD_H_
 
-#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      3   /**< number of commands, has to be changed whenever struct t_cmd commands[NO_OF_COMMANDS] is changed    */
+#define INPUT_LINE_LENGTH   21  /**< maximum length of command line including terminating zero                                          */
+#define MAX_CMD_LENGTH      6   /**< maximum length of command string itself, including terminating zero                                */
+#define NO_OF_COMMANDS      4   /**< 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 );
 
 /**
+ *  Produce dummy error.
+ *
+ *  @param[in]  argc    number of arguments
+ *  @param[in]  argv[]  list of null terminated argument strings
+ *
+ *  @return EXIT_FAILURE, yes, always!
+ */
+extern char func_dummy(
+        const unsigned char argc,
+        const char *        argv[]
+);
+
+/**
  *  Dump any memory content. Open for changing.
  *
  *  @param[in]  argc    number of arguments
 int main( int argc, char * argv[] ) {
     unsigned char   _argc = 0, i;
     char *          _argv[( INPUT_LINE_LENGTH + 1 ) / 2] = { 0 };
-    char            ch;
     char *          cmd_string;
     struct t_cmd    cmd_to_check = { "", NULL };
     char            input_buffer[INPUT_LINE_LENGTH] = { 0 };
     char            input_buffer_cpy[INPUT_LINE_LENGTH] = { 0 };
+    char            trash_buffer[1024];
     size_t          input_string_length;
     char            last_return_value = EXIT_SUCCESS;
     struct t_cmd *  p_found_cmd;
         _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.   */
+         *  from STDIN. */
         if ( last_return_value ) {
             (void) printf( "%d > ", last_return_value );
         } else {
         }
 
         input_string_length = strlen( input_buffer );
+
+        /*  if input line longer than INPUT_LINE_LENGTH, flush.
+         *  note: this is not platform independent, especially on
+         *  microcontrollers STDIN maybe unbuffered!    */
+        if ( input_string_length == INPUT_LINE_LENGTH - 1 &&
+                input_buffer[input_string_length-1] != '\n' ) {
+            do {
+                fgets( trash_buffer, 1024, stdin );
+            } while ( trash_buffer[strlen(trash_buffer)-1] != '\n' );
+            fprintf( stderr, 
+                    "cmdline was longer than %d characters, truncated!\n",
+                    INPUT_LINE_LENGTH - 1 );
+        }
+
+        /*  handle trailing newline */
         if ( input_buffer[input_string_length-1] == '\n' ) {
             input_buffer[input_string_length-1] = ' ';
         }
             _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 );
             last_return_value = p_found_cmd->func( _argc, (const char **) _argv );
         }
 
-        /**
-          * @todo   flush stdio input buffer to not get input more than
-          *         our buffer size next time. issue warning if
-          *         truncated.
-          */
-        puts( "Flushing input ..." ); 
-        while ( ( ch = getchar() ) != '\n' && ch != EOF );
 
     }