-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.c
44 lines (29 loc) · 1.12 KB
/
tokenize.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
#include "header.h"
#include "command.h"
#include "shell.h"
#include "hop.h"
#include "log.h"
#include "global.h"
void tokenize(char* command, char* home_path, char* previous){
if(!strlen(command)) return;
saveCommand(command,home_path,previous);
const char outer_delimiters[] = ";";
const char inner_delimiters[] = "&";
char* token;
char* outer_saveptr = NULL;
char* inner_saveptr = NULL;
token = strtok_r(command, outer_delimiters, &outer_saveptr);
while(token != NULL){
int count = 0;
for(int i = 0; i < strlen(token); i++) if(token[i] == '&') count++;
char* inner_token = strtok_r(token, inner_delimiters, &inner_saveptr);
int cur_cnt = 0;
while(inner_token != NULL){
inner_token = trim(inner_token);
remove_consecutive_spaces(inner_token);
if(strlen(inner_token)) handlePiping(inner_token,home_path,previous,(++cur_cnt <= count));
inner_token = strtok_r(NULL, inner_delimiters,&inner_saveptr);
}
token = strtok_r(NULL, outer_delimiters,&outer_saveptr);
}
}