内部模块化的命令行菜单

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  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值