-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
62 lines (54 loc) · 1.55 KB
/
Copy pathshell.c
File metadata and controls
62 lines (54 loc) · 1.55 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "interpreter.h"
#include "shellmemory.h"
int MAX_USER_INPUT = 1000;
int parseInput(char ui[]);
// Start of everything
int main(int argc, char *argv[]) {
printf("%s\n", "Shell v1.0");
help();
char prompt = '$'; // Shell prompt
char userInput[MAX_USER_INPUT]; // user's input stored here
int errorCode = 0; // zero means no error, default
//init user input
for (int i=0; i<MAX_USER_INPUT; i++)
userInput[i] = '\0';
//init shell memory
mem_init();
while(1) {
if (isatty(fileno(stdin))) printf("%c ",prompt);
fgets(userInput, MAX_USER_INPUT-1, stdin);
if (feof(stdin)){
freopen("/dev/tty", "r", stdin);
}
errorCode = parseInput(userInput);
if (errorCode == -1) exit(99); // ignore all other errors
memset(userInput, 0, sizeof(userInput));
}
return 0;
}
int parseInput(char ui[]) {
char tmp[200];
char *words[100];
int a = 0;
int b;
int w=0; // wordID
int errorCode;
for(a=0; ui[a]==' ' && a<1000; a++); // skip white spaces
while(ui[a] != '\n' && ui[a] != '\0' && a<1000) {
for(b=0; ui[a]!=';' && ui[a]!='\0' && ui[a]!='\n' && ui[a]!=' ' && a<1000; a++, b++){
tmp[b] = ui[a];
// extract a word
}
tmp[b] = '\0';
words[w] = strdup(tmp);
w++;
if(ui[a] == '\0') break;
a++;
}
errorCode = interpreter(words, w);
return errorCode;
}