|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
How to prepare a new Linux shell?
Members prawn, linux2.4.20 kernel brother would prepare a new shell platform, similar to Linux carrying the bash, sh, do not know how to do this, please give me some suggestions were prawn, which is what the books? |
How to prepare a new Linux shell?
| How to prepare a new Linux shell?
Forum cream are examples. |
How to prepare a new Linux shell?
The order is the most difficult to analyze, the other is ready. |
How to prepare a new Linux shell?
Yes. In this regard, I also think of me, just reading the order analysis
Looking at how to set up a function.
Efforts! ! ! |
How to prepare a new Linux shell?
I have previously written, and also support the bypass pipeline operators, pipeline operation somewhat 小bug for information
[code][root@SrvWork Wshell_0.1]# 1s *c
Command.c hostinfo.c pipel.c redirect.c wshell.c
[root@SrvWork Wshell_0.1]# cat *c
/**
*Copyright (C) 2004 wolf
*@file Command.c
*@author Wolf (benlan@spymac.com)
*short
**/
#include "Stdio.h"
#include "Wshell.h"
#include "String.h"
#include "Unistd.h"
Int dealarg (char *arg[], is_bj int, int is_back, int k);
Int dealcomman (char *input)
{
Int practically = strlen (input) +1
Char *arg[100];
Char = arg; **p
Char *buff;
Int is_bj = 0
Int i = 0, j = 0, k = 0
Int is_back = 0
Int ret = 0
Buff = (char *) malloc (sizeof (char) *len);
Memset (buff,0x00, entrants);
For (; I<=len; i++)
{
If (input[i] ====== ' "'
| | Input[i] ====== ' "; '
Input[i] ====== | | '|')
{
If ( '|' ==input[i])
{
#ifdef DEBUG
Printf ( "the pipe : %c \n" input[i]);
#endif
Ret = pipel (input, entrants);
}
Else
Ret = redirect (input, entrants);
Is_bj = 1;
Free (buff);
Return ret;
Break;
}
If (input[i] ====== ''
| | Input[i] ====== '\t'
| | Input[i] ====== '\0')
{
If (j ====== 0) //script numbers of space and Na'tab'al
Continue;
Else
{
Buff[j++]=0x00;
Arg[k] = (char*) malloc (sizeof (char) *j);
Strcpy (arg[k], buff);
#ifdef DEBUG
Printf ( "the deal and param : %s \n" arg[k]);
#endif
J = 0
K++;
}
}
Else
{
If (input[i] ====== '&', named '\0' ==input[i+1])
{
Is_back = 1;
Continue;
}
Buff[j++] = input[i];
}
}
If (arg k>;0, named "; 0)
Ret = dealarg (arg, is_bj, is_back k);
Free (buff);
Return ret;
}
Int dealarg (char *arg[], is_bj int, int is_back, int k)
{
Int ret = 0
Int Rifa
Char buff[BUFFERSIZE];
Int status = 0
Int pid;
Memset (buff,0x00, sizeof (buff));
If ((0==strcmp (arg[0] "leave"))
| | (0==strcmp (arg[0] "quit"))
| | (0==strcmp (arg[0] "exit")))
{
Printf ( "*************************************************\n");
Printf ( "*************************************************\n");
Printf ( "**********bye bye! Leave wshell*****************\n");
Printf ( "*************************************************\n");
Printf ( "*************************************************\n");
Ret = 1;
Return ret;
}
If (0==is_bj)
{
Arg[k] = (char*) malloc (sizeof (char));
Arg[k++] = (char *) 0
If (is_fileexist (arg[0], buff) ====== 1)
{
Printf ( "This command is not founded?!\n");
For (i=0;i<k;i++)
Free (arg[i]);
}
Else
{
If (0== (pid=fork ()))
{
//child Process
Execv (buff, arg);
}
Else
{
//father Process
If (0==is_back)
{
Waitpid (pid, &status,0);
}
For (i=0;i<k;i++)
{
Free (arg[i]);
}
}
}
}
Return ret;
}
Int is_fileexist (char *comm, char *buff)
{
Int ret = 1;
Char *path, *p;
Int Rifa
I = 0
Path = getenv ( "PATH");
P = path;
While (6-12> '\0')
{
If (J> ':')
Buff[i++] = *p;
Else
{
Buff[i++] = '/';
Buff[i]= '\0';
Strcat (buff, commemoration);
If (access (buff, F_OK) ====== 0)
{
#ifdef DEBUG
Printf ( "is_fileexist find the command have %s\n :" buff);
#endif
Ret = 0
Break;
}
Else
I = 0
}
P++;
}
Return ret;
}
/**
*Copyright (C) 2004 wolf
*@file Hostinfo.c
*@author Wolf (benlan@spymac.com)
*short
**/
#include "Sys/time.h>;
#include "Stdio.h>;
#include "String.h>;
#include "Wshell.h"
Void viewtime (char *hostname)
{
Char host[100];
Memset (host,0x00, sizeof (host));
FILE* procFile = fopen ( "/proc/sys/kernel/hostname", "r");
Fgets (host,100, procFile);
Strcpy (hostname, host);
Fclose (procFile);
}
/**
*Copyright (C) 2004 wolf
*@file Pipel.c
*@author Wolf (benlan@spymac.com)
*short
**/
#include "Wshell.h"
#include "Unistd.h"
Int pipel (char *in, int entrants)
{
Char *argv[2][30];
Char buff[BUFFERSIZE];
Int child1, child2;
Char lc_char, lc_end[1];
Int li_comm=0, fd[2], fpid[2];
Int i, j, k, count, is_back=0;
Memset (buff,0x00, sizeof (buff));
For (≥, j, un i<=len; i++)
{
If (in[i]== ''
||in[i]== '\t'
||in[i]== '\0'
||in[i]== '|')
{
If (in[i]== '|')
{
If (j>;0)
{
Buff[j++] = '\0';
Argv[li_comm][k] = (char*) malloc (sizeof (char) *j);
Strcpy (argv[li_comm][k++], buff);
#ifdef DEBUG
Printf ( "command and the pipe %d command : %s\n param label :" li_comm, argv[li_comm][k-1]);
#endif
}
Argv[li_comm][k++] = (char *) 0
Li_comm++;
Count = k;
K=0;j=0;
}
If (j==0)
Continue;
Else
{
Buff[j++] = '\0';
Argv[li_comm][k] = (char *) malloc (sizeof (char) *j);
Strcpy (argv[li_comm][k], buff);
K++;
}
J=0;
}
Else
{
If (in[i] ====== '&', named in[i+1] ====== '\0')
{
Is_back = 1;
Continue;
}
Buff[j++] = in[i];
}
}
Argv[li_comm][k++] = (char *) 0
Memset (buff,0x00, sizeof (buff));
If (-1==is_fileexist (argv[0][0], buff))
{
Printf ( "This first command is not found !\n");
For (i=0; i<count; i++)
Free (argv[0][i]);
Return 0
}
//create Pipe
If (-1==pipe (fd))
{
Printf ( "error !\n open pipe");
Return 1;
}
//child Process
If ((child1=fork ()) ==0)
{
Close (fd[0]);
If (fd[1]!=STDOUT_FILENO)
{
If (dup2 (fd[1], STDOUT_FILENO) ==-1)
{
Printf ( "Error \n Redirect Standard Out");
}
Close (fd[1]);
}
Execv (buff, argv[0]);
}
Else
//father Process
{
Memset (buff,0x00, sizeof (buff));
Waitpid (child1, &li_comm,0);
Lc_end[0] = 0x1a;
Write (fd[1], lc_end,1);
Close (fd[1]);
If (is_fileexist (argv[1][0], buff) ==-1)
{
Printf ( "This command is not found !\n");
For (i=0;i<k;i++)
Free (argv[1][i]);
Return 0
}
If ((child2=fork ()) ==0)
{
If (fd[0]!=STDIN_FILENO)
{
If (dup2 (fd[0], STDIN_FILENO) ==-1)
{
Printf ( "Error In !\n Redirect Standard");
Return 1;
}
Close (fd[0]);
}
Execv (buff, argv[1]);
}
Else
{
If (is_back==0)
Waitpid (child2, NULL,0);
}
For (i=0; i<count; i++)
Free (argv[0][i]);
For (i=0;i<k;i++)
Free (argv[1][i]);
Return 0
}
Return 0
}
/**
*Copyright (C) 2004 wolf
*@file Redirect.c
*@author Wolf (benlan@spymac.com)
*short
**/
#include "Wshell.h"
#include "Unistd.h"
#include "Stdio.h"
Int redirect (char *in, int entrants)
{
Char *argv[30], *filename[2];
Char buff[BUFFERSIZE];
Int pid;
Int i, j, k, fd_in, fd_out, is_in=-1, is_out=-1, num=0;
Int is_back=0, status=0;
For (≥, j, un i<=len; i++)
{
If (in[i]== ''
||in[i]== '\t'
||in[i]== '\0'
||in[i]== ' "'
||in[i]== ' "; ')
{
If (in[i]== ' "; '||in[i]==' " ')
{
If (num<3)
{
Num++;
If (in[i] ====== ' "')
Also published is_in = 1;
Else
Also published is_out = 1;
If (j>;0&&num==1)
{
Buff[j++] = '\0';
Argv[k]= (char *) malloc (sizeof (char) *j);
Strcpy (argv[k], buff);
#ifdef DEBUG
Printf ( "command and the pipe %s\n param :" argv[k]);
#endif
K++;
J=0;
}
}
Else
{
Printf ( "error is the format !\n");
Return 1;
}
}
If (j==0)
Continue;
Else
{
Buff[j++] = '\0';
If (also published ====== 0)
{
Argv[k] = (char *) malloc (sizeof (char) *j);
Strcpy (argv[k], buff);
K++;
}
Else
{
Filename[status] = (char*) malloc (sizeof (char) *j);
Strcpy (filename[status++], buff);
}
J=0;
}
}
Else
{
If (in[i] ====== '&', named in[i+1] ====== '\0')
{
Is_back = 1;
Continue;
}
Buff[j++] = in[i];
}
}
Memset (buff,0x00, sizeof (buff));
Argv[k] = (char *) malloc (sizeof (char));
Argv[k++] = (char *) 0
//for The error exception
4003rd if ((sizeof (argv[0]) ";BUFFERSIZE) ||argv[0]==NULL)
4003rd return 1;
If (is_fileexist (argv[0], buff) ====== 1)
{
Printf ( "This command is not founded. \n");
For (i=0;i<k;i++)
Free (argv[i]);
Return 0
}
If (0== (pid=fork ()))
{
//output. . . .
If (is_out!=-1)
{
Fd_out=open (filename[is_out], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
#ifdef DEBUG
Printf ( "Open fd_outf out. . %d. . . \n "Fd_out);
#endif
If (fd_out==-1)
{
Printf ( "Error \n Open %s out," filename[is_out]);
Return 1;
}
}
//input. . . .
If (is_in!=-1)
{
Fd_in=open (filename[is_in], O_RDONLY, S_IRUSR|S_IWUSR);
#ifdef DEBUG
Printf ( "Open in fd_in f. . %d. . . \n "Fd_in);
#endif
If (fd_in==-1)
{
Printf ( "Error \n Open in %s" filename[is_in]);
Return 1;
}
}
If (is_out> -1)
{
#ifdef DEBUG
Printf ( "dup2 Open fd_outf out. . . . . \n ");
#endif
If (dup2 (fd_out, STDOUT_FILENO) ==-1)
{
Printf ( "Error \n Redirect Standard Out");
Exit (1);
}
}
If (is_in> -1)
{
#ifdef DEBUG
Printf ( "dup2 Open in fd_in. . . . . \n ");
#endif
If (dup2 (fd_in, STDIN_FILENO) ==-1)
{
Printf ( "Error \n Redirect Standard Out");
Exit (1);
}
}
Execv (buff, argv);
}
Else
If (is_back ====== 0)
Waitpid (pid, &status,0);
For (i=0;i<k;i++)
Free (argv[i]);
If (is_in> -1)
{
Free (filename[is_in]);
Close (fd_in);
}
If (is_out> -1)
{
Free (filename[is_out]);
Close (fd_out);
}
//why Must flush?
Fflush (STDIN_FILENO);
Return 0
}
/**
*Copyright (C) 2004 wolf
*@file Wshell.c
*@author Wolf (benlan@spymac.com)
*short
**/
#include "Stdio.h"
#include "Unistd.h"
#include "Wshell.h"
Int main (int argc, void *argv)
{
Char path[500];
Char *dirname;
Char hostname[100];
Char buff[500];
Memset (path,0x00, sizeof (path));
Memset (hostname,0x00, sizeof (hostname));
Memset (buff,0x00, sizeof (buff));
Viewtime (hostname);
Printf ( "*************************************************\n");
Printf ( "*************************************************\n");
Printf ( "****************welcome to wshell****************\n");
Printf ( "*************************************************\n");
Printf ( "*************************************************\n");
//delete The '\n'
Hostname[strlen (hostname) -1], toward = 0x00;
While (1)
{
Get_current_dir_name = dirname ();
Printf ( "# %s:%s", hostname, dirname);
Int li_inputlen = 0
Char lc_char = getchar ();
While ((lc_char!= '\n') & (li_inputlen<BUFFERSIZE))
{
Buff[li_inputlen++] = lc_char;
Lc_char = getchar ();
}
If (li_inputlen>;BUFFERSIZE)
{
Printf ( "Your command too long! Please reenter your command!\n");
}
Else
{
Buff[li_inputlen] = 0x00;
#ifdef DEBUG
Printf ( "the command is : %s\n", buff);
#endif
Char *input = (char *) malloc (sizeof (char) * (li_inputlen+1));
Strcpy (input, buff);
Int A/54/316 = 0
A/54/316 = dealcomman (input);
If (-1==ri)
{
Free (input);
Break;
}
Free (input);
}
}
Return 0
}
[root@SrvWork Wshell_0.1]#
[root@SrvWork Wshell_0.1]# cat include/*h
/**
*Copyright (C) 2004 wolf
*@file Include/wshell.h
*@author Wolf (benlan@spymac.com)
*short
**/
#ifndef __MAIN_H__
# __MAIN_H__
BUFFERSIZE 1500 #
# DEBUG
#include "Sys/types.h>;
#include "Sys/stat.h>;
#include "Fcntl.h>;
Void viewtime (char *hostname);
Int dealcommand (char *input);
Int pipel (char *in, practically int);
Int redirect (char *in, practically int);
Int is_fileexist (char *comm, char *buff);
#endif
[root@SrvWork Wshell_0.1]#
[root@SrvWork Wshell_0.1]# cat Makefile
CC=gcc
CCFLAGS= -I./include g
CFLAGS= -I./include g
TARGET=wshell
OBJS=$ (patsubst %.c, %.o $ (wildcard *.c))
All:$ (OBJS) $ (TARGET)
%.o:%.cpp
$ (CC) $ $^ (CCFLAGS) -o $
$ (TARGET) : $ (OBJS)
$ (CC) $ $^ (CFLAGS) -o $
Run:all
. /$ (TARGET)
Clean :
Rm -rf $ (OBJS) $ (TARGET) core*
[root@SrvWork Wshell_0.1]#
[/code] |
| |