Simple emulation of command-line bash functionality

Source: Internet
Author: User

With Linux for a while, and consistently enter commands under bash but I've always wondered how this bash knows what commands I want to enter, so I use my knowledge to temporarily imitate some bash features and continue to refine the functionality.

First time Version:

/**********************************************************************   * Copyright (c) 2015,wk Studios * Filename:   main.c * compiler:gcc,vs,vc6.0  Win32   * AUTHOR:WK   * time:2015 29 4 ************************* /#include <stdio.h> #include <stdlib.h> #include < string.h> int main () {char cmd[128]={0}; while (1) {printf ("Input command:"); scanf ("%s", cmd); if (0==strcmp (cmd, "help")) {printf ("command\n");} else if (0== (cmd, "Quit") &&strcmp (cmd, "q" )) {exit (0);} else {printf ("Command not found\n");}}}

Second version:

/**********************************************************************   * Copyright (c) 2015,wk Studios * Filename:   bash.h * compiler:gcc,vs,vc6.0  Win32   * AUTHOR:WK   * time:2015 29 4 ************************* /#include <stdio.h> #include <stdlib.h> #include < string.h> #ifndef _bash_h#define _bash_h#define cmd_max_len 128#define desc_len  1024#define CMD_NUM   10int Help (); int quit (); int show (); typedef struct datanode{    char *cmd;    char *desc;    Int (*handler) ();//function pointer    struct DataNode *next;} tdatanode;//  T indicates that typedef defines a static Tdatanode head[]=//linked list array {    {"Help", "This is help cmd", help,&head[1]},    {"Version", "menu Program v1.0", Null,&head[2]},    {"Quit", "This is quit cmd", quit,null}//can add commands to the array, here are only a few commands};tdatanode* find (tdatanode* head,char *cmd); #endif

/**********************************************************************   * Copyright (c) 2015,wk Studios * Filename:   main.c * compiler:gcc,vs,vc6.0  Win32   * AUTHOR:WK   * time:2015 29 4 ************************* /#include "bash.h" tdatanode*  find (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 Show (Tdatanode *head) {tdatanode *p=head;printf ("menu list:\n"), while (P!=null) {printf ("%s-%s\n", p->cmd,p- >DESC);p =p->next;} return 1;} int Help ()//Print all commands {    show (head); return 1;} int quit () {exit (0);}

/**********************************************************************   * Copyright (c) 2015,wk Studios * Filename:   main.c * compiler:gcc,vs,vc6.0  Win32   * AUTHOR:WK   * time:2015 29 4 ************************* /#include "bash,h" int main () {while (1) {    char cmd[cmd_max_len]; printf ("Input a Command number>"), scanf ("%s", cmd), Tdatanode *p=find (Head,cmd), if (p==null) {printf ("command not Found!\n "); continue;} printf ("%s-%s\n", P->CMD,P->DESC); if (p->handler! = NULL)//a somewhat polymorphic meaning calls the same handler effect differently {            p->handler (); /functiom Point}}return 0;}


Simple emulation of command-line bash functionality

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.