This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
/
environment.c
277 lines (235 loc) · 5.73 KB
/
environment.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <alpm.h>
#include <alpm_list.h>
#include "conf.h"
#include "environment.h"
#include "util.h"
#include "wrapper.h"
struct config_t *config;
enum _pw_errno_t pwerrno = PW_ERR_OK;
char *powaur_dir;
char *powaur_editor;
int powaur_maxthreads;
struct colorstrs color;
/* Pacman configuration */
char *pacman_rootdir;
char *pacman_dbpath;
alpm_list_t *pacman_cachedirs;
alpm_list_t *pacman_syncdbs;
int setup_config(void)
{
config = config_init();
if (!config) {
return error(PW_ERR_INIT_CONFIG);
}
return 0;
}
/* Initialize colors */
void colors_setup(void)
{
if (config->color > 0) {
color.black = xstrdup(BLACK);
color.red = xstrdup(RED);
color.green = xstrdup(GREEN);
color.yellow = xstrdup(YELLOW);
color.blue = xstrdup(BLUE);
color.mag = xstrdup(MAG);
color.cyan = xstrdup(CYAN);
color.white = xstrdup(WHITE);
color.bblack = xstrdup(BBLACK);
color.bred = xstrdup(BRED);
color.bgreen = xstrdup(BGREEN);
color.byellow = xstrdup(BYELLOW);
color.bblue = xstrdup(BBLUE);
color.bmag = xstrdup(BMAG);
color.bcyan = xstrdup(BCYAN);
color.bwhite = xstrdup(BWHITE);
color.nocolor = xstrdup(NOCOLOR);
color.bold = xstrdup(BOLD);
color.votecol = xstrdup(VOTECOL);
/* Setup colorized print functions */
color_print_setup();
} else {
color.black = xstrdup("");
color.red = xstrdup("");
color.green = xstrdup("");
color.yellow = xstrdup("");
color.blue = xstrdup("");
color.mag = xstrdup("");
color.cyan = xstrdup("");
color.white = xstrdup("");
color.bblack = xstrdup("");
color.bred = xstrdup("");
color.bgreen = xstrdup("");
color.byellow = xstrdup("");
color.bblue = xstrdup("");
color.bmag = xstrdup("");
color.bcyan = xstrdup("");
color.bwhite = xstrdup("");
color.nocolor = xstrdup("");
color.bold = xstrdup("");
color.votecol = xstrdup("");
/* Restore non-colorized print functions */
color_print_restore();
}
}
static void colors_cleanup(void)
{
free(color.black);
free(color.red);
free(color.green);
free(color.yellow);
free(color.blue);
free(color.mag);
free(color.cyan);
free(color.white);
free(color.bblack);
free(color.bred);
free(color.bgreen);
free(color.byellow);
free(color.bblue);
free(color.bmag);
free(color.bcyan);
free(color.bwhite);
free(color.nocolor);
free(color.bold);
free(color.votecol);
}
/* @param reload set to > 0 if reloading libalpm so that cachedirs can be
* reinitialized.
*/
static int setup_pacman_environment(int reload)
{
enum _alpm_errno_t err;
alpm_list_t *i;
if (reload) {
pw_printf(PW_LOG_DEBUG, "Reloading pacman configuration\n");
pacman_cachedirs = NULL;
}
if (parse_pmconfig()) {
/* Free cachedirs */
FREELIST(pacman_cachedirs);
return error(PW_ERR_PM_CONF_PARSE);
}
if (!pacman_rootdir) {
pacman_rootdir = xstrdup(PACMAN_DEF_ROOTDIR);
}
if (!pacman_dbpath) {
pacman_dbpath = xstrdup(PACMAN_DEF_DBPATH);
}
if (!pacman_cachedirs) {
pacman_cachedirs = alpm_list_add(pacman_cachedirs,
xstrdup(PACMAN_DEF_CACHEDIR));
}
alpm_option_set_cachedirs(config->handle, pacman_cachedirs);
config->handle = alpm_initialize(pacman_rootdir, pacman_dbpath, &err);
if (!config->handle) {
return error(PW_ERR_INIT_ALPM_HANDLE);
}
/* Register sync dbs */
for (i = pacman_syncdbs; i; i = i->next) {
if (!alpm_db_register_sync(config->handle, (const char *) i->data,
ALPM_SIG_USE_DEFAULT)) {
return error(PW_ERR_INIT_ALPM_REGISTER_SYNC);
}
}
return 0;
}
static int setup_powaur_config(void)
{
/* Check the following places for logfile:
* $XDG_CONFIG_HOME
* $HOME
*
* Then fallback to default settings
*/
int ret = -1;
FILE *fp = NULL;
char *dir;
char buf[PATH_MAX];
struct stat st;
pw_printf(PW_LOG_DEBUG, "%s: Setting up powaur configuration\n", __func__);
dir = getenv("XDG_CONFIG_HOME");
if (dir) {
snprintf(buf, PATH_MAX, "%s/%s", dir, PW_CONF);
if (!stat(buf, &st)) {
fp = fopen(buf, "r");
if (!fp) {
goto check_home;
}
pw_printf(PW_LOG_DEBUG, "%sParsing %s\n", TAB, buf);
parse_powaur_config(fp);
fclose(fp);
goto cleanup;
}
}
check_home:
/* Check $HOME */
dir = getenv("HOME");
if (dir) {
snprintf(buf, PATH_MAX, "%s/.config/%s", dir, PW_CONF);
if (!stat(buf, &st)) {
fp = fopen(buf, "r");
if (!fp) {
goto cleanup;
}
pw_printf(PW_LOG_DEBUG, "%sParsing %s\n", TAB, buf);
(fp);
fclose(fp);
}
}
cleanup:
/* Use default settings for unspecified options */
if (!powaur_dir) {
uid_t myuid = geteuid();
struct passwd *passwd_struct = getpwuid(myuid);
if (!passwd_struct) {
powaur_dir = xstrdup(PW_DEF_DIR);
} else {
snprintf(buf, PATH_MAX, "%s-%s", PW_DEF_DIR, passwd_struct->pw_name);
powaur_dir = xstrdup(buf);
}
pw_printf(PW_LOG_DEBUG, "%sFalling back to default directory %s\n",
TAB, powaur_dir);
}
if (!powaur_editor) {
powaur_editor = xstrdup(PW_DEF_EDITOR);
pw_printf(PW_LOG_DEBUG, "%sFalling back to default editor %s\n",
TAB, powaur_editor);
}
if (powaur_maxthreads <= 0 || powaur_maxthreads > PW_DEF_MAXTHREADS) {
powaur_maxthreads = PW_DEF_MAXTHREADS;
}
config->maxthreads = powaur_maxthreads;
pw_printf(PW_LOG_DEBUG, "%sMaximum no. of threads = %d\n", TAB,
config->maxthreads);
return 0;
}
int setup_environment(void)
{
if (setup_pacman_environment(0)) {
return -1;
}
if (setup_powaur_config()) {
return -1;
}
return 0;
}
void cleanup_environment(void)
{
if (config) {
config_free(config);
}
colors_cleanup();
free(powaur_editor);
free(powaur_dir);
/* No need to free pacman_cachedirs */
free(pacman_rootdir);
free(pacman_dbpath);
FREELIST(pacman_syncdbs);
}