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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include "conf-update.h"
char **get_listing(char *cmd, char *delim) {
FILE *pipe;
char *buf = NULL;
size_t bufsize = 0;
ssize_t read;
char **listing;
int count = 1, i = 0;
unsigned int j;
char c;
pipe = popen(cmd, "r");
if (pipe) {
read = getdelim(&buf, &bufsize, '\0', pipe);
char *buf_backup = buf;
if (read != -1) {
// determine number of tokens
while ((c = buf[i]) != '\0') {
for (j=0;j<strlen(delim);j++) {
if (c == delim[j]) {
count++;
}
}
i++;
}
listing = (char **) malloc(sizeof(char *) * count);
char *str;
i=0;
while ((str = strsep(&buf, delim))) {
listing[i] = strdup(str);
i++;
}
free(buf_backup);
// make sure the last one is always LAST_ENTRY
if(i == count) {
free(listing[count-1]);
}
listing[count-1] = LAST_ENTRY;
pclose(pipe);
return listing;
} else {
pclose(pipe);
listing = (char **)malloc(sizeof(char *));
listing[0] = LAST_ENTRY;
free(buf_backup);
return listing;
}
} else {
exit_error(0, cmd);
}
// just for gcc, that bitch
return NULL;
}
int compare_updates(const void *a, const void *b) {
char *real_a;
char *real_b;
int result;
signed mod = 1;
if (is_valid_entry(*(char **)a)) {
real_a = get_real_filename(*(char **)a);
} else {
real_a = NULL;
}
if (is_valid_entry(*(char **)b)) {
real_b = get_real_filename(*(char **)b);
} else {
real_b = NULL;
}
if (!real_a && !real_b) {
result = -1;
} else if (!real_a) {
result = 1;
} else if (!real_b) {
result = -1;
} else {
// both valid updates
if ((result = strcmp(real_a, real_b)) == 0) {
// same target
if ((result = strncmp(strstr(*(char **)a, "._cfg"), strstr(*(char **)b, "._cfg"), strlen("._cfg????"))) == 0) {
// same update number. interactively merged vs. predefined, 1:0 for the user.
mod = -1;
}
result = mod * strcmp(*(char **)a, *(char **)b);
}
}
free(real_a);
free(real_b);
return result;
}
struct node *fold_updates(char **list) {
struct node *root = malloc(sizeof(struct node));
struct node *mynode, *newnode;
char *endtok, *curtok;
int i;
int run;
root->name = strdup("/");
root->children = malloc(sizeof(struct node *));
root->ct_children = 0;
root->parent = NULL;
root->dir = TRUE;
root->link = NULL;
for (i=0;!is_last_entry(list[i]);i++) {
if (is_valid_entry(list[i])) {
endtok = list[i]+1;
run = 1;
while (run) {
if (run == 2) {
// 2 means we're on the last run
run = 0;
endtok = list[i] + strlen(list[i]) + 1;
} else {
if ((endtok = strchr(endtok+1, '/')) == NULL) {
run = 2;
}
}
curtok = strndup(list[i], endtok - list[i]);
mynode = find_node(root, curtok);
if (mynode == (struct node *)FALSE) {
mynode = root;
}
if (mynode != (struct node *)TRUE) {
// mynode is the parent of the new to be inserted node
newnode = malloc(sizeof(struct node));
newnode->name = strdup(curtok);
if (!strcmp(curtok,list[i])) {
// it's the file
newnode->dir = FALSE;
newnode->link = &list[i];
} else {
newnode->dir = TRUE;
newnode->link = NULL;
}
newnode->children = malloc(sizeof(struct node *));
newnode->ct_children = 0;
newnode->parent = mynode;
mynode->ct_children++;
mynode->children = realloc(mynode->children, sizeof(struct node *) * mynode->ct_children);
mynode->children[mynode->ct_children-1] = newnode;
}
free(curtok);
}
}
}
return root;
}
struct node *find_node(struct node *root, char *path) {
int i;
struct node *mynode;
if (!strcmp(root->name, path)) {
// already exists
return (struct node *)TRUE;
} else if (!strncmp(root->name, path, strlen(root->name)) && root->dir == TRUE) {
// at least it's in the same direction, go through the list of children
for (i=0;i<root->ct_children;i++) {
mynode = find_node(root->children[i], path);
if (mynode == (struct node *)TRUE) {
return (struct node *)TRUE;
} else if (mynode != (struct node *)FALSE) {
return mynode;
}
}
// if we hit this point, nothing was found, meaning that it has to be a child of the current node
return root;
} else {
// completely wrong
return (struct node *)FALSE;
}
}
void sanity_checks() {
extern struct configuration config;
unsigned int i;
FILE *pipe;
char *cmd = NULL;
char *tools[] = {
"diff",
"portageq",
strndup(config.pager, strchrnul(config.pager, ' ') - config.pager),
strndup(config.diff_tool, strchrnul(config.diff_tool, ' ') - config.diff_tool),
strndup(config.merge_tool, strchrnul(config.merge_tool, ' ') - config.merge_tool),
strndup(config.edit_tool, strchrnul(config.edit_tool, ' ') - config.edit_tool)
};
gchar *program_in_path;
if (getuid() != 0) {
fprintf(stderr, "!!! Oops, you're not root!\n");
exit(EXIT_FAILURE);
}
for (i=0;i<sizeof(tools)/sizeof(tools[0]);i++) {
// "" is okay for pager
if (strcmp(tools[i], "")) {
if (!(program_in_path = g_find_program_in_path((gchar *)tools[i]))) {
fprintf(stderr, "!!! ERROR: couldn't find necesary tool: %s\n", tools[i]);
exit(EXIT_FAILURE);
} else {
g_free(program_in_path);
}
}
}
free(cmd);
free(tools[2]);
free(tools[3]);
free(tools[4]);
free(tools[5]);
mkdir (MD5SUM_INDEX_DIR, 0755);
if ((pipe = fopen(MD5SUM_INDEX, "a"))) {
fclose(pipe);
} else {
fprintf(stderr, "!!! ERROR: Can't write to %s; check permissions", MD5SUM_INDEX);
exit(EXIT_FAILURE);
}
}
void draw_legend(WINDOW *inner) {
int i;
wattron(inner, COLOR_PAIR(2));
wattron(inner, A_BOLD);
box(inner, 0, 0);
for (i=1;i<LINES-5;i++) {
mvwhline(inner, i, 1, ' ', COLS-6);
}
wattroff(inner, A_BOLD);
wattron(inner, COLOR_PAIR(3));
mvwprintw(inner, 1, 2, "Select current: !!!!!!! | !!!ll | !!!nselect all");
mvwprintw(inner, 2, 2, "Show diff: !!!!!!! | !!!dit update | !!!erge interactively");
mvwprintw(inner, 3, 2, "Actions for all selected: !!!eplace config file(s) | !!!elete update(s)"); // | merge !!!nteractively");
mvwprintw(inner, 4, 2, "Quit: !!!");
wattron(inner, COLOR_PAIR(4));
mvwprintw(inner, 1, 2 + strlen("Select current: "), "[SPACE]");
mvwprintw(inner, 1, 2 + strlen("Select current: !!!!!!! | "), "[A]");
mvwprintw(inner, 1, 2 + strlen("Select current: !!!!!!! | !!!ll | "), "[U]");
mvwprintw(inner, 2, 2 + strlen("Show diff: "), "[ENTER]");
mvwprintw(inner, 2, 2 + strlen("Show diff: !!!!!!! | "), "[E]");
mvwprintw(inner, 2, 2 + strlen("Show diff: !!!!!!! | !!!dit update | "), "[M]");
mvwprintw(inner, 3, 2 + strlen("Actions for all selected: "), "[R]");
mvwprintw(inner, 3, 2 + strlen("Actions for all selected: !!!eplace config file(s) | "), "[D]");
//mvwprintw(inner, 3, 2 + strlen("Action shortcuts: !!!erge | !!!elete update | merge "), "[I]");
mvwprintw(inner, 4, 2 + strlen("Quit: "), "[Q]");
wattron(inner, COLOR_PAIR(2) | A_BOLD);
// TODO: replace COLS - 4/LINES - 7 with a function to determine size of inner
mvwhline(inner, 5, 1, 0, COLS - 4 -2);
mvwhline(inner, LINES - 7, 1, 0, COLS - 4 -2);
wrefresh(inner);
}
void draw_background() {
int i;
attron(A_BOLD);
attron(COLOR_PAIR(1));
// why does clear() not work here?
for (i=0;i<LINES;i++) {
mvhline(i, 0, ' ', COLS);
}
attron(COLOR_PAIR(5));
mvhline(LINES-2, 3, ' ', COLS-4);
mvvline(3, COLS-2, ' ', LINES-4);
attron(COLOR_PAIR(1));
mvprintw(0,1, PROG_NAME);
mvprintw(0,strlen(PROG_NAME) + 2, PROG_VERSION);
mvhline(1, 1, ACS_HLINE, COLS - 2);
refresh();
}
char *get_indent_name(struct node *update, int width) {
int ct_indents = 0;
struct node *mynode = update;
char *start, *name;
char *indent_name;
char number[] = "0000";
int num, remainder, i;
while ((mynode = mynode->parent)) {
ct_indents++;
}
indent_name = calloc(width + 1, sizeof(char));
if ((start = strstr(update->name, "._cfg"))) {
name = start+strlen("._cfg????_");
while (ct_indents > 0) {
strcat(indent_name, INDENT_STR);
ct_indents--;
}
strcat(indent_name, name);
strcat(indent_name, " (");
strncpy(number, start+strlen("._cfg"), 4);
num = atoi(number) + 1;
snprintf(indent_name + strlen(indent_name), 4, "%d", num);
strcat(indent_name, ")");
if (*(name - 1) == '-') {
strcat(indent_name, "(merged)");
}
} else {
start = strrchr(update->name, '/') + 1;
while (ct_indents > 0) {
strcat(indent_name, INDENT_STR);
ct_indents--;
}
strcat(indent_name, start);
strcat(indent_name, "/");
}
remainder = width - strlen(indent_name);
for(i=0;i<remainder;i++) {
strcat(indent_name, " ");
}
return indent_name;
}
int count_array_items(struct node *root) {
int count = 0, i;
for (i=0;i<root->ct_children;i++) {
count += count_array_items(root->children[i]);
}
return 1 + count;
}
void build_item_array(ITEM **item_array, struct node *root, int menu_width) {
int i = 0;
// fast-forward to the next NULL entry
while (item_array[i]) {
i++;
}
item_array[i] = new_item(get_indent_name(root, menu_width), "");
set_item_userptr(item_array[i], root->link);
for (i=0;i<root->ct_children;i++) {
build_item_array(item_array, root->children[i], menu_width);
}
}
void free_folded(struct node *root) {
int i;
for (i=0;i<root->ct_children;i++) {
free_folded(root->children[i]);
}
// if (root->dir) { // it seems name is assigned unconditionally since $sometime
free(root->name);
// }
free(root->children);
free(root);
}
bool get_confirmation(WINDOW *win, char *action) {
bool result;
echo();
nocbreak();
char ret[2] = " ";
wattron(win, COLOR_PAIR(4));
wattroff(win, A_BOLD);
// TODO: replace COLS - 4/LINES - 7 with a function to determine size of inner
mvwprintw(win, LINES - 6, 2, "Do you really want to ");
wprintw(win, action);
wprintw(win, " all selected updates? [y/n] ");
refresh();
wgetnstr(win, ret, 1);
if (!strcmp(ret,"y")) {
result = true;
} else {
result = false;
}
mvwhline(win, LINES - 6, 1, ' ', COLS - 4 -2);
noecho();
cbreak();
return result;
}
void exit_error(bool expr, char *hint) {
if (!expr) {
endwin();
char *mystring = calloc(sizeof(char), strlen("!!! ERROR: ") + strlen(hint) + 1);
sprintf(mystring, "!!! ERROR: %s", hint);
perror(mystring);
exit(EXIT_FAILURE);
}
}
char **get_files_list(char *searchpath, char **index, int *max) {
struct stat mystat, tmpstat;
int i = 0, j;
DIR *dirfd;
struct dirent *file = (struct dirent *)TRUE;
char *absfile;
char *myfile;
bool ignore;
if(-1 == lstat(searchpath, &mystat)) {
return index;
}
if (S_ISDIR(mystat.st_mode)) {
dirfd = opendir(searchpath);
if (dirfd) {
while (file) {
file = readdir(dirfd);
if (file && strcmp(file->d_name, ".") && strcmp(file->d_name, "..") && \
strcmp(file->d_name, ".svn") && strcmp(file->d_name, "CVS")) {
absfile = (char *)calloc((strlen(searchpath) + strlen("/") + strlen(file->d_name) + 1), sizeof(char *));
strcpy(absfile, searchpath);
strcat(absfile, "/");
strcat(absfile, file->d_name);
index = get_files_list(absfile, index, max);
free(absfile);
}
}
} else if (errno == ENOENT) {
return index;
} else {
exit_error(0, searchpath);
}
closedir(dirfd);
} else if (S_ISREG(mystat.st_mode)) {
if (!strncmp(strrchr(searchpath, '/')+1, "._cfg", strlen("._cfg"))) {
if (*(searchpath+strlen(searchpath)-1) != '~' && \
strcmp(searchpath+strlen(searchpath)-4,".bak")) {
myfile = get_real_filename(searchpath);
if (access(myfile, F_OK) != 0) {
// we don't want phantom updates
unlink(searchpath);
} else {
// we don't want duplicates either
ignore = FALSE;
for (j=0;j<(*max) && index[j] != LAST_ENTRY;j++) {
lstat(index[j], &tmpstat);
if (tmpstat.st_dev == mystat.st_dev && \
tmpstat.st_ino == mystat.st_ino) {
ignore = TRUE;
}
}
if (!ignore) {
while (i < (*max) && !is_last_entry(index[i])) {
i++;
}
if (i + 1 >= (*max)) {
(*max)++;
index = (char **)realloc(index, (*max) * sizeof(char *));
}
index[i] = strdup(searchpath);
index[i+1] = LAST_ENTRY;
}
}
free(myfile);
}
}
}
return index;
}
|