如何创建自己的shell和命令

时间:2014-06-08 16:12:39

标签: c macos shell command vte

大家好我已经在问问题了......再次:)。我使用示例代码使用vte创建自定义shell。我有几个问题。我首先使用的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gtn.h"





    int cmpstr(const void *vs1, const void *vs2)
        {
            char * const *s1 = vs1;
            char * const *s2 = vs2;
            return strcmp(*s1, *s2);
        }




    int rjhchdir(char *args)
        {

            if(args!= NULL)
            {
                printf("hello %s.\n", args);
            }

            else
            {
                printf("You changed to your home directory.\n");
            }
            return 0;
        }



    int rjhclear(void)
        {
            printf("You cleared the display.\n");
            return 0;
        }




    int rjhdf()
    {   

        printf("You asked for disk usage info.\n");
        return 0;
    }



    int rjhdu(char *args)
        {
            printf("You asked for directory usage info.\n");
            printf("hello %s.\n", args);

            if(args != NULL)
            {
                printf("Specifically, you wanted the option %s\n", args);
            }

            return 0;
        }




        int rjhls(char *args)
        {
            printf("You asked for a directory listing.\n");

            if(args != NULL)
            {
                printf("Options: %s\n", args);
            }
            return 0;
        }




        int rjhmkdir(char *args)
        {


            if(args != NULL)
            {
                printf("You created a directory, %s\n", args);
            }

            else
            {
                printf("You have to say what "
                    "directory you want to create.\n");
            }
            return 0;
        }





        int rjhsu(char *args)
        {
            int a=0;
            if(args != NULL)
            {
                a=atoi(args);
                //printf("You switched user to holaaa %s %d\n", args, a);
                graphtn(a); 
            }

            else
            {
                printf("You must enter wich termocouple wou want to visualize.\n");
            }
            return 0;
        }




int main(void)
{
/* ensure this list is sorted! */
    char *command[] ={"cd","clear","df","du","exit","ls","mkdir","su",};

size_t numcommands = sizeof command / sizeof command[0];
char line[4096] = {0};
char safeline[4096] = {0};
int done = 0;
int i = 0;
char *s = line;
char **t = NULL;
char *prompt = ">";
char *args = NULL;
char *nl = NULL;

    while(!done)
    {
        printf("%s", prompt);
        fflush(stdout);

        if(NULL == fgets(line, sizeof line, stdin))
        {
            t = NULL;
            done = 1;
        }

        else
        {
            nl = strchr(line, '\n');

            if(nl != NULL)
            {
                *nl = '\0';
                strcpy(safeline, line);
            }

            else
            {
                int ch;
                printf("Line too long! Ignored.\n");

                while((ch = getchar()) != '\n' && ch != EOF)
                {
                    continue;
                }

                if(ch == EOF)
                {
                    done = 1;
                }
            }

            args = strchr(line, ' ');

            if(args != NULL)
            {
                *args++ = '\0';

            }

            t = bsearch(&s,
            command,
            numcommands,
            sizeof command[0],cmpstr);
        }


        if(!done && t != NULL)
        {
            i = (int)(t - command);
            switch(i)
            {

                case 0: rjhchdir(args); break;
                case 1: rjhclear(); break;
                case 2: rjhdf(); break;
                case 3: rjhdu(args); break;
                case 4: done = 1; break;
                case 5: rjhls(args); break;
                case 6: rjhmkdir(args); break;
                case 7: rjhsu(args); break;
                default: break;
            }
        }

        else
        {
            printf("%s is not an internal command. ""Trying to execute it instead.\n", line);
            system(safeline);
        }
    }

return 0;
}

此时我知道每个案例都会使用列出的每个功能。所以我可以使用每个函数来运行我自己的程序(我的兽医控制台内的命令)。我使用的示例代码有最常见的命令,如su,cd,mkdir等。我想更改所用命令的名称,然后在函数,swicht和定义处重写名称。

示例:

int rjhsu(char *args) 

int rjhgraph(char *args)

char *command[] ={"cd","clear","df","du","exit","ls","mkdir","su",};

char *command[] ={"cd","clear","df","du","exit","ls","mkdir","graph",};

case 7: rjhsu(args); break;

case 7: rjhgraph(args); break;

但是,如果我这样做,我会得到else语句

printf("%s is not an internal command. ""Trying to execute it instead.\n", line);

我想了一点,我试图在/ usr / bin创建一个名为graph的命令,但它根本不起作用。我还是很困惑,有什么建议吗?

0 个答案:

没有答案