实验七:将menu设计为可重用的子系统


实验步骤

  • linktable.clinktable.h与上次的实验一样,这里不在赘述。
  • menu.h的定义,菜单的添加函数和执行函数。
/* add cmd to menu */
int MenuConfig(char* cmd, char* desc, int (*handler)(int argc, char *argv[]));
/* Menu Engine Execute */
int ExecuteMenu();
  • menu.c的实现,与原来的menu.c相比,此次实验增加了MenuConfig函数来添加指令,ExecuteMenu函数来执行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "linktable.h"
#include "menu.h"
tLinkTable * head = NULL;
#define CMD_MAX_LEN      128
#define DESC_LEN         1024
#define CMD_NUM          10
#define CMD_MAX_ARGV_NUM 10
/* data struct and its operations */
typedef struct DataNode
{
    tLinkTableNode * pNext;
    char*   cmd;
    char*   desc;
    int     (*handler)(int argc, char *argv[]);
} tDataNode;
int SearchCondition(tLinkTableNode * pLinkTableNode, void *args)
{
    char *cmd = (char *)args;
    tDataNode * pNode = (tDataNode *)pLinkTableNode;
    if(strcmp(pNode->cmd, cmd) == 0)
    {
        return  SUCCESS;  
    }
    return FAILURE;           
}
/* find a cmd in the linklist and return the datanode pointer */
tDataNode* FindCmd(tLinkTable * head, char * cmd)
{
    return  (tDataNode*)SearchLinkTableNode(head,SearchCondition,cmd);
}
/* show all cmd in listlist */
int ShowAllCmd(tLinkTable * head)
{
    tDataNode * pNode = (tDataNode*)GetLinkTableHead(head);
    while(pNode != NULL)
    {
        printf("%s - %s\n", pNode->cmd, pNode->desc);
        pNode = (tDataNode*)GetNextLinkTableNode(head,(tLinkTableNode *)pNode);
    }
    return 0;
}
int Help(int argc, char *argv[])
{
    ShowAllCmd(head);
    return 0;
}

int MenuConfig(char* cmd, char* desc, int (*handler)(int argc, char *argv[]))
{
    tDataNode *pNode = NULL;
    if(head == NULL)
    {
        head = CreateLinkTable();
        pNode = (tDataNode*)malloc(sizeof(tDataNode));
        pNode->cmd = "help";
        pNode->desc = "This is the help list";
        pNode->handler = Help;
        AddLinkTableNode(head, (tLinkTableNode*)pNode);
    }
    pNode = (tDataNode*)malloc(sizeof(tDataNode));
    pNode->cmd = cmd;
    pNode->desc = desc;
    pNode->handler = handler;
    AddLinkTableNode(head, (tLinkTableNode*)pNode);
    return 0;
}
/* menu program */
int ExecuteMenu()
{
   /* cmd line begins */
    while(1)
    {
        int argc = 0;
        char *argv[CMD_MAX_ARGV_NUM];
        char cmd[CMD_MAX_LEN];
        char *pcmd = NULL;
        printf("Input a command> ");
        pcmd = fgets(cmd, CMD_MAX_LEN, stdin);
        tDataNode *p = FindCmd(head, cmd);
        if( pcmd == NULL)
        {
            continue;
        }
        pcmd = strtok(pcmd, " ");
        while(pcmd != NULL && argc < CMD_MAX_ARGV_NUM)
        {
            argv[argc] = pcmd;
            argc ++;
            pcmd = strtok(NULL, " ");
        }
        if(argc == 1)
        {
            int len = strlen(argv[0]);
            *(argv[0] + len -1) = '\0';
        }
        tDataNode *pn = (tDataNode*)SearchLinkTableNode(head, SearchCondition, (void*)argv[0]);
        if(pn == NULL)
        {
            printf("This is a wrong cmd!\n");
            continue;
        }
        if(pn->handler != NULL)
        {
            pn->handler(argc, argv);
        }
    }
    return 0;
}
  • test.c作为测试模块,实现了菜单的添加和运行。
#include <stdio.h>
#include <stdlib.h>
#include "linktable.h"
#include "menu.h"

int Exit(int argc, char *argv[])
{
    printf("Bye-bye\n");
    exit(0);
}
int Version(int argc, char *argv[])
{
    printf("menu program v3.0\n");
}

int main(int argc, char *argv[])
{
    MenuConfig("version", "menu program v3.0", Version);
    MenuConfig("exit", "Exit this program", Exit);
    ExecuteMenu();
    return 0;
}

  • 通过getopt函数 int getopt(int argc,char const argv[ ],const char optstring);可以对optstring所指的字符串作为短参数列表进行带参数的不同输出。当我们输入测试命令“test -a”时,将输出“aa”;当我们输入测试命令“test -b”时,将输出“bb”


int testOpt(int argc, char** argv)  
{  
    const char * optString = "abc";  
    opterr = 0;  
    int opt;  
    while((opt = getopt(argc, argv, optString)) != -1)  
    {  
        switch(opt)  
        {  
            case 'a':  
                printf("aa\n");  
                break;  
            case 'b':  
                printf("bb\n");  
                break;  
            default:  
                break;  
        }  
    }  
    optind = 0;   
    return 0;  
}

  • Makefile的实现
    这里写图片描述

运行结果

这里写图片描述


复审代码

从github下载代码重新编译运行
这里写图片描述


实验小结

本次实验通过对menu.c的修改,使得接口更加通用。同时Makefile文件降低了使用者的操作复杂度,只需makemake clean指令即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值