readline 命令行程序

一、readline简介

readline 是一个强大的库,只要使用了它的程序,都可以用同一个配置文件配置,而且用同样的方法操作命令行,让你可以方便的编辑命令行。

readline 方法:

从 命令行中获取一整行(一直到换行符,但不包括换行符),并返回由此得到的字符串。

 

二、示例代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>


#define READLINE_PROMPT "command->"
#define MAX_COMMAND_LINE_LENGTH 1024

int print_help()
{
	printf("option command... \n\
				              \n");
	return 0;
}

int is_command_line(char *command)
{
    char *p = command;
    
    if (p == NULL)
    {
        return 0;
    }

    while (*p == ' ' || *p == '\t' || *p == '\n')
    {
        p++;
    }

    if (*p == '#' || *p == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int command_handler(char *pCommandLine)
{
    int Ret = 0;
    char Param[20][30];
    char *pToken = NULL;
    int i =0;
    int Address = 0;
    int Value = 0;

    if (0 == is_command_line(pCommandLine))
    {
        printf("invalid command or parameter\n ");
        print_help();
        return 1;
    }

    pToken = strtok(pCommandLine, " \t\n\r");
	for(i =0; pToken != NULL&& i<16;i++ )
    {
  		if((strlen(pToken) > 28) || (*pToken == '\0'))
       	{
    	    printf("invalid command or parameter\n ");
            print_help();
            return 0;
		}
		strcpy(Param[i],pToken);
  	 	pToken = strtok(NULL," \t\n\r");
  	}

	if(0 == strcmp(Param[0],"read"))
	{
        if(0 == strcmp(Param[1],"\0"))
        {
            printf("invalid conmmand or parameter\n");
            return -1;
        }

        Address = strtol(Param[1],NULL,16);
        Ret = xxxx_read(Address,&Value);
        if(Ret != 0)
        {
            printf("xxxx read eroor\n");
            return -1;
        }
        printf("xxxx read 0x%x , 0x%x\n", Address, Value);
	}
    else if(0 == strcmp(Param[0],"q"))
    {
        exit(0);
    }
    else
    {
	    printf("invalid command or parameter\n ");
        print_help();
        return -1;
    }
    return 0;
}

int main()
{
    char *pReadLine = NULL;
    char CommandLine[MAX_COMMAND_LINE_LENGTH] = {0};
    int Ret = -1;

    print_help();

    while(1)
    {
        pReadLine = readline(READLINE_PROMPT);
        if (pReadLine && *pReadLine && strlen(pReadLine) < MAX_COMMAND_LINE_LENGTH - 1)
        {
            strcpy(CommandLine, pReadLine);
        }
        else if (strlen(pReadLine) >= MAX_COMMAND_LINE_LENGTH - 1)
        {
            printf("   -- Error: Too long command line!\n");
            continue;
        }

        Ret = command_handler(CommandLine);
        if (0 == Ret)
        {
            add_history(pReadLine);

            if (pReadLine != NULL)
            {
                free(pReadLine);
                pReadLine = NULL;
                CommandLine[0] = 0;
            }
        }
    }

    return 0;
}






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值