-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
113 lines (105 loc) · 2.68 KB
/
parser.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "include/magic_bar.h"
void default_handler(t_config*, char*);
void submit_handler(t_config*, char*);
void favorite_handler(t_config*, char*);
char *map_key(const char* key)
{
char *res = malloc(sizeof(char) * strlen(key) + 1);
strcpy(res, key);
return res;
}
t_config_parser_item *get_parser_item(const char *key, void(*handler)(t_config*, char*))
{
t_config_parser_item *item;
item = malloc(sizeof(t_config_parser_item));
item->key = map_key(key);
item->handler = handler;
item->next = NULL;
return item;
}
t_config_parser_item *get_parser()
{
t_config_parser_item *def;
t_config_parser_item *submit;
t_config_parser_item *favorite;
def = get_parser_item("default:", &default_handler);
submit = get_parser_item("submit:", &submit_handler);
favorite = get_parser_item("->", &favorite_handler);
def->next = submit;
submit->next = favorite;
return def;
}
void default_handler(t_config *config, char *line)
{
int i = 0;
char *default_command;
while (*line && *line != '"') {
line++;
}
default_command = malloc(sizeof(char) * strlen(line) + 1);
while (*line) {
if (*line != '"' && *line != '\n') {
default_command[i] = *line;
i++;
}
line++;
}
config->default_command = default_command;
}
void submit_handler(t_config *config, char *line)
{
int i = 0;
char *submit_key;
while (*line && *line != '[') {
line++;
}
submit_key = malloc(sizeof(char) * strlen(line) + 1);
while (*line) {
if (*line != '[' && *line != ']' && *line != '\n') {
submit_key[i] = *line;
i++;
}
line++;
}
config->submit = submit_key;
return;
}
void favorite_handler(t_config *config, char *line)
{
int i = 0;
t_key_bin *fav_lookup;
t_key_bin *fav = malloc(sizeof(t_key_bin));
char key;
char *command = malloc(sizeof(char) * strlen(line));
while (*line && (*line != '-' && *(line + 1) != '>')) {
if (*line != '[' && *line != ']') {
key = *line;
i++;
}
*line++;
}
i = 0;
line += 2; // skip "->"
while (*line) {
if (*line != '"') {
command[i] = *line;
i++;
}
*line++;
}
fav->command = command;
fav->key = key;
fav->next = NULL;
if (config->favorite_head == NULL) {
config->favorite_head = fav;
return;
}
fav_lookup = config->favorite_head;
while (fav_lookup && fav_lookup->next) {
fav_lookup = fav_lookup->next;
}
if (fav_lookup) {
fav_lookup->next = fav;
}
return;
}