-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
68 lines (51 loc) · 1.66 KB
/
input.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
#include "headers.h"
#include "input.h"
#include "prompt.h"
//char* input(char ***subcommands)
char* input(char **subcommands)
{
char *temp = NULL;
size_t size_of_input = 1024; // Assuming
int len = getline(&temp,&size_of_input,stdin);
if(len < 0)
{
printf(RED "\n Ctrl-D Detected \n" RESET);
exit(0);
}
temp[len - 1] = '\0' ; // removing newline charecter
//printf("%s\n",temp);
// if(len == 1) // empty string
// {
// return NULL;
// }
char* tempcomm[100]; // can handle 100 subinstructions
tempcomm[0] = NULL; // initialize it initially as NULL
char* token = strtok(temp,";"); // All semicolon seperated instructions
for(int i = 0; token != NULL;i++)
{
tempcomm[i] = token;
tempcomm[i+1] = NULL; // for safety purposes
token = strtok(NULL,";"); // Use same string
}
subcommands[0] = NULL;
for(int i = 0; tempcomm[i] != NULL;i++)
{
subcommands[i+1] = NULL;
//subcommands[i] = (char*)malloc(100*sizeof(char));
subcommands[i] = tempcomm[i];
}
// for(int i = 0; tempcomm[i] != NULL;i++)
// {
// subcommands[i+1] = NULL;
// subcommands[i] = (char**)malloc(100*sizeof(char*));
// subcommands[i][0] = NULL; // it's a 3d array now , break this argument down to smaller parts
// char* token_l = strtok(tempcomm[i],"&"); // tokenise on this
// for(int j = 0; token_l != NULL;j++)
// {
// subcommands[i][j+1] = NULL;
// subcommands[i][j] = token_l;
// token_l = strtok(NULL,"&");
// }
// }
return temp;
}