File: debfoster.c

package info (click to toggle)
debfoster 2.7-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,188 kB
  • sloc: sh: 4,243; ansic: 2,397; perl: 39; makefile: 26; sed: 16
file content (996 lines) | stat: -rw-r--r-- 29,200 bytes parent folder | download | duplicates (2)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <locale.h>
#include <sys/file.h>
#include <sys/ioctl.h>

#include "config.h"
#include "AVLTree.h"
#include "error.h"
#include "conffile.h"
#include "keepers.h"
#include "status.h"
#include "xargs.h"
#include "asktty.h"
#include "intl.h"

struct undo_record {
	symbol_t name;
	struct undo_record* prev;
};

static AVLTree *keeplist, *postpone;
static struct undo_record *undo_stack = NULL;

static struct option long_options[] = {
	{"help", 0, 0, 'h'},
	{"verbose", 0, 0, 'v'},
	{"version", 0, 0, 'V'},
	{"quiet", 0, 0, 'q'},
	{"force", 0, 0, 'f'},
	{"mark-only", 0, 0, 'm'},
	{"upgrade", 0, 0, 'u'},
	{"keeperfile", 1, 0, 'k'},
	{"no-keeperfile", 0, 0, 'n'},
	{"show-keepers", 0, 0, 'a'},
	{"show-orphans", 0, 0, 's'},
	{"show-depends", 1, 0, 'd'},
	{"show-dependents", 1, 0, 'e'},
	{"show-providers", 1, 0, 'p'},
	{"show-related", 1, 0, 'r'},
	{"use-tasks", 0, 0, 't'},
	{"option", 1, 0, 'o'},
	{"config", 1, 0, 'c'},
	{"ignore-default-rules", 0, 0, 'i'},
	{0, 0, 0, 0}
};

enum {
	EAllDepends,
	EOrphansOnly
};

enum {
	ENoAlternatives,
	ECommittedAlternatives,
	EAllAlternatives
};

/* Find out the number of columns supported by the tty.
 * For non-tty's the default is 80 columns.
 */
int columns(void) {
#ifdef TIOCGWINSZ
	struct winsize w;
	if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*)&w) && w.ws_col)
		return w.ws_col;
#endif
	return 80;
}

/* Print a message if Verbose mode is on. */
int Vprintf(char *fmt, ...) {
	va_list ap;
	int n = 0;

	if(Verbose) {
		va_start(ap, fmt);
		n = vprintf(fmt, ap);
		va_end(ap);
	}
	return n;
}


/* Print a title and a set of symbols nicely on tty.
 * If the set is empty, nothing is printed.
 */
int show_set(AVLTree *set, char *fmt, ...) {
	va_list ap;
	AVLNode *p;
	int width = columns() - 2, n = width;

	if(set->head) {
		va_start(ap, fmt);
		vprintf(fmt, ap);
		va_end(ap);

		for(p = set->head; p; p = p->next) {
			if(n + strlen((symbol_t)p->item) >= width)
				n = printf("\n ");
			n += printf(" %s", (symbol_t)p->item);
		}
		printf("\n");
		return 1;
	}
	return 0;
}

static void showinfo(symbol_t pkg) {
	AVLTree t;
	AVLNode n;
	AVLInitTree(&t, (AVLCompare)strcmp);
	AVLInitNode(&n, pkg);
	AVLInsertTopNode(&t, &n);
	xargs(&t, InfoCmd);
}

/* A two-key sort function for optimizing packet processing
 * order for the interactive engine.
 */
int pkgsortcmp(struct package *a, struct package *b) {
	int k = b->orphan_depends - a->orphan_depends;
	return k ? k : strcasecmp(a->name, b->name);
}

/* Add a package and all its dependents to the keep list. */
void add_keeplist(struct package *pkg) {
        AVLNode *p;

	if(!AVLInsert(keeplist, pkg->name))
		return;

	/* Add all direct depends on keeper list */
	for(p = pkg->depends.head; p; p = p->next)
		add_keeplist(pkg_find(p->item));

	/* If this packge is a meta package and there is exactly one
	 * installed package providing it, add the provider to the
	 * keeper list. If there are multiple providers of the meta
	 * package, the user will have to answer some questions.
	 * In Force-mode we add all providers, since we don't know
	 * which one(s) the user would like to keep.
	 */
	if(!pkg->installed && (Force || pkg->provider_count == 1))
		for(p = pkg->providers.head; p; p = p->next)
			add_keeplist(pkg_find(p->item));
}

