⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sh.y.svn-base

📁 大学操作系统课程实验:Linux下实现的一个shell解释器。能够比较完整完好地执行Linux下的普通命令、重定向命令、管道命令等多种命令。
💻 SVN-BASE
字号:
%{#include <stdio.h>#include <ctype.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include "global.h"#define LEN 20%}%union{char *sym;}%token String%right '&'%type <sym> String ExecFile%type <sym> '&'%%CommandLine : //NULL           |FgCommandLine                  {reset();}	   |FgCommandLine '&'             {reset();is_back=1;}               	         ;FgCommandLine :   SimpleCommand            {storesimple();}                   |FirstCommand MidCommand LastCommand   {cmd_flag=PIPE_CMD;resettoppipe();}                         	;SimpleCommand:    ProgInvocation  InputRedirect OutputRedirect   {};FirstCommand :    ProgInvocation  InputRedirect  {storepipe();reset();};MidCommand:     //NULL                |MidCommand '|' ProgInvocation   {storepipe();reset();}                   ;LastCommand:    '|'ProgInvocation  OutputRedirect   {storepipe();reset();};ProgInvocation :  ExecFile Args   {};InputRedirect:  //NULL                    |'<'String  {pushio();storefile();cmd_flag=REDIRECT_CMD;io_flag=FD_READ;};OutputRedirect: //NULL                     |'>'String {pushio();storefile();cmd_flag=REDIRECT_CMD;io_flag=FD_WRITE;};ExecFile:	String                   {pushcmd();}	  ;Args:    //NULL	|  Args String                   {pushpara();}	;%%char *backjobs="&";char *pipejobs="|";char *injobs="<";char *outjobs=">";char *stackpara[LEN];char *stackcmd[LEN];char *stackfile[LEN];char buf[100];char *p = buf;int lineno = 0;int topcmd = 0;int toppara=0;int topfile=0;int toppipe=0;    /**********************************************************************************************************/ yylex(){	int c;	p = buf;	while((c = getchar()) == ' ' || c == '\t');	if(c == '\n'){	return 0;                }	if(c != '\n') {		do {			*p++ = c;			c = getchar();		}while(c != ' ' && c != '\t' && c != '\n');		ungetc(c,stdin);		*p = '\0';		yylval.sym = buf;		if(!strcmp(buf,backjobs))			return '&';                else if(!strcmp(buf,injobs))                        return '<';                else if(!strcmp(buf,outjobs))                        return '>';                else if(!strcmp(buf,pipejobs))                        return '|'; 		else			return String;	} else {		lineno ++;	}	return c;}    /**********************************************************************************************************/ yyerror(char *s){	warning(s, (char *)0);}    /**********************************************************************************************************/ warning(char *s,char *t){	}    /**********************************************************************************************************/ reset(){	int i;	for(i = 0; i < topcmd; i++) {		free(stackcmd[i]);		stackcmd[i] = NULL;	}	topcmd = 0;	for(i = 0; i < toppara; i++) {		free(stackpara[i]);		stackpara[i] = NULL;	}	toppara=0;	for(i=0;i<topfile;i++){	    free(stackfile[i]);	    stackfile[i]=NULL;	    }	topfile=0;}    /**********************************************************************************************************/ pushcmd(){	char *temp = NULL;	if(topcmd == 0)		temp = (char *)malloc(sizeof(char) * 10);	else		temp = (char *)malloc(strlen(buf) + 1);	strcpy(temp,buf);	stackcmd[topcmd] = temp;	topcmd++;}    /**********************************************************************************************************/ pushpara(){	char *temp = NULL;	if(toppara == 0)		temp = (char *)malloc(sizeof(char) * 30);	else		temp = (char *)malloc(strlen(buf) + 1);	strcpy(temp,buf);	stackpara[toppara] = temp;        toppara++;}    /**********************************************************************************************************/ pushio(){	char *temp = NULL;	if(topfile == 0)		temp = (char *)malloc(sizeof(char) * 30);	else		temp = (char *)malloc(strlen(buf) + 1);	strcpy(temp,buf);	stackfile[topfile] = temp;        	topfile++;}    /**********************************************************************************************************/ storesimple(){	int i=0;        if(topcmd)        {	   cmdStr.cmd=(char *)malloc(sizeof(char) * 10);	   strcpy(cmdStr.cmd,stackcmd[0]);		}        for(i=0;i<toppara;i++){	cmdStr.para[i]=(char *)malloc(sizeof(char) * 10);	strcpy(cmdStr.para[i],stackpara[i]);        cmdStr.para[i+1]=NULL;	}}/**********************************************************************************************************/storefile(){    cmdStr.filename=(char*)malloc(sizeof(char)*50);    strcpy(cmdStr.filename,stackfile[0]);    }        /**********************************************************************************************************/        storepipe(){    int i=0;    if(topcmd)    {         pipeStr[toppipe].cmd=(char *)malloc(sizeof(char) * 10);       strcpy(pipeStr[toppipe].cmd,stackcmd[0]);      }        for(i=0;i<toppara;i++){        pipeStr[toppipe].para[i]=(char *)malloc(sizeof(char) * 10);        strcpy(pipeStr[toppipe].para[i],stackpara[i]);    }     if(stackfile[0]!=NULL){        pipeStr[toppipe].filename=(char *)malloc(sizeof(char) * 50);        strcpy(pipeStr[toppipe].filename,stackfile[0]);    }    pipeStr[toppipe].io_flag = io_flag;    io_flag = NO_IO;    toppipe++;    pipe_len=toppipe;}/****************************************************************************************************************/resettoppipe(){toppipe=0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -