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
203
204
205
|
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
#include "config.h"
struct config_item ci[] = {
{ "device", CFG_TYPE_STR, offsetof(struct config, device) },
{ "repeat", CFG_TYPE_BOOL, offsetof(struct config, repeat) },
{ "show_shifted", CFG_TYPE_BOOL,
offsetof(struct config, show_shifted) },
{ "time", CFG_TYPE_BOOL, offsetof(struct config, time) }
};
void fix_config_file_owner(const char *config_path)
{
char *sudo_uid_str = getenv("SUDO_UID");
char *sudo_gid_str = getenv("SUDO_GID");
if (sudo_uid_str && sudo_gid_str) {
uid_t original_uid = atoi(sudo_uid_str);
gid_t original_gid = atoi(sudo_gid_str);
chown(config_path, original_uid, original_gid);
}
}
struct config *prepare_config_file()
{
struct config *cfg = malloc(sizeof(struct config));
if (!cfg) {
return NULL;
}
char *sudo_user = getenv("SUDO_USER");
cfg->home = malloc(PATH_MAX);
if (!cfg->home) {
return NULL;
}
snprintf(cfg->home, PATH_MAX, "/home/%s", sudo_user);
cfg->config_file_path = malloc(strlen(cfg->home) + strlen("/.klrc"));
snprintf(cfg->config_file_path, PATH_MAX, "%s/.klrc", cfg->home);
if (access(cfg->config_file_path, F_OK) == -1) {
FILE *fp = fopen(cfg->config_file_path, "a");
if (!fp) {
return NULL;
}
fputs(CFG_FILE_COMMENT, fp);
printf("Config file was generated: %s\n",
cfg->config_file_path);
printf("Warning: There is no device which is defined in config, see README for details.\n");
fix_config_file_owner(cfg->config_file_path);
printf("%s\n", cfg->config_file_path);
fclose(fp);
}
return cfg;
}
void cmp_str(char** cfg, const char* val) {
}
void parse_bool(const char* val, bool* member_ptr) {
if (strcmp(val, "true") == 0) {
*member_ptr = true;
} else {
*member_ptr = false;
}
}
void parse_str(const char* val, char* member_ptr, size_t max_len) {
strncpy(member_ptr, val, max_len - 1);
member_ptr[max_len - 1] = '\0';
}
struct config *parese_config(struct config *cfg){
if (!cfg) {
return NULL;
}
FILE* fp = fopen(cfg->config_file_path, "r");
if (!fp) {
return NULL;
}
int line_count = 0;
char line[128];
char key[128] = "";
char val[128] = "";
while (fgets(line, sizeof(line), fp)) {
line_count++;
if (line[0] == '#' || line[0] == '\n' || line[0] == '\r')
continue;
int count = sscanf(line, "%s = %s", key, val);
if (count < 2) {
fprintf(stderr, "%s:%d: Invalid config\n", cfg->config_file_path, line_count);
fclose(fp);
return NULL;
}
for (int i = 0; i < CI_SIZE; i++) {
if (strcmp(key, ci[i].key) == 0) {
if (ci[i].type == CFG_TYPE_BOOL) {
parse_bool(val,(bool*)((char*)cfg + ci[i].offset));
}
if (ci[i].type == CFG_TYPE_STR) {
printf("key:%s\nval:%s\n", key, val);
parse_str(val, (char*)cfg + ci[i].offset, sizeof(cfg->device));
}
break;
}
}
}
fclose(fp);
return cfg;
}
/* struct config *parese_config(struct config *cfg) */
/* { */
/* if (!cfg) { */
/*
/* } */
/* FILE *fp = fopen(cfg->config_file_path, "r"); */
/* if (!fp) { */
/* return NULL; */
/* } */
/* int line_count = 0; */
/* char line[128]; */
/* char key[64] = { 0 }; */
/* char val[64] = { 0 }; */
/* while (fgets(line, sizeof(line), fp)) { */
/* line_count++; */
/* /\* Skip comment and blank lines *\/ */
/* if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') { */
/* continue; */
/* } */
/* int count = sscanf(line, "%s = %s", key, val); */
/* if (count != 2) { */
/* printf("Error at %s:%d\n invalid token: %s", */
/* cfg->config_file_path, line_count, line); */
/* return NULL; */
/* } */
/* if (CFG_COMPLETE(key, "device")) { */
/* strncpy(cfg->device, val, sizeof(cfg->device) - 1); */
/* cfg->device[sizeof(cfg) - 1] = '\0'; */
/* } else if (CFG_COMPLETE(key, "time")) { */
/* if (CFG_COMPLETE(val, "true")) { */
/* cfg->time = true; */
/* } else { */
/* cfg->time = false; */
/* } */
/* } else if (CFG_COMPLETE(key, "key_counter")) { */
/* if (CFG_COMPLETE(val, "true")) { */
/* cfg->keys_counter = true; */
/* } else { */
/* cfg->keys_counter = false; */
/* } */
/* } else if (CFG_COMPLETE(key, "repeat")) { */
/* if (CFG_COMPLETE(val, "true")) { */
/* cfg->repeat = true; */
/* } else { */
/* cfg->repeat = false; */
/* } */
/* } */
/* } */
/* fclose(fp); */
/* return cfg; */
/* } */
struct config *config_init()
{
struct config *cfg = prepare_config_file();
if (!cfg) {
return NULL;
}
if (parese_config(cfg) == NULL) {
return NULL;
}
return cfg;
}
|