/* Add all packages on the keepers and postpone lists, including their
 * dependencies, to the keeplist.
 */
void build_keeplist(void) {
	AVLNode *c;
	struct package *pkg;

	AVLFreeNodes(keeplist);
	for(c = packages->head; c; c = c->next) {
		pkg = (struct package *)c->item;
		if(pkg->keep || AVLSearch(keepers, pkg->name) || AVLSearch(postpone, pkg->name))
			add_keeplist(pkg);
	}
}

/* Remove packages from keeper list if they are no longer installed. */
void prune_keepers(void) {
	AVLNode *c, *p;
	struct package *pkg;

	for(c = keepers->head; c; c = p) {
		p = c->next;
		pkg = pkg_find(c->item);
		if(!pkg || !(pkg->installed || pkg->task)) {
			Vprintf(_("Package was removed: %s\n"), (symbol_t)c->item);
			AVLDeleteNode(keepers, c);
		}
	}
}

/* Do a graph search for all dependencies of package <name>. The
 * <visited> list has a dual purpose. It serves as a loop detection
 * mechanism in the graph search. In the end it contains the result
 * of the search. Initially <visited> should be empty.
 *
 *  <include_providers> indicates how to deal with meta packages:
 *    ENoAlternatives           Don't include providers of meta packages
 *                              as dependencies.
 *    ECommittedAlternatives    If there is a single package that provides
 *                              a meta package, include it as a dependency.
 *    EAllAlternatives          Include all installed providers of a meta
 *                              package as dependencies.
 *
 *  <orphans_only> indicates whether to include all dependecies or only
 *  those that are potentially orphans:
 *    EAllDepends               Include all dependencies.
 *    EOrphansOnly              Include only potential orphans.
 *
 */
int all_depends(symbol_t name, AVLTree *visited, int include_providers, int orphans_only) {
	struct package *pkg;
	AVLNode *p;
	int count;

	if((orphans_only && AVLSearch(keeplist, name)) || !AVLInsert(visited, name))
		return 0;

	pkg = pkg_find(name);
	if(!pkg)
		return 0;
	count = pkg->installed ? 1 : 0;

	for(p = pkg->depends.head; p; p = p->next)
		count += all_depends(p->item, visited, include_providers, orphans_only);

	if(include_providers && (include_providers == EAllAlternatives
	|| pkg->provider_count == 1))
		for(p = pkg->providers.head; p; p = p->next)
			count += all_depends(p->item, visited, include_providers, orphans_only);

	return count;
}

/* Do a graph search for all dependents of package <name>. The
 * <visited> list has a dual purpose. It serves as a loop detection
 * mechanism in the graph search. In the end it contains the result
 * of the search. Initially <visited> should be empty.
 *
 *  <include_provides> indicates how to deal with meta packages:
 *    ENoAlternatives           Don't include meta packages as dependents.
 *    ECommittedAlternatives    If the package is the only one that provides
 *                              a meta package, include the meta package as
 *                              a dependent.
 *    EAllAlternatives          Always include meta packages as dependents
 *                              regardles of how many other packages provide
 *                              them.
 *
 */
void all_dependents(symbol_t name, AVLTree *visited, int include_provides) {
	AVLNode *p;
	struct package *pkg;
	if(AVLInsert(visited, name)) {
		pkg = pkg_find(name);
		for(p = pkg->dependents.head; p; p = p->next)
			all_dependents(p->item, visited, include_provides);
		if(include_provides)
			for(p = pkg->provides.head; p; p = p->next)
				if(include_provides == EAllAlternatives
				|| pkg_find(p->item)->provider_count == 1)
					all_dependents(p->item, visited, include_provides);
	}
}

