-
Notifications
You must be signed in to change notification settings - Fork 2
/
purge.c
109 lines (92 loc) · 2.91 KB
/
purge.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
/*
$Header: /cvs/src/tdl/purge.c,v 1.6.2.1 2004/01/07 00:09:05 richard Exp $
tdl - A console program for managing to-do lists
Copyright (C) 2001-2004 Richard P. Curnow
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <time.h>
#include "tdl.h"
static void purge_from_bottom_up(struct links *x)/*{{{*/
{
struct node *y, *ny;
for (y = x->next; y != (struct node *) x; y = ny) {
/* Calculate this now in case y gets deleted later */
ny = y->chain.next;
if (has_kids(y)) {
purge_from_bottom_up(&y->kids);
}
if (y->flag) {
/* If the node still has children, just keep quite and don't delete it.
* The idea of the 'purge' command is just a fast bulk delete of all
* old stuff under certain tasks, so this is OK */
if (!has_kids(y)) {
/* Just unlink from the chain for now, don't bother about full clean up. */
struct node *next, *prev;
next = y->chain.next;
prev = y->chain.prev;
prev->chain.next = next;
next->chain.prev = prev;
}
}
}
}
/*}}}*/
static void scan_from_top_down(struct links *x, time_t then)/*{{{*/
{
struct node *y;
for (y = x->next; y != (struct node *) x; y = y->chain.next) {
if ((y->done > 0) && (y->done < then)) y->flag = 1;
scan_from_top_down(&y->kids, then);
}
}
/*}}}*/
int process_purge(char **x)/*{{{*/
{
int argc;
time_t now, then;
char *d;
int error;
struct node *narrow_top;
struct links *to_purge_from;
narrow_top = get_narrow_top();
to_purge_from = narrow_top ? &narrow_top->kids : ⊤
argc = count_args(x);
if (argc < 1) {
fprintf(stderr, "Usage: purge <interval> <entry_index> ...\n");
return -1;
}
d = x[0];
if (*d == '@') d++;
now = time(NULL);
then = parse_date(d, now, 0, &error);
if (error < 0) return error;
x++;
clear_flags(to_purge_from);
now = time(NULL);
if (!*x) {
/* If no indices given, do whole database */
scan_from_top_down(to_purge_from, then);
} else {
while (*x) {
struct node *n;
n = lookup_node(*x, 0, NULL);
if (!n) return -1;
if ((n->done > 0) && (n->done < then)) n->flag = 1;
scan_from_top_down(&n->kids, then);
x++;
}
}
purge_from_bottom_up(to_purge_from);
return 0;
}
/*}}}*/