]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | $Header: /cvs/src/tdl/purge.c,v 1.6.2.1 2004/01/07 00:09:05 richard Exp $ | |
3 | ||
4 | tdl - A console program for managing to-do lists | |
5 | Copyright (C) 2001-2004 Richard P. Curnow | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
20 | */ | |
21 | ||
22 | #include <time.h> | |
23 | #include "tdl.h" | |
24 | ||
25 | static void purge_from_bottom_up(struct links *x)/*{{{*/ | |
26 | { | |
27 | struct node *y, *ny; | |
28 | for (y = x->next; y != (struct node *) x; y = ny) { | |
29 | ||
30 | /* Calculate this now in case y gets deleted later */ | |
31 | ny = y->chain.next; | |
32 | ||
33 | if (has_kids(y)) { | |
34 | purge_from_bottom_up(&y->kids); | |
35 | } | |
36 | ||
37 | if (y->flag) { | |
38 | /* If the node still has children, just keep quite and don't delete it. | |
39 | * The idea of the 'purge' command is just a fast bulk delete of all | |
40 | * old stuff under certain tasks, so this is OK */ | |
41 | if (!has_kids(y)) { | |
42 | /* Just unlink from the chain for now, don't bother about full clean up. */ | |
43 | struct node *next, *prev; | |
44 | next = y->chain.next; | |
45 | prev = y->chain.prev; | |
46 | prev->chain.next = next; | |
47 | next->chain.prev = prev; | |
48 | } | |
49 | } | |
50 | } | |
51 | } | |
52 | /*}}}*/ | |
53 | static void scan_from_top_down(struct links *x, time_t then)/*{{{*/ | |
54 | { | |
55 | struct node *y; | |
56 | for (y = x->next; y != (struct node *) x; y = y->chain.next) { | |
57 | if ((y->done > 0) && (y->done < then)) y->flag = 1; | |
58 | scan_from_top_down(&y->kids, then); | |
59 | } | |
60 | } | |
61 | /*}}}*/ | |
62 | ||
63 | int process_purge(char **x)/*{{{*/ | |
64 | { | |
65 | int argc; | |
66 | time_t now, then; | |
67 | char *d; | |
68 | int error; | |
69 | struct node *narrow_top; | |
70 | struct links *to_purge_from; | |
71 | ||
72 | narrow_top = get_narrow_top(); | |
73 | to_purge_from = narrow_top ? &narrow_top->kids : ⊤ | |
74 | ||
75 | argc = count_args(x); | |
76 | if (argc < 1) { | |
77 | fprintf(stderr, "Usage: purge <interval> <entry_index> ...\n"); | |
78 | return -1; | |
79 | } | |
80 | ||
81 | d = x[0]; | |
82 | if (*d == '@') d++; | |
83 | now = time(NULL); | |
84 | then = parse_date(d, now, 0, &error); | |
85 | if (error < 0) return error; | |
86 | x++; | |
87 | ||
88 | clear_flags(to_purge_from); | |
89 | now = time(NULL); | |
90 | ||
91 | if (!*x) { | |
92 | /* If no indices given, do whole database */ | |
93 | scan_from_top_down(to_purge_from, then); | |
94 | } else { | |
95 | while (*x) { | |
96 | struct node *n; | |
97 | n = lookup_node(*x, 0, NULL); | |
98 | if (!n) return -1; | |
99 | if ((n->done > 0) && (n->done < then)) n->flag = 1; | |
100 | scan_from_top_down(&n->kids, then); | |
101 | x++; | |
102 | } | |
103 | } | |
104 | ||
105 | purge_from_bottom_up(to_purge_from); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | /*}}}*/ |