-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
97 lines (96 loc) · 2.67 KB
/
parse.c
File metadata and controls
97 lines (96 loc) · 2.67 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
95
96
97
#include "./include/headers.h"
int waste(char*string)
{
int size=strlen(string);
for(int i=0;i<size;i++)
{
if(string[i]=='\n'||string[i]==' '||string[i]=='\t'||string[i]=='\v'||string[i]=='\f'||string[i]=='\r'||string[i]==';'||string[i]=='&')
continue;
else
return 0;
}
return 1;
}
char *trim(char *token)
{
int size=strlen(token);
char *result = (char *)malloc(sizeof(char) *5000);
result[0] = '\0';
for (int i = 1; i < size + 1; i++)
result[i] = '\0';
int pos = 0;
for (int i = 0; i < size; i++)
{
if ((token[i] == ' ' || token[i] == '\n' || token[i] == '\t' || token[i] == '\r' || token[i] == '\v' || token[i] == '\f') && pos == 0)
continue;
else
{
result[pos++] = token[i];
}
}
result[pos] = '\0';
return result;
}
struct commandnode *parse(char *string)
{
if(waste(string))
return NULL;
char *temp = (char *)malloc(sizeof(char) * 500);
strcpy(temp, string);
char *token;
char *ptr_in = NULL;
char del1[] = ";&\n";
int pos = 0;
int cnt = 0;
token = __strtok_r(temp, del1, &ptr_in);
if(token==NULL)
return NULL;
char*tempu=(char*)malloc(sizeof(char)*1000);
tempu[0]='\0';
strcpy(tempu,token);
char* result1=trim(tempu);
struct commandnode *head = (struct commandnode *)malloc(sizeof(struct commandnode));
head->next = NULL;
head->prev = NULL;
struct commandnode *last;
// char *result1 = trim(token);
if (strlen(result1) > 0)
{
struct commandnode *new = (struct commandnode *)malloc(sizeof(struct commandnode));
strcpy(new->command, result1);
new->bg = false;
if (string[(int)(ptr_in - temp) - 1] == '&')
new->bg = true;
else
new->bg = false;
last = new;
last->next = NULL;
last->prev = head;
head->next = new;
}
token = __strtok_r(NULL, del1, &ptr_in);
while (token != NULL)
{
// printf("%ld\n",strlen(token));
char *result = trim(token);
if (strlen(result) == 0)
{
token = __strtok_r(NULL, del1, &ptr_in);
continue;
}
struct commandnode *tempo = (struct commandnode *)malloc(sizeof(struct commandnode));
tempo->bg = false;
if (string[(int)(ptr_in - temp) - 1] == '&')
tempo->bg = true;
else
tempo->bg = false;
strcpy(tempo->command, result);
last->next = tempo;
tempo->prev = last;
last = tempo;
token = __strtok_r(NULL, del1, &ptr_in);
}
last->next = head;
head->prev = last;
return head;
}