Commits

Alexander Dahl  committed 4a82e7f

define data structures and function stubs

  • Participants
  • Parent commits 296adc2

Comments (0)

Files changed (4)

 
 # tmp files
 **.swp
+tmp.diff
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include "cmd.h"
+
+/**
+ *  @attention  Beware of MAX_CMD_LENGTH limit!
+ */ 
+struct t_cmd commands[NO_OF_COMMANDS] = {
+        { "help", func_help },
+        { "quit", func_quit },
+};      
+
+int cmd_cmp( const void *e1, const void *e2 ) {
+    struct t_cmd * p_cmd_1 = (struct t_cmd *) e1;
+    struct t_cmd * p_cmd_2 = (struct t_cmd *) e2;
+        
+    return strncmp( p_cmd_1->cmd, p_cmd_2->cmd, MAX_CMD_LENGTH );
+}
+
+char func_help(
+        const unsigned char argc,
+        const char *        argv[] )
+{  
+    (void) printf( "Usage: cmd <arg1> <arg2> <arg3> ...\n" );
+    (void) printf( "\n" );
+    (void) printf( "Commands:\n" );
+    (void) printf( "    help    print this help message\n" );
+    (void) printf( "    quit    quit application\n" );
+        
+    return EXIT_SUCCESS;
+}
+
+char func_quit(
+        const unsigned char argc,
+        const char *        argv[] )
+{
+    (void) printf( "Exiting ...\n" );
+    exit( EXIT_SUCCESS );
+}
 #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      2   /**< number of commands, has to be changed whenever struct t_cmd commands[NO_OF_COMMANDS] is changed    */
+
+/**
+ *  Function pointer type for command evaluation.
+ */
+typedef char (*t_cmd_func)( const unsigned char argc, const char * argv[] );
+
+/**
+ *  Command evaluation structure containing the string of the command
+ *  itself and a function pointer to the function to be executed.
+ */
+struct t_cmd {
+    char        cmd[MAX_CMD_LENGTH];    /**< command string     */
+    t_cmd_func  func;                   /**< function pointer   */
+};
+
+extern struct t_cmd commands[NO_OF_COMMANDS];
+
+/**
+ *  Compare function for qsort() and bsearch() comparing structures of
+ *  type struct t_cmd.
+ *
+ *  @param[in]  e1  pointer to first struct
+ *  @param[in]  e2  pointer to second struct
+ *
+ *  @return -1, 0 or 1 depending which is less, equal or greater.
+ */
+extern int cmd_cmp( const void *e1, const void *e2 );
+
+/**
+ *  Print help message, nothing more.
+ *
+ *  @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_help(
+        const unsigned char argc,
+        const char *        argv[]
+);
+
+extern char func_quit(
+        const unsigned char argc,
+        const char *        argv[]
+);
+
 #endif
+#include <stdio.h>
 #include <stdlib.h>
 #include "cmd.h"
 
 int main( int argc, char * argv[] ) {
 
+    qsort( commands, NO_OF_COMMANDS, sizeof(struct t_cmd), cmd_cmp ); 
+
+    while( 1 ) {
+
+    }
+
     return EXIT_SUCCESS;
 }