forked from NEKTech-Labs/NEKTech-Linux-Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_parser.c
287 lines (257 loc) · 7.3 KB
/
input_parser.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* input_parser.c - Linux Shell
*
* Copyright (C) NEK Tech 2013
* Developers V1.0: Deepika Pandey
* Shubhangi Maheshwari
*
* Copyright (C) NEK Tech 2014
* Developer V2.0: Jitendra Khasdev
* Neha Choudhary
* Vikas Kumar
*
* Author and Architect: Pankaj Saraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms and conditions of NEK Tech.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include <sys/wait.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "nektech_shell.h"
/* Taking User Input and Parsing the input to take action.
* String operations and intelligece for user input.
* Developer: Deepika Pandey
* Provide suppoert for cd command when no argument pass.
* then cd cammand redirect automatically user's home
* folder.
* Developer: Jitendra Khasdev
*/
int nektech_etc_passwd_search();
int nektech_search_path(char *,int);
char *nk_temp_arg[1];
int redirection = 0, append = 0;
char *redirt_file = NULL;
int main()
{
char cmd[MAX_LEN];
char *cmd_arg[MAX_ARG];
int cmdlen,i,j,tag, pending_exec = 0, error = 0;
do{
/* init cmd */
for(i=0;i<MAX_LEN;i++) cmd[i]='\0';
printf("NEK Tech>> ");
fgets(cmd,MAX_LEN,stdin);
cmdlen=strlen(cmd);
cmdlen--;
cmd[cmdlen]='\0';
redirt_file = NULL;
redirection = 0;
append = 0;
for(i=0;i<MAX_ARG;i++) cmd_arg[i]=NULL;
i=0; j=0; tag=0;
while(i<cmdlen && j<MAX_ARG){
if(cmd[i]==' '){
cmd[i]='\0';
tag=0;
}else{
if(tag==0)
cmd_arg[j++]=cmd+i;
tag=1;
}
i++;
}
if(j>=MAX_ARG && i<cmdlen){
printf("TOO MANY ARGUMENTS\n");
continue;
}
/* cmd_arg NULL Condition. */
if (cmd_arg[0] == NULL )
continue;
/* cmd quit/exit/q: exit NEK Tech Shell */
if((strcmp(cmd_arg[0],"quit")==0) || (strcmp(cmd_arg[0],"exit")== 0) || (strcmp(cmd_arg[0],"q")== 0))
break;
/* command change Directory */
if(strcmp(cmd_arg[0],"cd")==0){
nektech_change_dir(cmd_arg);
continue;
}
/* Handling Redirection out output to a file.*/
i = 0;
while (cmd_arg [i]){
if(strcmp(cmd_arg[i],">")==0 || strcmp(cmd_arg[i],">>")==0){
if (cmd_arg[i+1] != NULL){
redirt_file = cmd_arg[i+1];
if (strcmp(cmd_arg[i],">>")==0)
append = 1;
while (cmd_arg[i] != NULL){
cmd_arg[i++] = cmd_arg[i+2];
}
redirection = 1;
break;
}
else{
error = 1;
break;
}
}
i++;
}
if (error){
printf ("SHELL PARSING ERROR. WRONG INPUT.\n");
continue;
}
/* other cmd for fork/exec*/
nektech_run_cmd(cmd_arg);
}while(1);
}
/* Function name : nektech_change_dir ()
* Description : Changes Directory of the shell process to the directory
* given as user input.cd is Shell internal Command to
* change the present working Directory.It calls chdir().
* Error conditions : EACCESS, EPERM, ENOENT, ENOTDIR
* Developer : Shubhangi Maheshwari
* Jitendra Khasdev (cd command with home directoty intelligence)
*/
int nektech_change_dir(char *argv[])
{
if(argv[1]!=NULL){
/*
* cd ~ command support to change the
* directory to user home directory.
*/
if(*argv[1] == '~'){
argv[1]=strcat(nk_temp_arg[1],(*(argv+1)+1));
}
if(chdir(argv[1])<0)
switch(errno){
case ENOENT:
printf("DIRECTORY NOT FOUND\n");
break;
case ENOTDIR:
printf("NOT A DIRECTORY NAME\n");
break;
case EACCES:
printf("YOU DO NOT HAVE RIGHT TO ACCESS\n");
break;
default:
printf("SOME ERROR HAPPENED IN CHDIR\n");
}
}
else{
if (nektech_etc_passwd_search()){
printf("Missing system file./n");
return -1;
}
nektech_change_dir(nk_temp_arg);
}
return 1;
}
/*
*NEKTech Research Labs
*
*Function name : nektech_etc_passwd_search()
*Description : This fuction is used for getting home directory path
* from /etc/passwd file.
*
*Fucntion Design : 1. open /etc/passwd file
* 2. store line till '\n' character
* 3. then call nektech_search_path(char *ptr,int userid)
* 4. which give path in global variable nk_temp_arg[1]
* 5. it is used in change_dir function.
*
*Developer : jitendra khasdev
*
*/
int nektech_etc_passwd_search()
{
int fp, c, i=0, k, j, ret = 0;
static char buffer[200],temp[200];
int end_file_position, current_position=1,flag=0,repeat=0,count=0;
fp = open("/etc/passwd",O_RDONLY);
if( fp == -1 ){
printf("\n Unable to reach /dir with error [%s]\n",strerror(errno));
ret = -1;
}
else{
end_file_position=lseek(fp,0L,2); //reach the end of the file
lseek(fp,0L,0); // set file pointer to start
while(i < end_file_position){
for(k=0;k<200;k++) buffer[k]='\0'; //initiallzation of buffer
if(count = read( fp, buffer, sizeof(buffer))){
for(k=0,j=0;k<200;k++,j++){
temp[j]=*(buffer+k);
if(*(buffer+k)=='\n'){
if(nektech_search_path(temp,getuid())!= 0){
for(j=0;j<200;j++)temp[j]='\0';
j=0;
}
else
break;
}
}
}
i=i+count;
}
}
return ret;
}
/*
*NEKTech Research Labs
*
*
*function name : nektech_search_path()
*Description : This fuction is give path with respect to userid.
*
*Function Design : 1. it is called by nektech_etc_passwd_search()
* 2. now it search delimitor(":")
* 3. if second delimitor then start storing string till third delimitor
* 4. now compare with uid if this is found same
* 5. then it start storing path from 5 to 6 delimitor.
*
*Developer : jitendra khasdev
*/
int nektech_search_path(char *ptr,int userid)
{
int count=0,i=0,j=0,k=0,flag=0;
char nk_temp[200];
char temp_cd[2]="cd";
for(k=0;k<100;k++) nk_temp[k]='\0';
while(*(ptr) != '\n')
{
if(*(ptr) == ':')
count++;
if(count == 2){
nk_temp[j]= *(ptr+1);
j++;
}
if(count == 3){
nk_temp[j-1] = '\0';
j=0;
if(atoi(nk_temp)==userid){
flag=1;
}
}
if(count == 5 && flag == 1){
nk_temp[j] = *(ptr+1);
j++;
}
if(count == 6 && flag == 1 ){
nk_temp[j-1]='\0';
flag=0;
j=0;
nk_temp_arg[1]=nk_temp;
return 0;
}
ptr++;
}
return 1;
}