summaryrefslogtreecommitdiffstats
path: root/src/global/tok822_tree.c
blob: a66cf115675471fe3ef2a8e4bb8a60f239483faa (plain)
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
/*++
/* NAME
/*	tok822_tree 3
/* SUMMARY
/*	assorted token tree operators
/* SYNOPSIS
/*	#include <tok822.h>
/*
/*	TOK822	*tok822_append(t1, t2)
/*	TOK822	*t1;
/*	TOK822	*t2;
/*
/*	TOK822	*tok822_prepend(t1, t2)
/*	TOK822	*t1;
/*	TOK822	*t2;
/*
/*	TOK822	*tok822_cut_before(tp)
/*	TOK822	*tp;
/*
/*	TOK822	*tok822_cut_after(tp)
/*	TOK822	*tp;
/*
/*	TOK822	*tok822_unlink(tp)
/*	TOK822	*tp;
/*
/*	TOK822	*tok822_sub_append(t1, t2)
/*	TOK822	*t1;
/*
/*	TOK822	*tok822_sub_prepend(t1, t2)
/*	TOK822	*t1;
/*	TOK822	*t2;
/*
/*	TOK822	*tok822_sub_keep_before(t1, t2)
/*	TOK822	*tp;
/*
/*	TOK822	*tok822_sub_keep_after(t1, t2)
/*	TOK822	*tp;
/*
/*	int	tok822_apply(list, type, action)
/*	TOK822	*list;
/*	int	type;
/*	int	(*action)(TOK822 *token);
/*
/*	int	tok822_grep(list, type)
/*	TOK822	*list;
/*	int	type;
/*
/*	TOK822	*tok822_free_tree(tp)
/*	TOK822	*tp;
/* DESCRIPTION
/*	This module manipulates trees of token structures. Trees grow
/*	to the right or downwards. Operators are provided to cut and
/*	combine trees in various manners.
/*
/*	tok822_append() appends the token list \fIt2\fR to the right
/*	of token list \fIt1\fR. The result is the last token in \fIt2\fR.
/*	The appended list inherits the \fIowner\fR attribute from \fIt1\fR.
/*	The parent node, if any, is not updated.
/*
/*	tok822_prepend() inserts the token list \fIt2\fR to the left
/*	of token \fIt1\fR. The result is the last token in \fIt2\fR.
/*	The appended list inherits the \fIowner\fR attribute from \fIt1\fR.
/*	The parent node, if any, is not updated.
/*
/*	tok822_cut_before() breaks a token list on the left side of \fItp\fR
/*	and returns the left neighbor of \tItp\fR.
/*
/*	tok822_cut_after() breaks a token list on the right side of \fItp\fR
/*	and returns the right neighbor of \tItp\fR.
/*
/*	tok822_unlink() disconnects a token from its left and right neighbors
/*	and returns the left neighbor of \tItp\fR.
/*
/*	tok822_sub_append() appends the token list \fIt2\fR to the right
/*	of the token list below \fIt1\fR. The result is the last token
/*	in \fIt2\fR.
/*
/*	tok822_sub_prepend() prepends the token list \fIt2\fR to the left
/*	of the token list below \fIt1\fR. The result is the last token
/*	in \fIt2\fR.
/*
/*	tok822_sub_keep_before() keeps the token list below \fIt1\fR on the
/*	left side of \fIt2\fR and returns the tail of the disconnected list.
/*
/*	tok822_sub_keep_after() keeps the token list below \fIt1\fR on the
/*	right side of \fIt2\fR and returns the head of the disconnected list.
/*
/*	tok822_apply() applies the specified action routine to all tokens
/*	matching the given type (to all tokens when a null type is given).
/*	Processing terminates when the action routine returns a non-zero
/*	value. The result is the last result returned by the action routine.
/*	tok822_apply() does not traverse vertical links.
/*
/*	tok822_grep() returns a null-terminated array of pointers to tokens
/*	matching the specified type (all tokens when a null type is given).
/*	tok822_grep() does not traverse vertical links. The result must be
/*	given to myfree().
/*
/*	tok822_free_tree() destroys a tree of token structures and
/*	conveniently returns a null pointer.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>

/* Utility library. */

#include <mymalloc.h>
#include <vstring.h>

/* Global library. */

#include "tok822.h"

/* tok822_append - insert token list, return end of inserted list */