/* add_provider(pkg, pkg) adds package <pkg> to the providers lists of
 * all meta packages that it provides. The routine traverses the graph
 * of provides links and adds the package to each node.
 *
 * We collapse any provider chains so that the providers list of
 * package X is guaranteed to contain only installed packages that
 * really provide X.  This simplifies the dependency handling for
 * alternative packages.
 *
 * We also detect problems such as the following:
 *
 *     A     Installed packages B and C depend on each other and both
 *    / \    claim to provide A. A now appears to have two alternative
 *   B===C   dependencies, whereas in truth both B and C are required.
 *
 * We break the above into one of the following (non-deterministic):
 *
 *     A       A
 *    /         \
 *   B===C   B===C
 *
 */
void add_provider(struct package *pkg, struct package *ppkg) {
        AVLNode *c, *p;
	AVLTree depends, dependents;

	if(AVLSearch(&ppkg->providers, pkg->name))
		return;

	if(ppkg->provider_count) {
		/* Remove multiple alternative dependencies to a dependency loop */
		AVLInitTree(&depends, (AVLCompare)symcmp);
		AVLInitTree(&dependents, (AVLCompare)symcmp);
		all_depends(pkg->name, &depends, ENoAlternatives, EAllDepends);
		all_dependents(pkg->name, &dependents, ENoAlternatives);
		for(c = ppkg->providers.head; c; c = p) {
			p = c->next;
			if(AVLSearch(&depends, c->item)) {
				/* pkg depends on something already on provider list. Don't add pkg. */
			        pkg = NULL;
				break;
			}
			if(AVLSearch(&dependents, c->item)) {
				/* Something on the provider list depends on pkg. Remove it. */
				AVLDeleteNode(&ppkg->providers, c);
				ppkg->provider_count--;
			}
		}
	}

	if (pkg) {
		AVLInsert(&ppkg->providers, pkg->name);
		ppkg->provider_count++;

		for(c = ppkg->provides.head; c; c = c->next)
	        	add_provider(pkg, pkg_find(c->item));
	}
}

/* remove_provider(pkg, pkg) removes package <pkg> from the providers
 * lists of all meta packages that it provides. The routine traverses
 * the graph of "provides" links and removes the package from each
 * node.
 *
 * This routine gets called when debfoster has decided to remove a
 * particular package. If, as a result of removing the package, any
 * other package X becomes the last remaining alternative that
 * provides a dependency Y that is on keeplist, X and all its
 * dependencies will also be added to the keeplist. This locks them as
 * required dependencies.
 */
void remove_provider(struct package *pkg, struct package *ppkg) {
        AVLNode *p;

	if(!(p = AVLSearch(&ppkg->providers, pkg->name)))
		return;

	AVLDeleteNode(&ppkg->providers, p);
	ppkg->provider_count--;

	if(!ppkg->installed && ppkg->provider_count == 1
	&& AVLSearch(keeplist, ppkg->name))
		add_keeplist(pkg_find(ppkg->providers.head->item));

	for(p = ppkg->provides.head; p; p = p->next)
	        remove_provider(pkg, pkg_find(p->item));
}

/* Initialize list of installed packages. Reads the dpkg status file
 * and builds the dependents and providers lists for each package.
 */
void init_packages(int ignore_defaults) {
	AVLNode *c, *p;
	struct package *pkg, *ppkg;

	/* Read dpkg status */
	readstatus();

	/* build provider and dependent lists for each package */
	for(c = packages->head; c; c = c->next) {
		pkg = (struct package *)c->item;
		if(pkg->installed) {
			if(ignore_defaults) { /* clear keep and nokeep hints */
				pkg->keep = 0;
				pkg->nokeep = 0;
			}
			add_provider(pkg, pkg);
			for(p = pkg->depends.head; p; p = p->next) {
				ppkg = pkg_find(p->item);
				AVLInsert(&ppkg->dependents, pkg->name);
			}
		}
	}
}

/* Count the number of orphans that would be committed to keeplist if
 * <pkg> were to be marked as a keeper.
 */
void count_orphan_depends(struct package *pkg) {
	AVLTree visited;
	AVLInitTree(&visited, (AVLCompare)symcmp);
	pkg->orphan_depends = all_depends(pkg->name, &visited, ECommittedAlternatives, EOrphansOnly);
}

