-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode.c
111 lines (104 loc) · 1.69 KB
/
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
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
#include "main.h"
/**
* rem_spe - remove special characters
*
* @str: input string
* Return: Modified string
*/
char *rem_spe(char *str)
{
while (*str == ' ' || *str == '\n')
str++;
return (str);
}
/**
* rem_ - remove spaces
*
* @str: input string
* Return: Modified string
*/
char *rem_(char *str)
{
while (*str == ' ')
str++;
return (str);
}
/**
* int_mode - Interactive mode
*
* @argv: argument variables
*/
void int_mode(char **argv)
{
char *t_input = NULL, *input = NULL, **tokens;
int status, loop = 0, read;
size_t bufflen = 0;
while (1)
{
loop++;
if (isatty(STDIN_FILENO))
prompt();
read = _getline(&t_input, &bufflen, stdin);
if (read == EOF)
{
free(t_input);
exit(0);
}
if (!t_input)
{
free(t_input);
break;
}
input = rem_(t_input);
if (input[0] == '\0' || _strcmp(input, "\n") == 0)
{
continue;
}
tokens = tokenise(input);
if (_strcmp(tokens[0], "exit") == 0)
_xit(input, argv, tokens, loop);
else
status = execute(tokens, loop);
freearray(tokens);
continue;
}
exit(status);
}
/**
* batch_mode - batch run a script
*
* @filename: name of file
* Return: status
*/
int batch_mode(char *filename)
{
FILE *fptr;
int status, loop = 0;
char line[1024];
char *input = NULL, *t_input = NULL, **args;
fptr = fopen(filename, "r");
if (fptr == NULL)
{
perror("Error:");
return (1);
}
else
{
while (fgets(line, sizeof(line), fptr) != NULL)
{
loop++;
t_input = line;
input = rem_(t_input);
if (input[0] == '\0' || _strcmp(input, "\n") == 0)
{
free(t_input);
continue;
}
args = tokenise(input);
status = execute(args, loop);
freearray(args);
}
}
fclose(fptr);
return (status);
}