-
Notifications
You must be signed in to change notification settings - Fork 0
/
advshell.cpp
147 lines (138 loc) · 2.95 KB
/
advshell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<iostream>
#include<bits/stdc++.h>
#include<fstream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include "header.h"
#define argl 1024
#define cmdl 100
#define mx 100
#define ml 1024
char cdpath[mx];
using namespace std;
map<string, string> ms;
/*void showPrompt(){
char *cwd = (char *)malloc(mx*sizeof(char));
char hostname[mx];
cout<<getenv("USER")<<"@";
gethostname(hostname,mx);
cout<<hostname<<getcwd(cwd,mx)<<":~"<<getenv("PS1")<<"$ ";
}*/
int main(){
int i,count=0;
//setting the path for c - comand
strcpy(cdpath,getenv("PWD"));
//setitng the environment variables for my shell
//char* st[]={"myrc",NULL};
char* st[2];st[0]="myrc";st[1]=NULL;
bashrc(st);
while(1){
i=0;
showPrompt();
char *st[argl];
int rflag = 0,pflag = 0;
char c;// = getchar();
string temp = "";
struct termios termios_p;
//int rtv = tcgetattr(0, termios_p);
//cout<<rtv<<endl;
do{
c = getchar();
if(c != ' ' && c != '\t' && c != '\n'){
temp+=string(1,c);
//cout<<temp<<endl;
}
else if(c == ' '){
//cout<<"space handle"<<endl;
char *cmd = (char *)malloc(temp.length()*sizeof(char));
strcpy(cmd,temp.c_str());
st[i] = cmd;
//printf("%s \n",st[i]);
temp = "";
i++;
}
else if(c == '\n'){
//cout<<"new line handle"<<endl;
if(temp.length() != 0){
char *cmd = (char *)malloc(temp.length()*sizeof(char));
strcpy(cmd,temp.c_str());
st[i] = cmd;
//printf("%s \n",st[i]);
temp = "";
i++;
}
break;
}
if(c == '>'){
rflag = 1;
}
if(c == '|'){
pflag = 1;
}
}while(1);
if(i>0){
st[i]=NULL;
count = i;
i = 0;
//condition to exit from the shell
if(!strcmp(st[0],"exit")){
cout<<"Bye !"<<endl;
exit(0);
}
//checking for change directory
if(!strcmp(st[0],"cd")){
if(count>2){
cout<<"too many arguments"<<endl;
}
else{
changeDirectory(st);
}
continue;
}
//checking for alias and setting
if(!strcmp(st[0],"alias")){
if(count>2){
cout<<"too many arguments"<<endl;
}
else{
setAlias(st);
}
continue;
}
//finding alias
/*int chkal = 0;
while(st[chkal]){
st[chkal] = findAlias(st);
chkal++;
}*/
int pid = fork();
if(pid){
int wc = wait(&pid);
}
else{
if(rflag == 1 && pflag == 1){
piping(st,rflag,pflag);
}
else if(rflag == 0 && pflag == 1){
piping(st,rflag,pflag);
}
else if(rflag == 1 && pflag == 0){
redirection(st,rflag,pflag);
}
else{
if(execvp(st[0],st)<0){
//cout<<"error: please check "<<endl;
}
exit(0);
}
}
}
}
}