-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
94 lines (72 loc) · 2.11 KB
/
token.c
File metadata and controls
94 lines (72 loc) · 2.11 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<token.h>
#include<constants.h>
TOKEN* new_token(void) {
TOKEN *tok = (TOKEN *)malloc(sizeof(TOKEN));
if(tok != NULL) {
tok->is_background = false;
tok->is_option = false;
tok->is_command = false;
tok->val = "";
tok->length = 0;
}
return tok;
}
//Tokenize the string
bool tokenize(char *str, TOKEN **token_list, size_t* token_list_length) {
int n = strlen(str);
if(str == NULL)
return false;
//Count the number of tokens
*token_list_length = 1;
for(int i = 0; i < n; i++) {
if(str[i] == ' ')
(*token_list_length)++;
}
*token_list = (TOKEN *)malloc(sizeof(TOKEN)*(*token_list_length));
if(token_list == NULL) {
fprintf(stderr, error_msg);
exit(1);
}
char *token = NULL;
(*token_list_length) = 0;
size_t last_cmd = 0, token_len;
bool is_sep = true;
while((token = strsep(&str, " \n")) != NULL) {
token_len = strlen(token);
if(token_len == 0)
continue;
TOKEN *curr = NULL;
if(strcmp(token, "&") == 0) {
if((*token_list)[last_cmd].is_background) {
fprintf(stderr, "Invalid token \"&\" detected\n");
if(last_cmd != 0)
*token_list_length = last_cmd;
return false;
}
else
(*token_list)[last_cmd].is_background = true;
}
else if(strcmp(token, "&&") == 0)
is_sep = true;
else {
curr = new_token();
if(token[0] == '-' || !is_sep) {
curr->is_option = true;
(*token_list)[last_cmd].length++;
}
else {
curr->is_command = true;
curr->length = 1;
last_cmd = *token_list_length;
}
curr->val = token;
(*token_list)[(*token_list_length)++] = *curr;
is_sep = false;
}
}
return true;
}