linux网络编程(2)

3.2.4简单命令处理程序

inarg.c

#include"smallsh.h"
static char special[]={' ','\t','*',';','\n','\0'};
inarg(char c)
{
    char *wrk;
    for(wrk=special;*wrk!='\0';wrk++)
        if(c==*wrk)
            return(0);
    return (1);
}

procline.c

#include "smallsh.h"
procline()
{
    char *arg[MAXARG+1];
    int  toktype;
    int  narg;
    int type;
    for(narg=0;;)
    {
        switch(toktype=gettok(&arg[narg]))
        {
            case ARG:
                if (narg<MAXARG)
                    narg++;
                break;
            case EOL:
            case SEMICOLON:
            case AMPERSAND:
                type=(toktype==AMPERSAND)?
                BACKGROUND:FOREGROUND;
                if (narg!=0)
                {
                    arg[narg]=NULL;
                    runcommand(arg,type);
                }
                if (toktype==EOL)
                    narg=0;
                break;
        }
    }
}

runcommand.c

#include "smallsh.h"
runcommand(char**  cline,int where)
{
    int  pid,exitstat,ret;
    if((pid=fork())<0) 
    {
        perror("fork fail");
        return(-1);
    }
    if (!pid) 
    {     /*  子进程代码*/
        execvp(*cline,cline);
        perror(*cline);
        exit(127);
    }
        /*  父进程代码*/
        /*  后台进程代码*/
    if (where==BACKGROUND)
    {
        printf("[process id %d]\n",pid);
        return(0);
    }    
        /*  前台进程代码*/
    while ((ret=wait(&exitstat))!=pid && ret !=-1) ;
    return (ret==-1?-1:exitstat);
}

smallsh.c

#include"smallsh.h"
char *prompt="command>";

main()
{
    while(userin(prompt)!=EOF)
        procline();
}

smallsh.h

#include<stdio.h>

#define EOL 1 /*行结束*/
#define ARG 2
#define AMPERSAND 3
#define SEMICOLON 4
#define MAXARG 512 /*输入命令行参数的最大值*/
#define MAXBUF 512 /*输入行的最大长度*/
#define FOREGROUND 0
#define BACKGROUND 1

userin.c

#include "smallsh.h"
/*  程序缓冲区和指针*/
static char inpbuf[MAXBUF];
static char tokbuf[2*MAXBUF];
static char *ptr =inpbuf,*tok=tokbuf;
/*  userin()函数*/
int  userin(char* p)
{
    int  c,count;
    ptr=inpbuf;
    tok=tokbuf;
    /*  显示提示*/
    printf("%s ",p);
    for (count=0;;) 
    {
        if ((c=getchar())==EOF)
        return(EOF);
        if (count<MAXBUF)
            inpbuf[count++]=c;
        if (c =='\n' && count <MAXBUF)
        {
            inpbuf[count]='\0';
            return(count);
        }    
        /*  如果行过长重新输入*/
        if (c=='\n') 
        {
            printf(" smallsh:input line too long\n");
            count=0;
            printf("%s ",p);
        }
    }
}
gettok(char* output)
{
    int type;
    output=tok;
    /*  首先去除空白字符*/
    for (; *ptr == ' ' || *ptr == '\t';ptr++);
    *tok++ = *ptr;
    switch(*ptr ++)
    {
        case '\n':
            type=EOL;break;
        case '&':
            type=AMPERSAND;break;
        case ';':
            type=SEMICOLON;break;
        default:
            type = ARG;
        while (inarg(*ptr))
            *tok++ = *ptr++;
    }
    *tok++='\0';
    return (type);
}

smallsh程序的运行方法(也可以写成一个makefile文件)

posted @ 2015-11-25 09:47  剑风云  阅读(153)  评论(0编辑  收藏  举报