TOK822 *tok822_append(TOK822 *t1, TOK822 *t2)
{
    TOK822 *next = t1->next;

    t1->next = t2;
    t2->prev = t1;

    t2->owner = t1->owner;
    while (t2->next)
	(t2 = t2->next)->owner = t1->owner;

    t2->next = next;
    if (next)
	next->prev = t2;
    return (t2);
}

/* tok822_prepend - insert token list, return end of inserted list */

TOK822 *tok822_prepend(TOK822 *t1, TOK822 *t2)
{
    TOK822 *prev = t1->prev;

    if (prev)
	prev->next = t2;
    t2->prev = prev;

    t2->owner = t1->owner;
    while (t2->next)
	(t2 = t2->next)->owner = t1->owner;

    t2->next = t1;
    t1->prev = t2;
    return (t2);
}

/* tok822_cut_before - split list before token, return predecessor token */

TOK822 *tok822_cut_before(TOK822 *tp)
{
    TOK822 *prev = tp->prev;

    if (prev) {
	prev->next = 0;
	tp->prev = 0;
    }
    return (prev);
}

/* tok822_cut_after - split list after token, return successor token */

TOK822 *tok822_cut_after(TOK822 *tp)
{
    TOK822 *next = tp->next;

    if (next) {
	next->prev = 0;
	tp->next = 0;
    }
    return (next);
}

/* tok822_unlink - take token away from list, return predecessor token */

TOK822 *tok822_unlink(TOK822 *tp)
{
    TOK822 *prev = tp->prev;
    TOK822 *next = tp->next;

    if (prev)
	prev->next = next;
    if (next)
	next->prev = prev;
    tp->prev = tp->next = 0;
    return (prev);
}

/* tok822_sub_append - append sublist, return end of appended list */

TOK822 *tok822_sub_append(TOK822 *t1, TOK822 *t2)
{
    if (t1->head) {
	return (t1->tail = tok822_append(t1->tail, t2));
    } else {
	t1->head = t2;
	t2->owner = t1;
	while (t2->next)
	    (t2 = t2->next)->owner = t1;
	return (t1->tail = t2);
    }
}

/* tok822_sub_prepend - prepend sublist, return end of prepended list */

TOK822 *tok822_sub_prepend(TOK822 *t1, TOK822 *t2)
{
    TOK822 *tp;

    if (t1->head) {
	tp = tok822_prepend(t1->head, t2);
	t1->head = t2;
	return (tp);
    } else {
	t1->head = t2;
	t2->owner = t1;
	while (t2->next)
	    (t2 = t2->next)->owner = t1;
	return (t1->tail = t2);
    }
}

/* tok822_sub_keep_before - cut sublist, return tail of disconnected list */

TOK822 *tok822_sub_keep_before(TOK822 *t1, TOK822 *t2)
{
    TOK822 *tail = t1->tail;

    if ((t1->tail = tok822_cut_before(t2)) == 0)
	t1->head = 0;
    return (tail);
}

/* tok822_sub_keep_after - cut sublist, return head of disconnected list */

TOK822 *tok822_sub_keep_after(TOK822 *t1, TOK822 *t2)
{
    TOK822 *head = t1->head;

    if ((t1->head = tok822_cut_after(t2)) == 0)
	t1->tail = 0;
    return (head);
}

/* tok822_free_tree - destroy token tree */

TOK822 *tok822_free_tree(TOK822 *tp)
{
    TOK822 *next;

    for (/* void */; tp != 0; tp = next) {
	if (tp->head)
	    tok822_free_tree(tp->head);
	next = tp->next;
	tok822_free(tp);
    }
    return (0);
}

/* tok822_apply - apply action to specified tokens */

int     tok822_apply(TOK822 *tree, int type, TOK822_ACTION action)
{
    TOK822 *tp;
    int     result = 0;

    for (tp = tree; tp; tp = tp->next) {
	if (type == 0 || tp->type == type)
	    if ((result = action(tp)) != 0)
		break;
    }
    return (result);
}

/* tok822_grep - list matching tokens */

TOK822 **tok822_grep(TOK822 *tree, int type)
{
    TOK822 **list;
    TOK822 *tp;
    int     count;

    for (count = 0, tp = tree; tp; tp = tp->next)
	if (type == 0 || tp->type == type)
	    count++;

    list = (TOK822 **) mymalloc(sizeof(*list) * (count + 1));

    for (count = 0, tp = tree; tp; tp = tp->next)
	if (type == 0 || tp->type == type)
	    list[count++] = tp;

    list[count] = 0;
    return (list);
}