-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive_mode.c
47 lines (45 loc) · 1.26 KB
/
interactive_mode.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
#include "main.h"
/**
* interactive_mode - Creates a simple UNIX command interpreter
* for interactive mode.
*
* Return: Always 0 (Success), -1 otherwise.
*/
int interactive_mode(void)
{
char *prompt = "$ ", *line, *operator = ";", **parsed_line;
int i, valid;
/*When Ctrl+C is pressed, C program doesn't terminate, return newline*/
signal(SIGINT, _ctrl_c_signal_handler);
/* Counts the number of commands, starting from one */
cmd_counter = 1;
/* Global variable to handel exit of shell */
should_run = 1;
/* It displayed prompt again eachtime a command has been executed*/
while (should_run != 0)
{
/* Display a prompt to wait for the user to type a command */
_print(prompt);
/* Read text prompt from the stdin */
line = _read_line();
if (line == NULL)
return (-1);
/* Split input line into individual commands */
parsed_line = _split_line(line, operator);
/*Counts num of commands in a single line, starting from zero*/
i = 0;
while (parsed_line[i] != NULL)
{
valid = _handle_logical_operators(parsed_line[i]);
if (valid == -1)
{
_print("Invalid command: Cannot have both");
_print(" '&&' and '||' operators\n");
break;
}
i++;
}
free(line), free(parsed_line); /* Free the memory allocated */
}
return (0);
}