/* --show-providers option */
void show_providers(symbol_t name) {
	struct package *pkg;

	if(!(pkg = pkg_find(name))) {
		printf(_("Not a dependency: \"%s\"\n"), name);
		return;
	}

	if(pkg->installed)
		printf(_("%s is an installed package.\n"), pkg->name);
	else if(pkg->providers.head)
		show_set(&pkg->providers, _("Dependency %s is met by:"), name);
	else
		printf(_("Dependency %s is not met by any package.\n"), pkg->name);
}

/* --show-depends option */
void show_depends(symbol_t name) {
	struct package *pkg;
	AVLTree visited;
	AVLNode *c, *p;

	if(!(pkg = pkg_find(name)) || !(pkg->installed || pkg->task)) {
		printf(_("Not an installed package: \"%s\"\n"), name);
		return;
	}

	AVLInitTree(&visited, (AVLCompare)strcasecmp);
	all_depends(name, &visited, ECommittedAlternatives, EAllDepends);

	for(c = visited.head; c; c = p) {
		p = c->next;
		if(c->item == name || !pkg_find(c->item)->installed)
			AVLDeleteNode(&visited, c);
	}
	if(!show_set(&visited, _("Package %s depends on:"), name))
		printf(_("Package %s has no depends.\n"), name);
}

/* --show-dependents option */
void show_dependents(symbol_t name) {
	struct package *pkg;
	AVLTree dependents;
	AVLNode *c, *p;
	int others = 0, count = 0;

	if(!(pkg = pkg_find(name))) {
		printf(_("Not an installed package: \"%s\"\n"), name);
		return;
	}

	AVLInitTree(&dependents, (AVLCompare)symcmp);
	all_dependents(name, &dependents, ECommittedAlternatives);

	for(c = dependents.head; c; c = p) {
		p = c->next;
		if (!AVLSearch(keepers, c->item)) {
			others |= pkg_find(c->item)->keep;
			AVLDeleteNode(&dependents, c);
		} else
			count++;
	}
	if (!show_set(&dependents, _("The following %d packages on the keeper list rely on %s:"), count, name))
		printf(_("Packages on the keeper list do not rely on %s.\n"), name);
	printf(_("Packages kept by default rules %s %s.\n"), others ? _("rely on") : _("do not rely on"), name);
}

/* Find a set of orphan packages that are dependencies of orphan
 * package <name> and not dependencies of any other package that is
 * not also a dependency of <name>. The set consists with a high
 * probability of packages that are closely related together and can
 * be removed as a group if the user so wishes.
 */
int related_set(symbol_t name, AVLTree *set) {
	AVLTree ancestors, eliminate;
	AVLNode *c, *p;
	struct package *pkg, *ppkg;
	int count = 0;

	AVLInitTree(&ancestors, (AVLCompare)symcmp);
	AVLInitTree(&eliminate, (AVLCompare)symcmp);

	all_depends(name, set, ECommittedAlternatives, EOrphansOnly);
	AVLInsert(&ancestors, name);
	for(c = set->head; c; c = c->next)
		all_dependents(c->item, &ancestors, ECommittedAlternatives);
	AVLDelete(&ancestors, name);

	for(c = ancestors.head; c; c = c->next) {
		if(!AVLSearch(set, c->item) && !AVLSearch(nokeepers, c->item)) {
			ppkg = pkg_find(c->item);
			if(ppkg->installed && !ppkg->nokeep)
				all_depends(c->item, &eliminate, ECommittedAlternatives, EOrphansOnly);
		}
	}

	for(c = set->head; c; c = p) {
		p = c->next;
		if(c->item == name
		|| AVLSearch(&eliminate, c->item)
		|| AVLSearch(nokeepers, c->item)
		|| (pkg = pkg_find(c->item),
			!pkg->installed || pkg->keep || pkg->nokeep))
			AVLDeleteNode(set, c);
		else
			count++;
	}

	return count;
}

