-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_main.c
108 lines (97 loc) · 2.14 KB
/
shell_main.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
#include "main.h"
/**
* _prompt - print the shell's prompt and reads the input
*
* Return: returns pointer to the input string or NULL
*/
char *_prompt()
{
char *line_ptr;
char *prompt;
prompt = "$ ";
write(STDOUT_FILENO, prompt, strlen(prompt));
/*fflush(stdout)*/
/* build a readline function */
line_ptr = _readLine();
return (line_ptr);
}
/**
* runNonInteractive - interface to execute commands passed to redirections
* @prog: the name of the program
*
* Return: always 0 on success. Always successful.
*/
int runNonInteractive(char *prog)
{
char *inputLine, **cmdLineArr, *cmdName = NULL;
cmd_t *cmdData;
int cmdCount = 1, exitStatus;
while (1)
{
inputLine = _readLine();
if (inputLine == NULL)
break;
/* split input line and create array of words */
cmdLineArr = parseLine(inputLine, " ");
if (cmdLineArr == NULL)
{
free(inputLine);
continue;
}
/* execute the command */
cmdData = searchCmd(cmdLineArr[0]);
if (cmdData == NULL)
{
cmdName = cmdLineArr[0];
dprintf(STDERR_FILENO, "%s: %d: %s: not found\n", prog, cmdCount, cmdName);
free(cmdData);
freeArrayOfPtr(cmdLineArr);
exit(127);
}
exitStatus = executeCmd(cmdData, cmdLineArr);
/* catch the exit status from executeCmd */
if (exitStatus != 0)
exit(exitStatus);
}
return (0);
}
/**
* runInteractive - interface to start up the terminal to input command after a
* prompt
* @prog: the name of the program
*
* Return: always 0 on success. Alway successful.
*/
int runInteractive(char *prog)
{
char *inputLine, **cmdLineArr, *cmdName = NULL;
cmd_t *cmdData;
size_t cmdCount;
cmdCount = 0;
/*signal(SIGINT, SIG_IGN);*/
while (1)
{
cmdCount++;
inputLine = _prompt();
if (inputLine == NULL)
break;
cmdLineArr = parseLine(inputLine, " ");
if (cmdLineArr == NULL)
{
free(inputLine);
continue;
}
cmdData = searchCmd(cmdLineArr[0]);
if (cmdData == NULL)
{
cmdName = cmdLineArr[0];
dprintf(STDERR_FILENO, "%s: %lu: %s: not found\n", prog, cmdCount, cmdName);
free(cmdData);
freeArrayOfPtr(cmdLineArr);
continue;
}
executeCmd(cmdData, cmdLineArr);
}
free(inputLine);
exit(EXIT_SUCCESS);
}