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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#include <ctype.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slice {
char* ref;
size_t len;
} slice_t;
typedef struct pair {
slice_t key;
slice_t value;
} pair_t;
pair_t* global_arg_table = NULL;
size_t args_count = 0;
void parse_config_file(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "failed to open config file: %s\n", filename);
exit(1);
}
char* line = NULL;
size_t len = 0;
ssize_t read;
size_t new_args_count = args_count;
while ((read = getline(&line, &len, file)) != -1) {
if (read > 0 && line[0] != '#' && line[0] != '\n') {
new_args_count++;
}
}
rewind(file);
if (new_args_count > args_count) {
pair_t* new_table = realloc(global_arg_table, new_args_count * sizeof(pair_t));
if (!new_table) {
fprintf(stderr, "realloc failed for config file parsing\n");
fclose(file);
free(line);
exit(1);
}
global_arg_table = new_table;
}
while ((read = getline(&line, &len, file)) != -1) {
if (read <= 1 || line[0] == '#' || line[0] == '\n') continue;
if (line[read - 1] == '\n') line[read - 1] = '\0';
char* current = line;
char* eq_sign = strchr(current, '=');
if (eq_sign == NULL) {
fprintf(stderr, "config file error: expected 'key=value' in '%s'\n", current);
continue;
}
// key
size_t key_len = eq_sign - current;
size_t key_start = 0;
while (key_start < key_len && isspace(current[key_start])) {
key_start++;
key_len--;
}
while (key_len > 0 && isspace(current[key_start + key_len - 1])) {
key_len--;
}
if (key_len == 0) {
fprintf(stderr, "config file error: empty key in '%s'\n", current);
continue;
}
// value
size_t value_start = eq_sign - current + 1;
size_t value_len = strlen(current) - value_start;
while (value_start < strlen(current) && isspace(current[value_start])) {
value_start++;
value_len--;
}
while (value_len > 0 && isspace(current[value_start + value_len - 1])) {
value_len--;
}
// store
char* key_str = strndup(current + key_start, key_len);
char* value_str = strndup(current + value_start, value_len);
if (!key_str || !value_str) {
fprintf(stderr, "strndup failed for config file parsing\n");
free(key_str);
free(value_str);
fclose(file);
free(line);
exit(1);
}
global_arg_table[args_count].key.ref = key_str;
global_arg_table[args_count].key.len = key_len;
global_arg_table[args_count].value.ref = value_str;
global_arg_table[args_count].value.len = value_len;
args_count++;
}
fclose(file);
free(line);
}
void parse_arguments(int argc, char** argv) {
if (argc <= 1) return;
global_arg_table = calloc(argc - 1, sizeof(pair_t));
if (!global_arg_table) {
fprintf(stderr, "calloc failed for argument parsing\n");
exit(1);
}
for (int i = 1; i < argc; i++) {
char* current = argv[i];
char* eq_sign = strchr(current, '=');
if (eq_sign == NULL) {
printf(
"argument error: "
"moonpaste expects named arguments with values. example: 'foo=bar'\n"
" in '%s'",
current);
exit(1);
}
// Key
size_t key_len = eq_sign - current;
size_t key_start = 0;
while (key_start < key_len && isspace(current[key_start])) {
key_start++;
key_len--;
}
while (key_len > 0 && isspace(current[key_start + key_len - 1])) {
key_len--;
}
if (key_len == 0) {
printf(
"argument error: empty key\n"
" in '%s'\n",
current);
exit(1);
}
// value
size_t value_start = eq_sign - current + 1;
size_t value_len = strlen(current) - value_start;
while (value_start < strlen(current) && isspace(current[value_start])) {
value_start++;
value_len--;
}
while (value_len > 0 && isspace(current[value_start + value_len - 1])) {
value_len--;
}
// config file
if (key_len == 6 && strncmp(current + key_start, "config", 6) == 0) {
char* config_file = strndup(current + value_start, value_len);
if (!config_file) {
fprintf(stderr, "strndup failed for config file path\n");
exit(1);
}
parse_config_file(config_file);
free(config_file);
continue;
}
// store
global_arg_table[args_count].key.ref = current + key_start;
global_arg_table[args_count].key.len = key_len;
global_arg_table[args_count].value.ref = current + value_start;
global_arg_table[args_count].value.len = value_len;
args_count++;
}
}
char* get_config(char* key, bool mandatory) {
size_t len = strlen(key);
for (int i = 0; i < args_count; i++) {
if (global_arg_table[i].key.len != len) continue;
if (strncmp(key, global_arg_table[i].key.ref, len) != 0) continue;
char* new_str = strndup(global_arg_table[i].value.ref, global_arg_table[i].value.len);
if (!new_str && mandatory) {
fprintf(stderr, "strndup failed for config value\n");
exit(1);
}
return new_str; // free me!
}
if (!mandatory) return NULL;
fprintf(stderr, "program expected argument %s but did not receive it.\n", key);
return NULL;
}
|