/* --show-related option */
void show_related(symbol_t name) {
	struct package *pkg;
	AVLTree related;
	int count;

	if(!(pkg = pkg_find(name)) || !(pkg->installed || pkg->task)) {
		printf(_("Not an installed package: \"%s\"\n"), name);
		return;
	}

	AVLInitTree(&related, (AVLCompare)strcasecmp);
	count = related_set(name, &related);
	if(!show_set(&related, _("The following %d packages are brought in by %s:"), count, name))
		printf(_("No packages are brought in by %s.\n"), name);
}

/* Push a symbol onto undo stack */
void push_undo(symbol_t name) {
	struct undo_record *undo = xmalloc(sizeof(struct undo_record));
	undo->name = name;
	undo->prev = undo_stack;
	undo_stack = undo;
}

/* Pop a symbol from the undo stack. Returns zero if stack is empty. */
symbol_t pop_undo(void) {
	struct undo_record *undo = undo_stack;
	if(undo) {
		symbol_t name = undo->name;
		undo_stack = undo->prev;
		return name;
	}
	return 0;
}

int main(int argc, char **argv) {
	AVLTree *installs, *removes, *orphans, *related;
	AVLNode *c, *p;
	struct package *pkg, *ppkg;
	int i, option_index, ignore_defaults = 0, show_option = 0, nokeeperfile = 0, upgrade = 0, mark_only = getuid();
	int related_count;
	symbol_t show_param = 0, base, undo_entry;

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	/* global trees */
	packages  = AVLAllocTree((AVLCompare)pkgcmp);
	keepers   = AVLAllocTree((AVLCompare)strcasecmp);
	nokeepers = AVLAllocTree((AVLCompare)strcasecmp);

	/* local trees */
	postpone  = AVLAllocTree((AVLCompare)strcasecmp);
	keeplist  = AVLAllocTree((AVLCompare)symcmp);
	installs  = AVLAllocTree((AVLCompare)strcasecmp);
	removes   = AVLAllocTree((AVLCompare)strcasecmp);
	orphans   = AVLAllocTree((AVLCompare)pkgsortcmp);
	related   = AVLAllocTree((AVLCompare)strcasecmp);

	readconfig(NULL);

	while((i = getopt_long(argc, argv, "vVhqfmuc:k:niasd:e:p:r:to:", long_options, &option_index)) != EOF) {
		option_t opt;
		char *s;
		switch(i) {
			case '?':
			case 'h':
				printf(_("Usage: %s [-ck FILE] [-adefhinopqrsvV] package1 package2-\n"), argv[0]);
				printf(_("Installs package1, deinstalls package2\n\n"));
				printf(_("-v, --verbose                  Be a loudmouth\n"));
				printf(_("-V, --version                  Show version and copyright information\n"));
				printf(_("-h, --help                     Show this message\n"));
				printf(_("-q, --quiet                    Silently build keeper file\n"));
				printf(_("-f, --force                    Force system to conform to keeper file\n"));
				printf(_("-m, --mark-only                Do not install or delete packages\n"));
				printf(_("-u, --upgrade                  Try to upgrade dependencies\n"));
				printf(_("-c, --config FILE              Specify configuration file\n"));
				printf(_("-k, --keeperfile FILE          Specify keeper file\n"));
				printf(_("-n, --no-keeperfile            Don't read keeper file\n"));
				printf(_("-i, --ignore-default-rules     Ignore default rules\n"));
				printf(_("-a, --show-keepers             Show packages on the keeper list\n"));
				printf(_("-s, --show-orphans             Show orphaned packages\n"));
				printf(_("-d, --show-depends PACKAGE     Show all depends of PACKAGE\n"));
				printf(_("-e, --show-dependents PACKAGE  Show dependents of PACKAGE\n"));
				printf(_("-p, --show-providers PACKAGE   Show packages providing PACKAGE\n"));
				printf(_("-r, --show-related PACKAGE     Show packages brought in by PACKAGE\n"));
				printf(_("-t, --use-tasks                Make tasks visible as packages\n"));
				printf(_("-o, --option OPT=VAL           Override any configuration option\n"));
				printf(_("\nSee also: debfoster(8)\n"));
			case 'V':
				printf(_("debfoster %s -- Copyright (C) 2000,2001 Wessel Dankers.\n"), VERSION);
                                printf(_("Distributed under the GNU General Public License.\n"));
				exit(i=='?');
			case 'v':
				Verbose = !Verbose;
			break;
			case 'q':
				Quiet = !Quiet;
			break;
			case 'f':
				Force = !Force;
			break;
			case 'm':
				mark_only = 1;
			break;
			case 'u':
				upgrade = 1;
			break;
			case 'k':
				find_option("KeeperFile")->set(optarg);
			break;
			case 'c':
				readconfig(optarg); /* Overrides any previous config options */
			break;
			case 'i':
				ignore_defaults = 1; /* Ignore keep and nokeep hints in packages */
			break;
			case 'n':
				nokeeperfile = 1; /* Start clean, don't read keeper file */
			break;
			case 'o':
				s = strchr(optarg, '=');
				if(s > optarg) {
					*s++ = '\0';
					if((opt = find_option(optarg))) {
						opt->set(s);
						break;
					}
				}
				error_exit(ERROR_USER, _("%s: Invalid config option -- \"%s\"\n"), argv[0], optarg);
			break;
			case 'd':
			case 'e':
			case 'p':
			case 'r':
				show_param = symbol(optarg);
			case 'a':
			case 's':
				show_option = i;
				mark_only = 1;
			break;
			case 't':
				UseTasks = !UseTasks;
			break;
		}
	}

	openkeepers(show_option && argc == optind); /* Readonly, if --show-xxx option and no extra args */
	if(!nokeeperfile)
		readkeepers();

	for(i=optind; i<argc; i++) {
		char *e;
		int add;
		symbol_t name;

		if(*argv[i]) {
			e = strchr(argv[i], '\0');
			if(*--e == '-') {
				*e = '\0';
				add = 0;
			} else {
				add = 1;
			}
			if(*argv[i]) {
				name = symbol(argv[i]);
				if (add) {
					AVLInsert(keepers, name);
					AVLInsert(installs, name);
					AVLDelete(nokeepers, name);
					AVLDelete(removes, name);
				} else {
					AVLDelete(keepers, name);
					AVLDelete(installs, name);
					AVLInsert(nokeepers, name);
					AVLInsert(removes, name);
				}
				continue;
			}
		}
		error_exit(ERROR_USER, _("Not a package: \"%s\"\n"), argv[i]);
	}

	/* Sync keeper file */
	if(optind < argc)
		writekeepers();

	/* Force option will attempt to make the system conform to the keepers file.
	 * This may include installing new packages as well as removing packages.
	 */
	if(Force) {
		for(c = keepers->head; c; c = c->next)
			AVLInsert(installs, c->item);
	}

	tty_init();

	init_packages(ignore_defaults);

	/* Print information */
	switch(show_option) {
	case 'a': /* --show-keepers option */
		if(!show_set(keepers, _("The following packages are on the keeper list:")))
			printf(_("The keeper list is empty.\n"));
		show_set(nokeepers, _("The following packages are marked as non-keepers:"));
		return 0;
	case 'd': /* --show-depends option */
		show_depends(show_param);
		return 0;
	case 'e': /* --show-dependents option */
		show_dependents(show_param);
		return 0;
	case 'p': /* --show-providers option */
		show_providers(show_param);
		return 0;
	case 'r': /* --show-related option */
		AVLDelete(keepers, show_param);
		build_keeplist();
		show_related(show_param);
		return 0;
	}

	if(installs->head && !mark_only) {
		/* Prune install list */
		for(c = installs->head; c; c = p) {
			p = c->next;
			pkg = pkg_find(c->item);
			if(pkg && pkg->installed) {
				if(!upgrade)
					AVLDeleteNode(installs, c);
			} else 
				Vprintf(_("Installing package: %s\n"), (symbol_t)c->item);
		}

		/* Add all dependencies to install list (--upgrade) */
		if(upgrade) {
			AVLTree install_depends;
			AVLInitTree(&install_depends, (AVLCompare)symcmp);
			for(c = installs->head; c; c = c->next)
				if(pkg_find(c->item))
					all_depends(c->item, &install_depends, ECommittedAlternatives, EAllDepends);
			while((c = install_depends.head)) {
				if(pkg_find(c->item)->installed)
					AVLInsert(installs, c->item);
				AVLDeleteNode(&install_depends, c);
			}
		}

		/* Install packages  */
		if(installs->head) {
			/* Save a list of packages installed prior to invoking InstallCmd */
			for(c = packages->head; c; c = c->next) {
				pkg = (struct package *)c->item;
				if(pkg->installed)
					AVLInsert(keeplist, pkg->name);
			}

			i = xargs(installs, InstallCmd);
			if(i) return i;

			/* Read dpkg status */
			init_packages(ignore_defaults);

			/* find out which packages actually were installed */
			AVLFreeNodes(installs);
			for(c = packages->head; c; c = c->next) {
				pkg = (struct package *)c->item;
				if(!AVLSearch(keeplist, pkg->name)) {
					AVLInsert(installs, pkg->name);
					AVLDelete(removes, pkg->name);
				}
			}
		}
	}

	if(!mark_only)
		prune_keepers();

	build_keeplist();

	for(;;) {
		/* Build a list of all packages that are not anchored either
		 * by the default rules or the keeper list and sort it so that
		 * the package with the most orphans dangling from it comes
		 * up first.
		 */
		AVLFreeNodes(orphans);
		for(c = packages->head; c; c = c->next) {
			pkg = (struct package *)c->item;
			if ((pkg->installed || pkg->task) && !AVLSearch(keeplist, pkg->name)) {
				count_orphan_depends(pkg);
				AVLInsert(orphans, pkg);
			}
		}

		i = 0;
		while((c = orphans->head)) {
	        	pkg = (struct package *)c->item;

			/* Has this one found a home? */
			if(AVLSearch(keeplist, pkg->name)) {
				AVLDeleteNode(orphans, c);
				continue;
			}

			/* Re-shuffle the pack for optimal ordering. */
			AVLUnlinkNode(orphans, c);
			AVLInitNode(c, pkg);

			/* Handle GuessDepends here. Guesses don't go into keeper file. */
			if(!ignore_defaults && (base = guessbase(pkg->name)) &&
					(ppkg = pkg_find(base)) && ppkg->installed) {
				if(AVLSearch(keeplist, ppkg->name)) {
					add_keeplist(pkg);
					continue;
				}
				if(AVLSearch(removes, ppkg->name)) {
					/* Base package was deleted. Set nokeep hint. */
					pkg->nokeep = 1;
				} else {
					/* Don't know yet. Reposition after the base package. */
					pkg->orphan_depends = ppkg->orphan_depends;
				}
			} else
				count_orphan_depends(pkg); /* Count may change due to user choises */

			AVLInsertNode(orphans, c);
			if(orphans->head != c)
				continue;

			/* Re-shuffle complete. We're still go. */
			AVLDeleteNode(orphans, c);

			/* If the package was just installed by debfoster it's a keeper.
			 * This catches packages that were installed by apt because the user
			 * gave a regular expression on the command line.
			 */
			if(AVLSearch(installs, pkg->name)) {
				AVLInsert(keepers, pkg->name);
				add_keeplist(pkg);
				AVLDelete(nokeepers, pkg->name);
				continue;
			}

			if(Force || pkg->nokeep || AVLSearch(nokeepers, pkg->name)) {
				AVLInsert(removes, pkg->name);
				remove_provider(pkg, pkg);
				continue;
			}

			/* Ignore a task if it doesn't bring at least two packages in */
			if(pkg->task && !pkg->installed) {
				if(related_set(pkg->name, related) < 2)
					continue;
				AVLFreeNodes(related);
			}

			if(Quiet) {
				AVLInsert(keepers, pkg->name);
				add_keeplist(pkg);
				Vprintf(_("Keeping package: %s\n"), pkg->name);
				continue;
			}

			if(show_option == 's') {  /* Treat --show-orphans option as skip */
				AVLInsert(postpone, pkg->name);
				add_keeplist(pkg);
				continue;
			}

			related_count = related_set(pkg->name, related);
			show_set(related, _("\n%s is keeping the following %d packages installed:"), pkg->name, related_count);

			do {
				switch((i = tty_ask(_("ynpshiuqx?"), _("Keep %s? [Ynpsiuqx?], [H]elp: "), pkg->name))) {
					case 4: /* h */
                                                printf("\n");
						printf(_("  Yes        Keep %s. [default]\n"), pkg->name);
						printf(_("  No         Delete %s.\n"), pkg->name);
						printf(_("  Prune      Delete %s and the packages it is keeping installed.\n"), pkg->name);
						printf(_("  Skip       Skip this question.\n"));
						printf(_("  Help       Print this message.\n"));
						printf(_("  Info or ?  Show information about %s.\n"), pkg->name);
						printf(_("  Undo       Undo last response.\n"));
						printf(_("  Quit       Exit without removing packages.\n"));
						printf(_("  Exit       Remove unwanted packages and exit.\n"));
                                                printf("\n");
                                                i = -1;
					break;
					case 9: /* ? */
                                        case 5: /* i */
						if(pkg->installed)
							showinfo(pkg->name);
						else if(pkg->task) {
							/* Tasksel has no command line option for printing task info
							 * so we just show the packages that belong to it.
							 */
							show_set(&pkg->depends,
								_("This is a task containing the following installed packages:"));
						}
                                                i = -1;
					break;
					case 2: /* p */
						for(c = related->head; c; c = c->next)
							AVLInsert(nokeepers, c->item);
					case 1: /* n */
						AVLInsert(nokeepers, pkg->name);
						AVLInsert(removes, pkg->name);
						remove_provider(pkg, pkg);
					break;
					case 0: /* y */
						AVLInsert(keepers, pkg->name);
						add_keeplist(pkg);
					break;
					case 3: /* s */
						AVLInsert(postpone, pkg->name);
						add_keeplist(pkg);
					break;
					case 6: /* u */
						if(!undo_stack) {
							printf(_("\aNothing to undo.\n"));
							i = -1;
							break;
						} 
						while((undo_entry = pop_undo())
						&& undo_entry != symbol("MARK")) {
							AVLDelete(keepers, undo_entry);
							AVLDelete(nokeepers, undo_entry);
							AVLDelete(removes, undo_entry);
							AVLDelete(postpone, undo_entry);
						}
						AVLFreeNodes(orphans);
					break;
					case 7: /* q */
                        switch((i = tty_ask(_("ny"), _("Exit without saving? [yN?]: ")))) {
                            case 0: /* n */
                                i = -1;
                                break;
                            case 1: /* y */
                                closekeepers();
                                return 0;
                        }
					case 8: /* x */
						AVLFreeNodes(orphans);
					break;
				}
			} while(i < 0);

			if(i < 4) { /* Yes, No, Purge or Skip */
				push_undo(symbol("MARK"));
				push_undo(pkg->name);
				if(i == 2) { /* Purge */
					for(c = related->head; c; c = c->next)
						push_undo(c->item);
				}
			}
			AVLFreeNodes(related);
		}

		if(show_option == 's') { /* --show-orphans option */
			if(!show_set(postpone, _("The following packages have been orphaned:")))
				printf(_("There are no orphaned packages.\n"));
			return 0;
		}

		/* Undo? Start over. */
		if(i == 6) { /* Undo */
			init_packages(ignore_defaults);
			build_keeplist();
			continue;
		}

		/* Save keeper file */
		writekeepers();

		if(!mark_only) {
			for(c = removes->head; c; c = p) {
				p = c->next;
				pkg = pkg_find(c->item);
				if (!pkg || !pkg->installed) {
					if(!pkg || !pkg->task)
						Vprintf(_("Package is not installed: %s\n"), (symbol_t)c->item);
					AVLDeleteNode(removes, c);
				} else if(AVLSearch(keeplist, pkg->name)) {
					Vprintf(_("Package is a dependency: %s\n"), pkg->name);
					AVLDeleteNode(removes, c);
				} else
					Vprintf(_("Removing package: %s\n"), pkg->name);
			}
			/* Remove packages */
			if(removes->head) {
				i = xargs(removes, RemoveCmd);
				if(i) return i;
				AVLFreeNodes(removes);
			}
		}

		break; /* done! */
	}

	closekeepers();

	return 0;
}