- 论坛徽章:
- 0
|
smallsh.h
- #define ARG 2
- #define AMPERSAND 3
- #define SEMICOLON 4
- #define MAXARG 512
- #define MAXBUF 512
- #define FOREGROUND 0
- #define BACKGROUND 1
- extern char inpbuf[MAXBUF], tokbuf[2*MAXBUF];
- extern char * ptr;
- extern char * tok;
- extern int userin(char *p);
- extern int gettok(char **outptr);
- extern int procline(void);
- extern int inarg(char c);
- extern int runcommand(char **cline,int where);
复制代码
main.c
- #include "smallsh.h"
- char *prompt = "Command>";
- int main(void)
- {
-
- while(userin(prompt) != EOF)
- procline();
- }
复制代码
procline.c
- #include "smallsh.h"
- int procline(void)
- {
- char *arg [MAXARG+1];
- int toktype;
- int narg;
- int type;
-
- narg = 0;
- for(;;)
- {
- switch(toktype = gettok(&arg[narg]))
- {
- case ARG:
- if(narg < MAXARG)
- narg++;
- break;
- case EOL:
- case SEMICOLON:
- case AMPERSAND:
- if(toktype == AMPERSAND)
- type = BACKGROUND;
- else
- type = FOREGROUND;
-
- if(narg != 0)
- {
- arg[narg] = NULL;
- int idx=0;
- for(;arg[idx]!=NULL;printf("[ %s ]\n",arg[idx]),idx++);
-
- runcommand(arg,type);
- }
- if(toktype == EOL)
- return;
- narg = 0;
- break;
- }
- }
- }
复制代码
runcommand.c
- #include "smallsh.h"
- int runcommand(char **cline, int where)
- {
- pid_t pid;
- int status;
-
- switch(pid = fork())
- {
- case -1:
- perror("smallsh");
- return(-1);
- case 0:
- execvp(*cline,cline);
- perror(*cline);
- exit(1);
-
- }
-
- if(where == BACKGROUND)
- {
- printf("[process id %d]\n",pid);
- return(0);
- }
- if(waitpid(pid,&status,0) == -1)
- return (-1);
- else
- return (status);
- }
复制代码
userin.c
- #include "smallsh.h"
- static char inpbuf[MAXBUF],tokbuf[2*MAXBUF],*ptr = inpbuf,*tok = tokbuf;
- int userin(char *p)
- {
- char c;int count;
- ptr=inpbuf;
- tok=tokbuf;
- printf("%s",p);
-
- count = 0;
-
- while(1)
- {
- 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);
- }
- }
- }
- int gettok(char **outptr)
- {
- int type;
- *outptr = tok;
- while(*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;
- }
复制代码
inarg.c
- static char special[] = {' ','\t','&',';','\n','\0'};
- int inarg(char c)
- {
- char *wrk;
- for(wrk = special;*wrk;wrk++)
- {
- if(c == *wrk)
- return(0);
- }
- return (1);
- }
复制代码
makefile
- smallsh: main.o userin.o inarg.o runcommand.o procline.o
- gcc -g main.o userin.o inarg.o runcommand.o procline.o -o smallsh
- main.o:main.c
- gcc -g -c main.c
- userin.o: userin.c
- gcc -g -c userin.c
- runcommand.o: runcommand.c
- gcc -g -c runcommand.c
- procline.o: procline.c
- gcc -g -c procline.c
- inarg.o: inarg.c
- gcc -g -c inarg.c
复制代码
我实在想不通为什么是 tokbuf[2*MAXBUF] 而不是tokbuf[MAXBUF]
在 int gettok(char **outptr)
这个函数中*tok++ =*ptr++ 而且参数char **outptr指向了tok 不管怎么讲tokbuf长度最终最长也就MAXBUF阿
为什么程序里是tokbuf[2*MAXBUF] |
|