内部模块化的命令行菜单

win7与VMware ubuntu虚拟机的共享文件夹在cd /mnt/hgfs下


linklist.h:

/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linklist.h                                                            */
/* PRINCIPAL AUTHOR      :  ls                                                                    */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linklist                                                              */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  linklist for menu progrom                                             */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/21
 *
 */

/* data struct and its operations */

typedef struct DataNode
{
    char*    cmd;
    char*    desc;
    int      (*handler)();
    struct   DataNode *next;
} tDataNode;

/* find a cmd in the linklist and return the data node pointer */
tDataNode * FindCmd(tDataNode *head, char *cmd);
/* show all cmd in linklist */
int ShowAllCmd(tDataNode* head);


linklist.c:

/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linklist.c                                                            */
/* PRINCIPAL AUTHOR      :  LISUN                                                                 */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linklist                                                                  */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  linklist for the  menu progrom                                                */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/21
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"


tDataNode* FindCmd(tDataNode* head, char* cmd)
{
     if (head == NULL || cmd == NULL)
     {
          return NULL;
     }
     tDataNode *p = head;
     while (p != NULL)
     {
          if (strcmp(p->cmd, cmd) == 0)
          {
               return p;
          }
          p = p->next;
      }
      return NULL;
} 

int ShowAllCmd(tDataNode* head)
{
     printf("Menu List:\n");
     tDataNode *p = head;
     while (p != NULL)
     {
          printf("%s - %s\n", p->cmd, p->desc);
	  p = p->next;
     }

     return 0;

}

menu.c

/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  menu.c                                                                */
/* PRINCIPAL AUTHOR      :  LISUN                                                                 */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  menu                                                                  */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  This is a menu progrom                                                */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/21
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"


int Help();
int Quit();


#define CMD_MAX_LEN 128
#define DESC_LEN    1024
#define CMD_NUM     10

/*
typedef struct DataNode
{
    char*    cmd;
    char*    desc;
    int      (*handler)();
    struct   DataNode *next;
} tDataNode;

tDataNode* FindCmd(tDataNode* head, char* cmd)
{
     if (head == NULL || cmd == NULL)
     {
          return NULL;
     }
     tDataNode *p = head;
     while (p != NULL)
     {
          if (strcmp(p->cmd, cmd) == 0)
          {
               return p;
          }
          p = p->next;
      }
      return NULL;
} 

int ShowAllCmd(tDataNode* head)
{
     printf("Menu List:\n");
     tDataNode *p = head;
     while (p != NULL)
     {
          printf("%s - %s\n", p->cmd, p->desc);
	  p = p->next;
     }

     return 0;
}*/
static tDataNode head[] =
{
    {"help", "this is help cmd!", Help, &head[1]},
    {"version", "menu program v1.0", NULL, &head[2]},
    {"quit", "Quit from menu", Quit, NULL}
};


int main ()
{
    /* cmd line begins */
    while (1)
    {
         char cmd[CMD_MAX_LEN];
	 printf("Input a cmd number > ");
	 scanf("%s", cmd);
	 tDataNode *p = FindCmd(head, cmd);
         if (p == NULL)
	 {
	      printf("This is a wrong cmd!\n");
	      continue;
	 }

	 printf("%s - %s\n", p->cmd, p->desc);
         if (p->handler != NULL)
	 {
	      p->handler();
	 }

    }
}

int Help()
{
     printf("Menu List:\n");
     tDataNode *p = head;
     while (p != NULL)
     {
          printf("%s - %s\n", p->cmd, p->desc);
	  p = p->next;
     }
     return 0;
}

int Quit()
{
    exit(0);
}





参考:

【1】 win7与VMware ubuntu虚拟机实现文件共享(最后一定要装open-vm-dkms插件)http://blog.csdn.net/warringah1/article/details/8927437  

你可能感兴趣的:(内部模块化的命令行菜单)