-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
136 lines (110 loc) · 2.43 KB
/
util.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
/*
* =====================================================================================
*
* Filename: util.c
*
* Description: praton shell helper routines
*
* Version: 1.0
* Created: Monday 19 April 2021 01:38:29 IST
* Revision: none
* Compiler: gcc
*
* Author: PRATHU BARONIA (praton), prathu.baronia@oneplus.com
* Organization: OnePlus RnD
*
* =====================================================================================
*/
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#define PSH_RL_BUFSIZE 1024
extern int psh_num_builtins(void);
extern char *builtin_str[];
extern int (*builtin_func[]) (char **);
char *prompt = "psh> ";
char *psh_read_line(void)
{
char* input = readline(prompt);
add_history(input);
return input;
}
#define PSH_TOK_BUFSIZE 64
#define PSH_TOK_DELIM " \t\r\n\a"
char **psh_split_line(char *line)
{
int bufsize = PSH_TOK_BUFSIZE;
int position = 0;
char **tokens = malloc(sizeof(char*) * bufsize);
char *token;
if(!tokens){
fprintf(stderr, "psh: tokens allocation error");
exit(EXIT_FAILURE);
}
token = strtok(line, PSH_TOK_DELIM);
while(token != NULL) {
tokens[position] = token;
position++;
if(position >= bufsize) {
bufsize += PSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "psh: tokens reallocation failure\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, PSH_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
int psh_launch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("psh");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
perror("psh");
} else {
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int psh_execute(char **args)
{
int i;
if (args[0] == NULL) {
// Empty command was entered.
return 1;
}
for(i = 0; i < psh_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return psh_launch(args);
}
void psh_loop(void)
{
char *line;
char **args;
int status;
do {
line = psh_read_line();
args = psh_split_line(line);
status = psh_execute(args);
free(line);
free(args);
}while(status);
}