-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_split_read.c
79 lines (77 loc) · 2.09 KB
/
command_split_read.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
#include"header.h"
/* Spliting a command in accordance with predefined delimiters */
char **command_spliter(char *command){
int pos = 0;
char **tokens = malloc(1000 * sizeof(char*)), *one;
if(!tokens){
fprintf(stderr, "Error in allocating space\n");
exit(EXIT_FAILURE);
}
one = strtok(command, COMMAND_DLMTR);
while(one){
// printf("%s\n", one);
tokens[pos] = one;
pos++;
one = strtok(NULL, COMMAND_DLMTR);
}
tokens[pos] = NULL;
return tokens;
}
/* Spliting a command in accordance with a particular delimiter */
char** myparser(char *command, char* delimiter)
{
int pos = 0;
char ** mytoken = malloc(1000*sizeof(char *)), *one;
if (!mytoken)
{
fprintf(stderr, "Error in allocating space\n");
exit(EXIT_FAILURE);
}
one = strtok(command, delimiter);
while(one)
{
mytoken[pos] = one;
pos++;
one = strtok(NULL, delimiter);
}
mytoken[pos]='\0';
pos++;
return mytoken;
}
/* This function returns commands seperated by semi-colon */
char **command_reader(){
int line_size = 1000, pos = 0, c;
char *line = malloc(sizeof(char) * line_size);
if(!line){
fprintf(stderr, "Error in allocating space\n");
exit(EXIT_FAILURE);
}
while(1){
// all the characters will be stored in their integer format, giving comfort in breaking them.
c = getchar(); // This function waits for input
if(c == '\n'){
line[pos] = '\0';
break;
}
else{
line[pos++] = c;
}
}
pos = 0;
// breaking the commands after ";"
/*printf("%s\n", line);*/
char **commands = malloc(1000 * sizeof(char*)), *command;
if(!commands){
fprintf(stderr, "Error in allocating space\n");
exit(EXIT_FAILURE);
}
command = strtok(line, ";");
while(command){
commands[pos] = command;
// printf("%s\n", command);
pos++;
command = strtok(NULL, ";");
}
commands[pos] = NULL;
return commands;
}