1*51157c88Srin /* $NetBSD: thread.c,v 1.16 2023/08/23 03:49:00 rin Exp $ */
2f3098750Schristos
3f3098750Schristos /*-
4f3098750Schristos * Copyright (c) 2006 The NetBSD Foundation, Inc.
5f3098750Schristos * All rights reserved.
6f3098750Schristos *
7f3098750Schristos * This code is derived from software contributed to The NetBSD Foundation
8f3098750Schristos * by Anon Ymous.
9f3098750Schristos *
10f3098750Schristos * Redistribution and use in source and binary forms, with or without
11f3098750Schristos * modification, are permitted provided that the following conditions
12f3098750Schristos * are met:
13f3098750Schristos * 1. Redistributions of source code must retain the above copyright
14f3098750Schristos * notice, this list of conditions and the following disclaimer.
15f3098750Schristos * 2. Redistributions in binary form must reproduce the above copyright
16f3098750Schristos * notice, this list of conditions and the following disclaimer in the
17f3098750Schristos * documentation and/or other materials provided with the distribution.
18f3098750Schristos *
19f3098750Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f3098750Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f3098750Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f3098750Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f3098750Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f3098750Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f3098750Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f3098750Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f3098750Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f3098750Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f3098750Schristos * POSSIBILITY OF SUCH DAMAGE.
30f3098750Schristos */
31f3098750Schristos
32f3098750Schristos /*
33f3098750Schristos * This module contains the threading and sorting routines.
34f3098750Schristos */
35f3098750Schristos
36f3098750Schristos #ifdef THREAD_SUPPORT
37f3098750Schristos
38f3098750Schristos #include <sys/cdefs.h>
39f3098750Schristos #ifndef __lint__
40*51157c88Srin __RCSID("$NetBSD: thread.c,v 1.16 2023/08/23 03:49:00 rin Exp $");
41f3098750Schristos #endif /* not __lint__ */
42f3098750Schristos
43f3098750Schristos #include <assert.h>
44f3098750Schristos #include <ctype.h>
45f3098750Schristos #include <stdio.h>
46f3098750Schristos #include <stdlib.h>
47f3098750Schristos #include <util.h>
48f3098750Schristos
49f3098750Schristos #include "def.h"
50f3098750Schristos #include "glob.h"
51f3098750Schristos #include "extern.h"
52f3098750Schristos #include "format.h"
53f3098750Schristos #include "thread.h"
54f3098750Schristos
55f3098750Schristos
56f3098750Schristos struct thread_s {
57f3098750Schristos struct message *t_head; /* head of the thread */
58f3098750Schristos struct message **t_msgtbl; /* message array indexed by msgnum */
59f3098750Schristos int t_msgCount; /* count of messages in thread */
60f3098750Schristos };
61f3098750Schristos #define THREAD_INIT {NULL, NULL, 0}
62f3098750Schristos
63f3098750Schristos typedef int state_t;
64f3098750Schristos #define S_STATE_INIT 0
65f3098750Schristos #define S_EXPOSE 1 /* flag to expose the thread */
66f3098750Schristos #define S_RESTRICT 2 /* flag to restrict to tagged messages */
67f3098750Schristos #define S_IS_EXPOSE(a) ((a) & S_EXPOSE)
68f3098750Schristos #define S_IS_RESTRICT(a) ((a) & S_RESTRICT)
69f3098750Schristos
70f3098750Schristos /* XXX - this isn't really a thread */
71f3098750Schristos static struct thread_s message_array = THREAD_INIT; /* the basic message array */
72f3098750Schristos static struct thread_s current_thread = THREAD_INIT; /* the current thread */
73f3098750Schristos
74f3098750Schristos static state_t state = S_STATE_INIT; /* the current state */
75f3098750Schristos
76f3098750Schristos /*
77f3098750Schristos * A state hook used by the format module.
78f3098750Schristos */
79f3098750Schristos PUBLIC int
thread_hidden(void)80f3098750Schristos thread_hidden(void)
81f3098750Schristos {
82f3098750Schristos return !S_IS_EXPOSE(state);
83f3098750Schristos }
84f3098750Schristos
85f3098750Schristos /************************************************************************
86f3098750Schristos * Debugging stuff that should evaporate eventually.
87f3098750Schristos */
88f3098750Schristos #ifdef THREAD_DEBUG
89f3098750Schristos static void
show_msg(struct message * mp)90f3098750Schristos show_msg(struct message *mp)
91f3098750Schristos {
92f3098750Schristos if (mp == NULL)
93f3098750Schristos return;
94f3098750Schristos /*
95f3098750Schristos * Arg! '%p' doesn't like the '0' modifier.
96f3098750Schristos */
973a9442afSchristos (void)printf("%3d (%p):"
983a9442afSchristos " flink=%p blink=%p clink=%p plink=%p"
99f3098750Schristos " depth=%d flags=0x%03x\n",
1003a9442afSchristos mp->m_index, mp,
1013a9442afSchristos mp->m_flink, mp->m_blink, mp->m_clink, mp->m_plink,
1023a9442afSchristos mp->m_depth, mp->m_flag);
103f3098750Schristos }
104f3098750Schristos
105f3098750Schristos #ifndef __lint__
106f3098750Schristos __unused
107f3098750Schristos static void
show_thread(struct message * mp)108f3098750Schristos show_thread(struct message *mp)
109f3098750Schristos {
110f3098750Schristos (void)printf("current_thread.t_head=%p\n", current_thread.t_head);
111f3098750Schristos for (/*EMPTY*/; mp; mp = next_message(mp))
112f3098750Schristos show_msg(mp);
113f3098750Schristos }
114f3098750Schristos #endif
115f3098750Schristos
116f3098750Schristos PUBLIC int
thread_showcmd(void * v)117f3098750Schristos thread_showcmd(void *v)
118f3098750Schristos {
119f3098750Schristos int *ip;
120f3098750Schristos
121f3098750Schristos (void)printf("current_thread.t_head=%p\n", current_thread.t_head);
122f3098750Schristos for (ip = v; *ip; ip++)
123f3098750Schristos show_msg(get_message(*ip));
124f3098750Schristos
125f3098750Schristos return 0;
126f3098750Schristos }
127f3098750Schristos #endif /* THREAD_DEBUG */
128f3098750Schristos
129f3098750Schristos /*************************************************************************
130f3098750Schristos * tag/restrict routines
131f3098750Schristos */
132f3098750Schristos
133f3098750Schristos /*
134f3098750Schristos * Return TRUE iff all messages forward or below this one are tagged.
135f3098750Schristos */
136f3098750Schristos static int
is_tagged_core(struct message * mp)137f3098750Schristos is_tagged_core(struct message *mp)
138f3098750Schristos {
13917a5e239Schristos if (S_IS_EXPOSE(state))
14017a5e239Schristos return 1;
14117a5e239Schristos
142f3098750Schristos for (/*EMPTY*/; mp; mp = mp->m_flink)
143f3098750Schristos if ((mp->m_flag & MTAGGED) == 0 ||
144f3098750Schristos is_tagged_core(mp->m_clink) == 0)
145f3098750Schristos return 0;
146f3098750Schristos return 1;
147f3098750Schristos }
148f3098750Schristos
149f3098750Schristos static int
is_tagged(struct message * mp)150f3098750Schristos is_tagged(struct message *mp)
151f3098750Schristos {
15217a5e239Schristos return mp->m_flag & MTAGGED && is_tagged_core(mp->m_clink);
153f3098750Schristos }
154f3098750Schristos
155f3098750Schristos /************************************************************************
156f3098750Schristos * These are the core routines to access messages via the links used
157f3098750Schristos * everywhere outside this module and fio.c.
158f3098750Schristos */
159f3098750Schristos
160f3098750Schristos static int
has_parent(struct message * mp)161f3098750Schristos has_parent(struct message *mp)
162f3098750Schristos {
163f3098750Schristos return mp->m_plink != NULL &&
164f3098750Schristos mp->m_plink->m_clink != current_thread.t_head;
165f3098750Schristos }
166f3098750Schristos
167f3098750Schristos static struct message *
next_message1(struct message * mp)168f3098750Schristos next_message1(struct message *mp)
169f3098750Schristos {
170f3098750Schristos if (mp == NULL)
171f3098750Schristos return NULL;
172f3098750Schristos
173f3098750Schristos if (S_IS_EXPOSE(state) == 0)
174f3098750Schristos return mp->m_flink;
175f3098750Schristos
176f3098750Schristos if (mp->m_clink)
177f3098750Schristos return mp->m_clink;
178f3098750Schristos
179f3098750Schristos while (mp->m_flink == NULL && has_parent(mp))
180f3098750Schristos mp = mp->m_plink;
181f3098750Schristos
182f3098750Schristos return mp->m_flink;
183f3098750Schristos }
184f3098750Schristos
185f3098750Schristos static struct message *
prev_message1(struct message * mp)186f3098750Schristos prev_message1(struct message *mp)
187f3098750Schristos {
188f3098750Schristos if (mp == NULL)
189f3098750Schristos return NULL;
190f3098750Schristos
191f3098750Schristos if (S_IS_EXPOSE(state) && mp->m_blink == NULL && has_parent(mp))
192f3098750Schristos return mp->m_plink;
193f3098750Schristos
194f3098750Schristos return mp->m_blink;
195f3098750Schristos }
196f3098750Schristos
197f3098750Schristos PUBLIC struct message *
next_message(struct message * mp)198f3098750Schristos next_message(struct message *mp)
199f3098750Schristos {
200f3098750Schristos if (S_IS_RESTRICT(state) == 0)
201f3098750Schristos return next_message1(mp);
202f3098750Schristos
203f3098750Schristos while ((mp = next_message1(mp)) != NULL && is_tagged(mp))
204f3098750Schristos continue;
205f3098750Schristos
206f3098750Schristos return mp;
207f3098750Schristos }
208f3098750Schristos
209f3098750Schristos PUBLIC struct message *
prev_message(struct message * mp)210f3098750Schristos prev_message(struct message *mp)
211f3098750Schristos {
212f3098750Schristos if (S_IS_RESTRICT(state) == 0)
213f3098750Schristos return prev_message1(mp);
214f3098750Schristos
215f3098750Schristos while ((mp = prev_message1(mp)) != NULL && is_tagged(mp))
216f3098750Schristos continue;
217f3098750Schristos
218f3098750Schristos return mp;
219f3098750Schristos }
220f3098750Schristos
221f3098750Schristos static struct message *
first_message(struct message * mp)222f3098750Schristos first_message(struct message *mp)
223f3098750Schristos {
224f3098750Schristos if (S_IS_RESTRICT(state) && is_tagged(mp))
225f3098750Schristos mp = next_message(mp);
226f3098750Schristos return mp;
227f3098750Schristos }
228f3098750Schristos
229f3098750Schristos PUBLIC struct message *
get_message(int msgnum)230f3098750Schristos get_message(int msgnum)
231f3098750Schristos {
232f3098750Schristos struct message *mp;
233f3098750Schristos
234f3098750Schristos if (msgnum < 1 || msgnum > current_thread.t_msgCount)
235f3098750Schristos return NULL;
236f3098750Schristos mp = current_thread.t_msgtbl[msgnum - 1];
237f3098750Schristos assert(mp->m_index == msgnum);
238f3098750Schristos return mp;
239f3098750Schristos }
240f3098750Schristos
241f3098750Schristos PUBLIC int
get_msgnum(struct message * mp)242f3098750Schristos get_msgnum(struct message *mp)
243f3098750Schristos {
244f3098750Schristos return mp ? mp->m_index : 0;
245f3098750Schristos }
246f3098750Schristos
247f3098750Schristos PUBLIC int
get_msgCount(void)248f3098750Schristos get_msgCount(void)
249f3098750Schristos {
250f3098750Schristos return current_thread.t_msgCount;
251f3098750Schristos }
252f3098750Schristos
253f3098750Schristos PUBLIC int
get_abs_msgCount(void)254f3098750Schristos get_abs_msgCount(void)
255f3098750Schristos {
256f3098750Schristos return message_array.t_msgCount;
257f3098750Schristos }
258f3098750Schristos
259f3098750Schristos PUBLIC struct message *
get_abs_message(int msgnum)260f3098750Schristos get_abs_message(int msgnum)
261f3098750Schristos {
262f3098750Schristos if (msgnum < 1 || msgnum > message_array.t_msgCount)
263f3098750Schristos return NULL;
264f3098750Schristos
265f3098750Schristos return &message_array.t_head[msgnum - 1];
266f3098750Schristos }
267f3098750Schristos
268f3098750Schristos PUBLIC struct message *
next_abs_message(struct message * mp)269f3098750Schristos next_abs_message(struct message *mp)
270f3098750Schristos {
271f3098750Schristos int i;
272f3098750Schristos
27317a5e239Schristos i = (int)(mp - message_array.t_head);
274f3098750Schristos
275f3098750Schristos if (i < 0 || i + 1 >= message_array.t_msgCount)
276f3098750Schristos return NULL;
277f3098750Schristos
278f3098750Schristos return &message_array.t_head[i + 1];
279f3098750Schristos }
280f3098750Schristos
281f3098750Schristos /************************************************************************/
282f3098750Schristos /*
283f3098750Schristos * routines to handle the recursion of commands.
284f3098750Schristos */
285f3098750Schristos PUBLIC int
do_recursion(void)286f3098750Schristos do_recursion(void)
287f3098750Schristos {
288f3098750Schristos return S_IS_EXPOSE(state) == 0 && value(ENAME_RECURSIVE_CMDS) != NULL;
289f3098750Schristos }
290f3098750Schristos
291f3098750Schristos static int
thread_recursion_flist(struct message * mp,int (* fn)(struct message *,void *),void * args)292f3098750Schristos thread_recursion_flist(struct message *mp, int (*fn)(struct message *, void *), void *args)
293f3098750Schristos {
294f3098750Schristos int retval;
295f3098750Schristos for (/*EMPTY*/; mp; mp = mp->m_flink) {
296f3098750Schristos if (S_IS_RESTRICT(state) && is_tagged(mp))
297f3098750Schristos continue;
298f3098750Schristos if ((retval = fn(mp, args)) != 0 ||
299f3098750Schristos (retval = thread_recursion_flist(mp->m_clink, fn, args)) != 0)
300f3098750Schristos return retval;
301f3098750Schristos }
302f3098750Schristos
303f3098750Schristos return 0;
304f3098750Schristos }
305f3098750Schristos
306f3098750Schristos PUBLIC int
thread_recursion(struct message * mp,int (* fn)(struct message *,void *),void * args)307f3098750Schristos thread_recursion(struct message *mp, int (*fn)(struct message *, void *), void *args)
308f3098750Schristos {
309f3098750Schristos int retval;
310f3098750Schristos
311f3098750Schristos assert(mp != NULL);
312f3098750Schristos
313f3098750Schristos if ((retval = fn(mp, args)) != 0)
314f3098750Schristos return retval;
315f3098750Schristos
316f3098750Schristos if (do_recursion() &&
317f3098750Schristos (retval = thread_recursion_flist(mp->m_clink, fn, args)) != 0)
318f3098750Schristos return retval;
319f3098750Schristos
320f3098750Schristos return 0;
321f3098750Schristos }
322f3098750Schristos
323f3098750Schristos /************************************************************************
324f3098750Schristos * A hook for sfmtfield() in format.c. It is the only place outside
325f3098750Schristos * this module that the m_depth is known.
326f3098750Schristos */
327f3098750Schristos PUBLIC int
thread_depth(void)328f3098750Schristos thread_depth(void)
329f3098750Schristos {
330f3098750Schristos return current_thread.t_head ? current_thread.t_head->m_depth : 0;
331f3098750Schristos }
332f3098750Schristos
333f3098750Schristos /************************************************************************/
334f3098750Schristos
335f3098750Schristos static int
reindex_core(struct message * mp)336f3098750Schristos reindex_core(struct message *mp)
337f3098750Schristos {
338f3098750Schristos int i;
339f3098750Schristos assert(mp->m_blink == NULL);
340f3098750Schristos
341f3098750Schristos i = 0;
34267e2fbb0Schristos for (mp = first_message(mp); mp; mp = mp->m_flink) {
343f3098750Schristos assert(mp->m_flink == NULL || mp == mp->m_flink->m_blink);
344f3098750Schristos assert(mp->m_blink == NULL || mp == mp->m_blink->m_flink);
345f3098750Schristos
34667e2fbb0Schristos assert(mp->m_size != 0);
34767e2fbb0Schristos
348f3098750Schristos if (S_IS_RESTRICT(state) == 0 || !is_tagged(mp))
349f3098750Schristos mp->m_index = ++i;
350f3098750Schristos
351f3098750Schristos if (mp->m_clink)
352f3098750Schristos (void)reindex_core(mp->m_clink);
353f3098750Schristos }
354f3098750Schristos return i;
355f3098750Schristos }
356f3098750Schristos
357f3098750Schristos
358f3098750Schristos static void
reindex(struct thread_s * tp)359f3098750Schristos reindex(struct thread_s *tp)
360f3098750Schristos {
361f3098750Schristos struct message *mp;
362f3098750Schristos int i;
363f3098750Schristos
36467e2fbb0Schristos assert(tp != NULL);
36567e2fbb0Schristos
36667e2fbb0Schristos if ((mp = tp->t_head) == NULL || mp->m_size == 0)
36767e2fbb0Schristos return;
36867e2fbb0Schristos
36967e2fbb0Schristos assert(mp->m_blink == NULL);
370f3098750Schristos
371f3098750Schristos if (S_IS_EXPOSE(state) == 0) {
372f3098750Schristos /*
373f3098750Schristos * We special case this so that all the hidden
374f3098750Schristos * sub-threads get indexed, not just the current one.
375f3098750Schristos */
376f3098750Schristos i = reindex_core(tp->t_head);
377f3098750Schristos }
378f3098750Schristos else {
379f3098750Schristos i = 0;
380f3098750Schristos for (mp = first_message(tp->t_head); mp; mp = next_message(mp))
381f3098750Schristos mp->m_index = ++i;
382f3098750Schristos }
383f3098750Schristos
384f3098750Schristos assert(i <= message_array.t_msgCount);
385f3098750Schristos
386f3098750Schristos tp->t_msgCount = i;
387f3098750Schristos i = 0;
388f3098750Schristos for (mp = first_message(tp->t_head); mp; mp = next_message(mp))
389f3098750Schristos tp->t_msgtbl[i++] = mp;
390f3098750Schristos }
391f3098750Schristos
392f3098750Schristos static void
redepth_core(struct message * mp,int depth,struct message * parent)393f3098750Schristos redepth_core(struct message *mp, int depth, struct message *parent)
394f3098750Schristos {
395f3098750Schristos assert(mp->m_blink == NULL);
396f3098750Schristos assert((parent == NULL && depth == 0) ||
397f3098750Schristos (parent != NULL && depth != 0 && depth == parent->m_depth + 1));
398f3098750Schristos
399f3098750Schristos for (/*EMPTY*/; mp; mp = mp->m_flink) {
400f3098750Schristos assert(mp->m_plink == parent);
401f3098750Schristos assert(mp->m_flink == NULL || mp == mp->m_flink->m_blink);
402f3098750Schristos assert(mp->m_blink == NULL || mp == mp->m_blink->m_flink);
40367e2fbb0Schristos assert(mp->m_size != 0);
404f3098750Schristos
405f3098750Schristos mp->m_depth = depth;
406f3098750Schristos if (mp->m_clink)
407f3098750Schristos redepth_core(mp->m_clink, depth + 1, mp);
408f3098750Schristos }
409f3098750Schristos }
410f3098750Schristos
411f3098750Schristos static void
redepth(struct thread_s * thread)412f3098750Schristos redepth(struct thread_s *thread)
413f3098750Schristos {
414f3098750Schristos int depth;
415f3098750Schristos struct message *mp;
416f3098750Schristos
417f3098750Schristos assert(thread != NULL);
418f3098750Schristos
41967e2fbb0Schristos if ((mp = thread->t_head) == NULL || mp->m_size == 0)
420f3098750Schristos return;
421f3098750Schristos
422f3098750Schristos depth = mp->m_plink ? mp->m_plink->m_depth + 1 : 0;
423f3098750Schristos
424f3098750Schristos #ifndef NDEBUG /* a sanity check if asserts are active */
425f3098750Schristos {
426f3098750Schristos struct message *tp;
427f3098750Schristos int i;
428f3098750Schristos i = 0;
429f3098750Schristos for (tp = mp->m_plink; tp; tp = tp->m_plink)
430f3098750Schristos i++;
431f3098750Schristos assert(i == depth);
432f3098750Schristos }
433f3098750Schristos #endif
434f3098750Schristos
435f3098750Schristos redepth_core(mp, depth, mp->m_plink);
436f3098750Schristos }
437f3098750Schristos
438f3098750Schristos /************************************************************************
439f3098750Schristos * To be called after reallocating the main message list. It is here
440f3098750Schristos * as it needs access to current_thread.t_head.
441f3098750Schristos */
442f3098750Schristos PUBLIC void
thread_fix_old_links(struct message * nmessage,ptrdiff_t off,int omsgCount)44381a719dfSmrg thread_fix_old_links(struct message *nmessage, ptrdiff_t off, int omsgCount)
444f3098750Schristos {
445f3098750Schristos int i;
446f3098750Schristos
447f3098750Schristos #ifndef NDEBUG
448f3098750Schristos message_array.t_head = nmessage; /* for assert check in thread_fix_new_links */
449f3098750Schristos #endif
450f3098750Schristos
45127b54bd9Schristos # define FIX_LINK(p) do {\
45281a719dfSmrg p = nmessage + off;\
453133e932aSrillig } while (0)
45427b54bd9Schristos
455f3098750Schristos FIX_LINK(current_thread.t_head);
456f3098750Schristos for (i = 0; i < omsgCount; i++) {
457f3098750Schristos FIX_LINK(nmessage[i].m_blink);
458f3098750Schristos FIX_LINK(nmessage[i].m_flink);
459f3098750Schristos FIX_LINK(nmessage[i].m_clink);
460f3098750Schristos FIX_LINK(nmessage[i].m_plink);
461f3098750Schristos }
462f3098750Schristos for (i = 0; i < current_thread.t_msgCount; i++)
463f3098750Schristos FIX_LINK(current_thread.t_msgtbl[i]);
464f3098750Schristos
465f3098750Schristos # undef FIX_LINK
466f3098750Schristos }
467f3098750Schristos
468f3098750Schristos static void
thread_init(struct thread_s * tp,struct message * mp,int msgCount)469f3098750Schristos thread_init(struct thread_s *tp, struct message *mp, int msgCount)
470f3098750Schristos {
471f3098750Schristos int i;
472f3098750Schristos
473f3098750Schristos if (tp->t_msgtbl == NULL || msgCount > tp->t_msgCount) {
474f3098750Schristos if (tp->t_msgtbl)
475f3098750Schristos free(tp->t_msgtbl);
476f3098750Schristos tp->t_msgtbl = ecalloc((size_t)msgCount, sizeof(tp->t_msgtbl[0]));
477f3098750Schristos }
478f3098750Schristos tp->t_head = mp;
479f3098750Schristos tp->t_msgCount = msgCount;
480f3098750Schristos for (i = 0; i < msgCount; i++)
481f3098750Schristos tp->t_msgtbl[i] = &mp[i];
482f3098750Schristos }
483f3098750Schristos
484f3098750Schristos /*
485f3098750Schristos * To be called after reading in the new message structures.
486f3098750Schristos * It is here as it needs access to current_thread.t_head.
487f3098750Schristos */
488f3098750Schristos PUBLIC void
thread_fix_new_links(struct message * message,int omsgCount,int msgCount)489f3098750Schristos thread_fix_new_links(struct message *message, int omsgCount, int msgCount)
490f3098750Schristos {
491f3098750Schristos int i;
492f3098750Schristos struct message *lastmp;
493f3098750Schristos
494f3098750Schristos /* This should only be called at the top level if omsgCount != 0! */
495f3098750Schristos assert(omsgCount == 0 || message->m_plink == NULL);
496f3098750Schristos assert(omsgCount == 0 || message_array.t_msgCount == omsgCount);
497f3098750Schristos assert(message_array.t_head == message);
498f3098750Schristos
499f3098750Schristos message_array.t_head = message;
500f3098750Schristos message_array.t_msgCount = msgCount;
501f3098750Schristos assert(message_array.t_msgtbl == NULL); /* never used */
502f3098750Schristos
503f3098750Schristos lastmp = NULL;
504f3098750Schristos if (omsgCount) {
505f3098750Schristos /*
506f3098750Schristos * Find the end of the toplevel thread.
507f3098750Schristos */
508f3098750Schristos for (i = 0; i < omsgCount; i++) {
509f3098750Schristos if (message_array.t_head[i].m_depth == 0 &&
510f3098750Schristos message_array.t_head[i].m_flink == NULL) {
511f3098750Schristos lastmp = &message_array.t_head[i];
512f3098750Schristos break;
513f3098750Schristos }
514f3098750Schristos }
515f3098750Schristos #ifndef NDEBUG
516f3098750Schristos /*
517f3098750Schristos * lastmp better be unique!!!
518f3098750Schristos */
519f3098750Schristos for (i++; i < omsgCount; i++)
520f3098750Schristos assert(message_array.t_head[i].m_depth != 0 ||
521f3098750Schristos message_array.t_head[i].m_flink != NULL);
522f3098750Schristos assert(lastmp != NULL);
523f3098750Schristos #endif /* NDEBUG */
524f3098750Schristos }
525f3098750Schristos /*
526f3098750Schristos * Link and index the new messages linearly at depth 0.
527f3098750Schristos */
528f3098750Schristos for (i = omsgCount; i < msgCount; i++) {
529f3098750Schristos message[i].m_index = i + 1;
530f3098750Schristos message[i].m_depth = 0;
531f3098750Schristos message[i].m_blink = lastmp;
532f3098750Schristos message[i].m_flink = NULL;
533f3098750Schristos message[i].m_clink = NULL;
534f3098750Schristos message[i].m_plink = NULL;
535f3098750Schristos if (lastmp)
536f3098750Schristos lastmp->m_flink = &message[i];
537f3098750Schristos lastmp = &message[i];
538f3098750Schristos }
539f3098750Schristos
540f3098750Schristos /*
541f3098750Schristos * Make sure the current thread is setup correctly.
542f3098750Schristos */
543f3098750Schristos if (omsgCount == 0) {
544f3098750Schristos thread_init(¤t_thread, message, msgCount);
545f3098750Schristos }
546f3098750Schristos else {
547f3098750Schristos /*
548f3098750Schristos * Make sure current_thread.t_msgtbl is always large
549f3098750Schristos * enough.
550f3098750Schristos */
551f3098750Schristos current_thread.t_msgtbl =
552f3098750Schristos erealloc(current_thread.t_msgtbl,
553f3098750Schristos msgCount * sizeof(*current_thread.t_msgtbl));
554f3098750Schristos
555f3098750Schristos assert(current_thread.t_head != NULL);
556f3098750Schristos if (current_thread.t_head->m_depth == 0)
557f3098750Schristos reindex(¤t_thread);
558f3098750Schristos }
559f3098750Schristos }
560f3098750Schristos
561f3098750Schristos /************************************************************************/
562f3098750Schristos /*
563f3098750Schristos * All state changes should go through here!!!
564f3098750Schristos */
56517a5e239Schristos
56617a5e239Schristos /*
56717a5e239Schristos * NOTE: It is the caller's responsibility to ensure that the "dot"
56817a5e239Schristos * will be valid after a state change. For example, when changing
56917a5e239Schristos * from exposed to hidden threads, it is necessary to move the dot to
57017a5e239Schristos * the head of the thread or it will not be seen. Use thread_top()
57117a5e239Schristos * for this. Likewise, use first_visible_message() to locate the
57217a5e239Schristos * first visible message after a state change.
57317a5e239Schristos */
57417a5e239Schristos
575f3098750Schristos static state_t
set_state(int and_bits,int xor_bits)576f3098750Schristos set_state(int and_bits, int xor_bits)
577f3098750Schristos {
578f3098750Schristos state_t old_state;
579f3098750Schristos old_state = state;
580f3098750Schristos state &= and_bits;
581f3098750Schristos state ^= xor_bits;
582f3098750Schristos reindex(¤t_thread);
583f3098750Schristos redepth(¤t_thread);
584f3098750Schristos return old_state;
585f3098750Schristos }
586f3098750Schristos
58717a5e239Schristos static struct message *
first_visible_message(struct message * mp)58817a5e239Schristos first_visible_message(struct message *mp)
58917a5e239Schristos {
59017a5e239Schristos struct message *oldmp;
59117a5e239Schristos
59217a5e239Schristos if (mp == NULL)
59317a5e239Schristos mp = current_thread.t_head;
59417a5e239Schristos
5959aae377cSchristos if (mp == NULL)
5969aae377cSchristos return NULL;
5979aae377cSchristos
59817a5e239Schristos oldmp = mp;
59917a5e239Schristos if ((S_IS_RESTRICT(state) && is_tagged(mp)) || mp->m_flag & MDELETED)
60017a5e239Schristos mp = next_message(mp);
60117a5e239Schristos
60217a5e239Schristos if (mp == NULL) {
60317a5e239Schristos mp = oldmp;
60417a5e239Schristos if ((S_IS_RESTRICT(state) && is_tagged(mp)) || mp->m_flag & MDELETED)
60517a5e239Schristos mp = prev_message(mp);
60617a5e239Schristos }
60717a5e239Schristos if (mp == NULL)
60817a5e239Schristos mp = current_thread.t_head;
60917a5e239Schristos
61017a5e239Schristos return mp;
61117a5e239Schristos }
61217a5e239Schristos
613f3098750Schristos static void
restore_state(state_t new_state)614f3098750Schristos restore_state(state_t new_state)
615f3098750Schristos {
616f3098750Schristos state = new_state;
617f3098750Schristos reindex(¤t_thread);
618f3098750Schristos redepth(¤t_thread);
61917a5e239Schristos dot = first_visible_message(dot);
62017a5e239Schristos }
62117a5e239Schristos
62217a5e239Schristos static struct message *
thread_top(struct message * mp)62317a5e239Schristos thread_top(struct message *mp)
62417a5e239Schristos {
62517a5e239Schristos while (mp && mp->m_plink) {
62617a5e239Schristos if (mp->m_plink->m_clink == current_thread.t_head)
62717a5e239Schristos break;
62817a5e239Schristos mp = mp->m_plink;
62917a5e239Schristos }
63017a5e239Schristos return mp;
631f3098750Schristos }
632f3098750Schristos
633f3098750Schristos /************************************************************************/
634f3098750Schristos /*
635f3098750Schristos * Possibly show the message list.
636f3098750Schristos */
637f3098750Schristos static void
thread_announce(void * v)638f3098750Schristos thread_announce(void *v)
639f3098750Schristos {
640f3098750Schristos int vec[2];
641f3098750Schristos
642f3098750Schristos if (v == NULL) /* check this here to avoid it before each call */
643f3098750Schristos return;
644f3098750Schristos
645f3098750Schristos if (dot == NULL) {
646f3098750Schristos (void)printf("No applicable messages\n");
647f3098750Schristos return;
648f3098750Schristos }
649f3098750Schristos vec[0] = get_msgnum(dot);
650f3098750Schristos vec[1] = 0;
651f3098750Schristos if (get_msgCount() > 0 && value(ENAME_NOHEADER) == NULL)
652f3098750Schristos (void)headers(vec);
653f3098750Schristos sawcom = 0; /* so next will print the first message */
654f3098750Schristos }
655f3098750Schristos
656f3098750Schristos /************************************************************************/
657f3098750Schristos
658f3098750Schristos /*
659f3098750Schristos * Flatten out the portion of the thread starting with the given
660f3098750Schristos * message.
661f3098750Schristos */
662f3098750Schristos static void
flattencmd_core(struct message * mp)663f3098750Schristos flattencmd_core(struct message *mp)
664f3098750Schristos {
665f3098750Schristos struct message **marray;
666f3098750Schristos size_t mcount;
667f3098750Schristos struct message *tp;
668f3098750Schristos struct message *nextmp;
669c172e3b9Slukem size_t i;
670f3098750Schristos
671f3098750Schristos if (mp == NULL)
672f3098750Schristos return;
673f3098750Schristos
674f3098750Schristos mcount = 1;
675f3098750Schristos for (tp = next_message(mp); tp && tp->m_depth > mp->m_depth; tp = next_message(tp))
676f3098750Schristos mcount++;
677f3098750Schristos
678f3098750Schristos if (tp && tp->m_depth < mp->m_depth)
679f3098750Schristos nextmp = NULL;
680f3098750Schristos else
681f3098750Schristos nextmp = tp;
682f3098750Schristos
683f3098750Schristos if (mcount == 1)
684f3098750Schristos return;
685f3098750Schristos
686f3098750Schristos marray = csalloc(mcount, sizeof(*marray));
687f3098750Schristos tp = mp;
688f3098750Schristos for (i = 0; i < mcount; i++) {
689f3098750Schristos marray[i] = tp;
690f3098750Schristos tp = next_message(tp);
691f3098750Schristos }
692f3098750Schristos mp->m_clink = NULL;
693f3098750Schristos for (i = 1; i < mcount; i++) {
694f3098750Schristos marray[i]->m_depth = mp->m_depth;
695f3098750Schristos marray[i]->m_plink = mp->m_plink;
696f3098750Schristos marray[i]->m_clink = NULL;
697f3098750Schristos marray[i]->m_blink = marray[i - 1];
698f3098750Schristos marray[i - 1]->m_flink = marray[i];
699f3098750Schristos }
700f3098750Schristos marray[i - 1]->m_flink = nextmp;
701f3098750Schristos if (nextmp)
702f3098750Schristos nextmp->m_blink = marray[i - 1];
703f3098750Schristos }
704f3098750Schristos
705f3098750Schristos /*
706f3098750Schristos * Flatten out all thread parts given in the message list, or the
707f3098750Schristos * current thread, if none given.
708f3098750Schristos */
709f3098750Schristos PUBLIC int
flattencmd(void * v)710f3098750Schristos flattencmd(void *v)
711f3098750Schristos {
712f3098750Schristos int *msgvec;
713f3098750Schristos int *ip;
714f3098750Schristos
715f3098750Schristos msgvec = v;
716f3098750Schristos
717f3098750Schristos if (*msgvec) { /* a message was supplied */
718f3098750Schristos for (ip = msgvec; *ip; ip++) {
719f3098750Schristos struct message *mp;
720f3098750Schristos mp = get_message(*ip);
721f3098750Schristos if (mp != NULL)
722f3098750Schristos flattencmd_core(mp);
723f3098750Schristos }
724f3098750Schristos }
725f3098750Schristos else { /* no message given - flatten current thread */
726f3098750Schristos struct message *mp;
727f3098750Schristos for (mp = first_message(current_thread.t_head);
728f3098750Schristos mp; mp = next_message(mp))
729f3098750Schristos flattencmd_core(mp);
730f3098750Schristos }
731f3098750Schristos redepth(¤t_thread);
732f3098750Schristos thread_announce(v);
733f3098750Schristos return 0;
734f3098750Schristos }
735f3098750Schristos
736f3098750Schristos
737f3098750Schristos /************************************************************************/
738f3098750Schristos /*
739f3098750Schristos * The basic sort structure. For each message the index and key
740f3098750Schristos * fields are set. The key field is used for the basic sort and the
741f3098750Schristos * index is used to ensure that the order from the current thread is
742f3098750Schristos * maintained when the key compare is equal.
743f3098750Schristos */
744f3098750Schristos struct key_sort_s {
745f3098750Schristos struct message *mp; /* the message the following refer to */
746f3098750Schristos union {
747f3098750Schristos char *str; /* string sort key (typically a field or address) */
748f3098750Schristos long lines; /* a long sort key (typically a message line count) */
749f3098750Schristos off_t size; /* a size sort key (typically the message size) */
750f3098750Schristos time_t time; /* a time sort key (typically from date or headline) */
751f3098750Schristos } key;
752f3098750Schristos int index; /* index from of the current thread before sorting */
753f3098750Schristos /* XXX - do we really want index? It is always set to mp->m_index */
754f3098750Schristos };
755f3098750Schristos
756f3098750Schristos /*
757f3098750Schristos * This is the compare function obtained from the key_tbl[]. It is
758f3098750Schristos * used by thread_array() to identify the end of the thread and by
759f3098750Schristos * qsort_cmpfn() to do the basic sort.
760f3098750Schristos */
761f3098750Schristos static struct {
762f3098750Schristos int inv;
763f3098750Schristos int (*fn)(const void *, const void *);
764f3098750Schristos } cmp;
765f3098750Schristos
766f3098750Schristos /*
767f3098750Schristos * The routine passed to qsort. Note that cmpfn must be set first!
768f3098750Schristos */
769f3098750Schristos static int
qsort_cmpfn(const void * left,const void * right)770f3098750Schristos qsort_cmpfn(const void *left, const void *right)
771f3098750Schristos {
772f3098750Schristos int delta;
773f3098750Schristos const struct key_sort_s *lp = left;
774f3098750Schristos const struct key_sort_s *rp = right;
775f3098750Schristos
776f3098750Schristos delta = cmp.fn(left, right);
777f3098750Schristos return delta ? cmp.inv ? - delta : delta : lp->index - rp->index;
778f3098750Schristos }
779f3098750Schristos
780f3098750Schristos static void
link_array(struct key_sort_s * marray,size_t mcount)781f3098750Schristos link_array(struct key_sort_s *marray, size_t mcount)
782f3098750Schristos {
783c172e3b9Slukem size_t i;
784f3098750Schristos struct message *lastmp;
785f3098750Schristos lastmp = NULL;
786f3098750Schristos for (i = 0; i < mcount; i++) {
787ca13337dSchristos marray[i].mp->m_index = (int)i + 1;
788f3098750Schristos marray[i].mp->m_blink = lastmp;
789f3098750Schristos marray[i].mp->m_flink = NULL;
790f3098750Schristos if (lastmp)
791f3098750Schristos lastmp->m_flink = marray[i].mp;
792f3098750Schristos lastmp = marray[i].mp;
793f3098750Schristos }
794f3098750Schristos if (current_thread.t_head->m_plink)
795f3098750Schristos current_thread.t_head->m_plink->m_clink = marray[0].mp;
796f3098750Schristos
797f3098750Schristos current_thread.t_head = marray[0].mp;
798f3098750Schristos }
799f3098750Schristos
800f3098750Schristos static void
cut_array(struct key_sort_s * marray,size_t beg,size_t end)801c172e3b9Slukem cut_array(struct key_sort_s *marray, size_t beg, size_t end)
802f3098750Schristos {
803c172e3b9Slukem size_t i;
804f3098750Schristos
805f3098750Schristos if (beg + 1 < end) {
806f3098750Schristos assert(marray[beg].mp->m_clink == NULL);
807f3098750Schristos
808f3098750Schristos marray[beg].mp->m_clink = marray[beg + 1].mp;
809f3098750Schristos marray[beg + 1].mp->m_blink = NULL;
810f3098750Schristos
811f3098750Schristos marray[beg].mp->m_flink = marray[end].mp;
812f3098750Schristos if (marray[end].mp)
813f3098750Schristos marray[end].mp->m_blink = marray[beg].mp;
814f3098750Schristos
815f3098750Schristos marray[end - 1].mp->m_flink = NULL;
816f3098750Schristos
817f3098750Schristos for (i = beg + 1; i < end; i++)
818f3098750Schristos marray[i].mp->m_plink = marray[beg].mp;
819f3098750Schristos }
820f3098750Schristos }
821f3098750Schristos
822f3098750Schristos static void
thread_array(struct key_sort_s * marray,size_t mcount,int cutit)823f3098750Schristos thread_array(struct key_sort_s *marray, size_t mcount, int cutit)
824f3098750Schristos {
825f3098750Schristos struct message *parent;
826f3098750Schristos
827f3790781Schristos if (mcount == 0)
828f3790781Schristos return;
829f3790781Schristos
830f3098750Schristos parent = marray[0].mp->m_plink;
831f3098750Schristos qsort(marray, mcount, sizeof(*marray), qsort_cmpfn);
832f3098750Schristos link_array(marray, mcount);
833f3098750Schristos
834f3098750Schristos if (cutit) {
835c172e3b9Slukem size_t i, j;
836f3098750Schristos /*
837f3098750Schristos * Flatten out the array.
838f3098750Schristos */
839f3098750Schristos for (i = 0; i < mcount; i++) {
840f3098750Schristos marray[i].mp->m_plink = parent;
841f3098750Schristos marray[i].mp->m_clink = NULL;
842f3098750Schristos }
843f3098750Schristos
844f3098750Schristos /*
845f3098750Schristos * Now chop it up. There is really only one level here.
846f3098750Schristos */
847f3098750Schristos i = 0;
848f3098750Schristos for (j = 1; j < mcount; j++) {
849f3098750Schristos if (cmp.fn(&marray[i], &marray[j]) != 0) {
850f3098750Schristos cut_array(marray, i, j);
851f3098750Schristos i = j;
852f3098750Schristos }
853f3098750Schristos }
854f3098750Schristos cut_array(marray, i, j);
855f3098750Schristos }
856f3098750Schristos }
857f3098750Schristos
858f3098750Schristos /************************************************************************/
859f3098750Schristos /*
860f3098750Schristos * thread_on_reference() is the core reference threading routine. It
861f3098750Schristos * is not a command itself by called by threadcmd().
862f3098750Schristos */
863f3098750Schristos
864f3098750Schristos static void
adopt_child(struct message * parent,struct message * child)865f3098750Schristos adopt_child(struct message *parent, struct message *child)
866f3098750Schristos {
867f3098750Schristos /*
868f3098750Schristos * Unhook the child from its current location.
869f3098750Schristos */
870f3098750Schristos if (child->m_blink != NULL) {
871f3098750Schristos child->m_blink->m_flink = child->m_flink;
872f3098750Schristos }
873f3098750Schristos if (child->m_flink != NULL) {
874f3098750Schristos child->m_flink->m_blink = child->m_blink;
875f3098750Schristos }
876f3098750Schristos
877f3098750Schristos /*
878f3098750Schristos * Link the child to the parent.
879f3098750Schristos */
880f3098750Schristos if (parent->m_clink == NULL) { /* parent has no child */
881f3098750Schristos parent->m_clink = child;
882f3098750Schristos child->m_blink = NULL;
883f3098750Schristos }
884f3098750Schristos else { /* add message to end of parent's child's flist */
885f3098750Schristos struct message *t;
886f3098750Schristos for (t = parent->m_clink; t && t->m_flink; t = t->m_flink)
887f3098750Schristos continue;
888f3098750Schristos t->m_flink = child;
889f3098750Schristos child->m_blink = t;
890f3098750Schristos }
891f3098750Schristos child->m_flink = NULL;
892f3098750Schristos child->m_plink = parent;
893f3098750Schristos }
894f3098750Schristos
895f3098750Schristos /*
896f3098750Schristos * Get the parent ID for a message (if there is one).
897f3098750Schristos *
898f3098750Schristos * See RFC 2822, sec 3.6.4.
899f3098750Schristos *
900f3098750Schristos * Many mailers seem to screw up the In-Reply-To: and/or
901f3098750Schristos * References: fields, generally by omitting one or both.
902f3098750Schristos *
903f3098750Schristos * We give preference to the "References" field. If it does
904f3098750Schristos * not exist, try the "In-Reply-To" field. If neither exist,
905f3098750Schristos * then the message is either not a reply or someone isn't
906f3098750Schristos * adding the necessary fields, so skip it.
907f3098750Schristos */
908f3098750Schristos static char *
get_parent_id(struct message * mp)909f3098750Schristos get_parent_id(struct message *mp)
910f3098750Schristos {
911f3098750Schristos struct name *refs;
912f3098750Schristos
913f3098750Schristos if ((refs = extract(hfield("references", mp), 0)) != NULL) {
914f3098750Schristos char *id;
915f3098750Schristos while (refs->n_flink)
916f3098750Schristos refs = refs->n_flink;
917f3098750Schristos
918f3098750Schristos id = skin(refs->n_name);
919f3098750Schristos if (*id != '\0')
920f3098750Schristos return id;
921f3098750Schristos }
922f3098750Schristos
923f3098750Schristos return skin(hfield("in-reply-to", mp));
924f3098750Schristos }
925f3098750Schristos
926f3098750Schristos /*
927f3098750Schristos * Thread on the "In-Reply-To" and "Reference" fields. This is the
928f3098750Schristos * normal way to thread.
929f3098750Schristos */
930f3098750Schristos static void
thread_on_reference(struct message * mp)931f3098750Schristos thread_on_reference(struct message *mp)
932f3098750Schristos {
93317a5e239Schristos struct {
93417a5e239Schristos struct message *mp;
93517a5e239Schristos char *message_id;
93617a5e239Schristos char *parent_id;
93717a5e239Schristos } *marray;
938f3098750Schristos struct message *parent;
939f3098750Schristos state_t oldstate;
940c172e3b9Slukem size_t mcount, i;
941f3098750Schristos
942f3098750Schristos assert(mp == current_thread.t_head);
943f3098750Schristos
944f3098750Schristos oldstate = set_state(~(S_RESTRICT | S_EXPOSE), S_EXPOSE); /* restrict off, expose on */
945f3098750Schristos
946f3098750Schristos mcount = get_msgCount();
947f3098750Schristos
948f3098750Schristos if (mcount < 2) /* it's hard to thread so few messages! */
949f3098750Schristos goto done;
950f3098750Schristos
951f3098750Schristos marray = csalloc(mcount + 1, sizeof(*marray));
952f3098750Schristos
953f3098750Schristos /*
954f3098750Schristos * Load up the array (skin where necessary).
955f3098750Schristos *
956f3098750Schristos * With a 40K message file, most of the time is spent here,
957f3098750Schristos * not in the search loop below.
958f3098750Schristos */
959f3098750Schristos for (i = 0; i < mcount; i++) {
960f3098750Schristos marray[i].mp = mp;
961f3098750Schristos marray[i].message_id = skin(hfield("message-id", mp));
962f3098750Schristos marray[i].parent_id = get_parent_id(mp);
963f3098750Schristos mp = next_message(mp);
964f3098750Schristos }
965f3098750Schristos
966f3098750Schristos /*
967f3098750Schristos * Save the old parent.
968f3098750Schristos */
969f3098750Schristos parent = marray[0].mp->m_plink;
970f3098750Schristos
971f3098750Schristos /*
972f3098750Schristos * flatten the array.
973f3098750Schristos */
974f3098750Schristos marray[0].mp->m_clink = NULL;
975f3098750Schristos for (i = 1; i < mcount; i++) {
976f3098750Schristos marray[i].mp->m_depth = marray[0].mp->m_depth;
977f3098750Schristos marray[i].mp->m_plink = marray[0].mp->m_plink;
978f3098750Schristos marray[i].mp->m_clink = NULL;
979f3098750Schristos marray[i].mp->m_blink = marray[i - 1].mp;
980f3098750Schristos marray[i - 1].mp->m_flink = marray[i].mp;
981f3098750Schristos }
982f3098750Schristos marray[i - 1].mp->m_flink = NULL;
983f3098750Schristos
984f3098750Schristos /*
985f3098750Schristos * Walk the array hooking up the replies with their parents.
986f3098750Schristos */
987f3098750Schristos for (i = 0; i < mcount; i++) {
988f3098750Schristos struct message *child;
989f3098750Schristos char *parent_id;
990c172e3b9Slukem size_t j;
991f3098750Schristos
992f3098750Schristos if ((parent_id = marray[i].parent_id) == NULL)
993f3098750Schristos continue;
994f3098750Schristos
995f3098750Schristos child = marray[i].mp;
996f3098750Schristos
997f3098750Schristos /*
998f3098750Schristos * Look for the parent message and link this one in
999f3098750Schristos * appropriately.
1000f3098750Schristos *
1001f3098750Schristos * XXX - This will not scale nicely, though it does
1002f3098750Schristos * not appear to be the dominant loop even with 40K
1003f3098750Schristos * messages. If this becomes a problem, implement a
1004f3098750Schristos * binary search.
1005f3098750Schristos */
1006f3098750Schristos for (j = 0; j < mcount; j++) {
1007f3098750Schristos /* message_id will be NULL on mbox files */
1008de5e815cSchristos if (marray[j].message_id == NULL)
1009f3098750Schristos continue;
1010f3098750Schristos
1011f3098750Schristos if (equal(marray[j].message_id, parent_id)) {
1012f3098750Schristos /*
1013f3098750Schristos * The child is at the top level. If
1014f3098750Schristos * it is being adopted and it was top
1015f3098750Schristos * left (current_thread.t_head), then
1016f3098750Schristos * its right sibling is the new top
1017f3098750Schristos * left (current_thread.t_head).
1018f3098750Schristos */
1019f3098750Schristos if (current_thread.t_head == child) {
1020f3098750Schristos current_thread.t_head = child->m_flink;
1021f3098750Schristos assert(current_thread.t_head != NULL);
1022f3098750Schristos }
1023f3098750Schristos adopt_child(marray[j].mp, child);
1024f3098750Schristos break;
1025f3098750Schristos }
1026f3098750Schristos }
1027f3098750Schristos }
1028f3098750Schristos
1029f3098750Schristos if (parent)
1030f3098750Schristos parent->m_clink = current_thread.t_head;
1031f3098750Schristos /*
1032f3098750Schristos * If the old state is not exposed, reset the dot to the head
1033f3098750Schristos * of the thread it lived in, so it will be in a valid spot
1034f3098750Schristos * when things are re-hidden.
1035f3098750Schristos */
1036f3098750Schristos if (!S_IS_EXPOSE(oldstate))
1037f3098750Schristos dot = thread_top(dot);
1038f3098750Schristos done:
1039f3098750Schristos restore_state(oldstate);
1040f3098750Schristos }
1041f3098750Schristos
1042f3098750Schristos /************************************************************************/
1043f3098750Schristos /*
1044f3098750Schristos * Tagging commands.
1045f3098750Schristos */
1046f3098750Schristos static int
tag1(int * msgvec,int and_bits,int xor_bits)1047f3098750Schristos tag1(int *msgvec, int and_bits, int xor_bits)
1048f3098750Schristos {
1049f3098750Schristos int *ip;
1050f3098750Schristos
1051f3098750Schristos for (ip = msgvec; *ip != 0; ip++)
1052f3098750Schristos (void)set_m_flag(*ip, and_bits, xor_bits);
1053f3098750Schristos
1054f3098750Schristos reindex(¤t_thread);
1055f3098750Schristos /* thread_announce(v); */
1056f3098750Schristos return 0;
1057f3098750Schristos }
1058f3098750Schristos
1059f3098750Schristos /*
1060f3098750Schristos * Tag the current message dot or a message list.
1061f3098750Schristos */
1062f3098750Schristos PUBLIC int
tagcmd(void * v)1063f3098750Schristos tagcmd(void *v)
1064f3098750Schristos {
1065f3098750Schristos return tag1(v, ~MTAGGED, MTAGGED);
1066f3098750Schristos }
1067f3098750Schristos
1068f3098750Schristos /*
1069f3098750Schristos * Untag the current message dot or a message list.
1070f3098750Schristos */
1071f3098750Schristos PUBLIC int
untagcmd(void * v)1072f3098750Schristos untagcmd(void *v)
1073f3098750Schristos {
1074f3098750Schristos return tag1(v, ~MTAGGED, 0);
1075f3098750Schristos }
1076f3098750Schristos
1077f3098750Schristos /*
1078f3098750Schristos * Invert all tags in the message list.
1079f3098750Schristos */
1080f3098750Schristos PUBLIC int
invtagscmd(void * v)1081f3098750Schristos invtagscmd(void *v)
1082f3098750Schristos {
1083f3098750Schristos return tag1(v, ~0, MTAGGED);
1084f3098750Schristos }
1085f3098750Schristos
1086f3098750Schristos /*
1087f3098750Schristos * Tag all messages below the current dot or below a specified
1088f3098750Schristos * message.
1089f3098750Schristos */
1090f3098750Schristos PUBLIC int
tagbelowcmd(void * v)1091f3098750Schristos tagbelowcmd(void *v)
1092f3098750Schristos {
1093f3098750Schristos int *msgvec;
1094f3098750Schristos struct message *mp;
1095f3098750Schristos state_t oldstate;
1096f3098750Schristos int depth;
1097f3098750Schristos
1098f3098750Schristos msgvec = v;
1099f3098750Schristos
1100f3098750Schristos oldstate = set_state(~(S_RESTRICT | S_EXPOSE), S_EXPOSE); /* restrict off, expose on */
1101f3098750Schristos mp = get_message(*msgvec);
1102f3098750Schristos if (mp) {
1103f3098750Schristos depth = mp->m_depth;
1104f3098750Schristos for (mp = first_message(current_thread.t_head); mp; mp = next_message(mp))
1105f3098750Schristos if (mp->m_depth > depth) {
1106f3098750Schristos mp->m_flag |= MTAGGED;
1107f3098750Schristos touch(mp);
1108f3098750Schristos }
1109f3098750Schristos }
111017a5e239Schristos /* dot is OK */
1111f3098750Schristos restore_state(oldstate);
1112f3098750Schristos /* thread_announce(v); */
1113f3098750Schristos return 0;
1114f3098750Schristos }
1115f3098750Schristos
1116f3098750Schristos /*
1117f3098750Schristos * Do not display the tagged messages.
1118f3098750Schristos */
1119f3098750Schristos PUBLIC int
hidetagscmd(void * v)1120f3098750Schristos hidetagscmd(void *v)
1121f3098750Schristos {
112217a5e239Schristos (void)set_state(~S_RESTRICT, S_RESTRICT); /* restrict on */
112317a5e239Schristos dot = first_visible_message(dot);
1124f3098750Schristos thread_announce(v);
1125f3098750Schristos return 0;
1126f3098750Schristos }
1127f3098750Schristos
1128f3098750Schristos /*
1129f3098750Schristos * Display the tagged messages.
1130f3098750Schristos */
1131f3098750Schristos PUBLIC int
showtagscmd(void * v)1132f3098750Schristos showtagscmd(void *v)
1133f3098750Schristos {
113417a5e239Schristos (void)set_state(~S_RESTRICT, 0); /* restrict off */
113517a5e239Schristos dot = first_visible_message(dot);
1136f3098750Schristos thread_announce(v);
1137f3098750Schristos return 0;
1138f3098750Schristos }
1139f3098750Schristos
1140f3098750Schristos /************************************************************************/
1141f3098750Schristos /*
1142f3098750Schristos * Basic threading commands.
1143f3098750Schristos */
1144f3098750Schristos /*
1145f3098750Schristos * Show the threads.
1146f3098750Schristos */
1147f3098750Schristos PUBLIC int
exposecmd(void * v)1148f3098750Schristos exposecmd(void *v)
1149f3098750Schristos {
1150f3098750Schristos (void)set_state(~S_EXPOSE, S_EXPOSE); /* expose on */
115117a5e239Schristos dot = first_visible_message(dot);
1152f3098750Schristos thread_announce(v);
1153f3098750Schristos return 0;
1154f3098750Schristos }
1155f3098750Schristos
1156f3098750Schristos /*
1157f3098750Schristos * Hide the threads.
1158f3098750Schristos */
1159f3098750Schristos PUBLIC int
hidecmd(void * v)1160f3098750Schristos hidecmd(void *v)
1161f3098750Schristos {
1162f3098750Schristos dot = thread_top(dot);
1163f3098750Schristos (void)set_state(~S_EXPOSE, 0); /* expose off */
116417a5e239Schristos dot = first_visible_message(dot);
1165f3098750Schristos thread_announce(v);
1166f3098750Schristos return 0;
1167f3098750Schristos }
1168f3098750Schristos
1169f3098750Schristos /*
1170f3098750Schristos * Up one level in the thread tree. Go up multiple levels if given an
1171f3098750Schristos * argument.
1172f3098750Schristos */
1173f3098750Schristos PUBLIC int
upcmd(void * v)1174f3098750Schristos upcmd(void *v)
1175f3098750Schristos {
1176f3098750Schristos char *str;
1177f3098750Schristos int upcnt;
1178f3098750Schristos int upone;
1179f3098750Schristos
1180f3098750Schristos str = v;
1181d727506fSchristos str = skip_WSP(str);
1182f3098750Schristos if (*str == '\0')
1183f3098750Schristos upcnt = 1;
1184f3098750Schristos else
1185f3098750Schristos upcnt = atoi(str);
1186f3098750Schristos
1187f3098750Schristos if (upcnt < 1) {
1188f3098750Schristos (void)printf("Sorry, argument must be > 0.\n");
1189f3098750Schristos return 0;
1190f3098750Schristos }
1191f3098750Schristos if (dot == NULL) {
1192f3098750Schristos (void)printf("No applicable messages\n");
1193f3098750Schristos return 0;
1194f3098750Schristos }
1195f3098750Schristos if (dot->m_plink == NULL) {
1196f3098750Schristos (void)printf("top thread\n");
1197f3098750Schristos return 0;
1198f3098750Schristos }
1199f3098750Schristos upone = 0;
1200f3098750Schristos while (upcnt-- > 0) {
1201f3098750Schristos struct message *parent;
1202f3098750Schristos parent = current_thread.t_head->m_plink;
1203f3098750Schristos if (parent == NULL) {
1204f3098750Schristos (void)printf("top thread\n");
1205f3098750Schristos break;
1206f3098750Schristos }
1207f3098750Schristos else {
1208f3098750Schristos struct message *mp;
1209f3098750Schristos assert(current_thread.t_head->m_depth > 0);
1210f3098750Schristos for (mp = parent; mp && mp->m_blink; mp = mp->m_blink)
1211f3098750Schristos continue;
1212f3098750Schristos current_thread.t_head = mp;
1213f3098750Schristos dot = parent;
1214f3098750Schristos upone = 1;
1215f3098750Schristos }
1216f3098750Schristos }
1217f3098750Schristos if (upone) {
1218f3098750Schristos reindex(¤t_thread);
1219f3098750Schristos thread_announce(v);
1220f3098750Schristos }
1221f3098750Schristos return 0;
1222f3098750Schristos }
1223f3098750Schristos
1224f3098750Schristos /*
1225f3098750Schristos * Go down one level in the thread tree from the current dot or a
1226f3098750Schristos * given message number if given.
1227f3098750Schristos */
1228f3098750Schristos PUBLIC int
downcmd(void * v)1229f3098750Schristos downcmd(void *v)
1230f3098750Schristos {
1231f3098750Schristos struct message *child;
1232f3098750Schristos struct message *mp;
1233f3098750Schristos int *msgvec = v;
1234f3098750Schristos
1235f3098750Schristos if ((mp = get_message(*msgvec)) == NULL ||
1236f3098750Schristos (child = mp->m_clink) == NULL)
1237f3098750Schristos (void)printf("no sub-thread\n");
1238f3098750Schristos else {
1239f3098750Schristos current_thread.t_head = child;
1240f3098750Schristos dot = child;
1241f3098750Schristos reindex(¤t_thread);
1242f3098750Schristos thread_announce(v);
1243f3098750Schristos }
1244f3098750Schristos return 0;
1245f3098750Schristos }
1246f3098750Schristos
1247f3098750Schristos /*
1248f3098750Schristos * Set the current thread level to the current dot or to the message
1249f3098750Schristos * if given.
1250f3098750Schristos */
1251f3098750Schristos PUBLIC int
tsetcmd(void * v)1252f3098750Schristos tsetcmd(void *v)
1253f3098750Schristos {
1254f3098750Schristos struct message *mp;
1255f3098750Schristos int *msgvec = v;
1256f3098750Schristos
1257f3098750Schristos if ((mp = get_message(*msgvec)) == NULL)
1258f3098750Schristos (void)printf("invalid message\n");
1259f3098750Schristos else {
1260f3098750Schristos for (/*EMPTY*/; mp->m_blink; mp = mp->m_blink)
1261f3098750Schristos continue;
1262f3098750Schristos current_thread.t_head = mp;
1263f3098750Schristos reindex(¤t_thread);
1264f3098750Schristos thread_announce(v);
1265f3098750Schristos }
1266f3098750Schristos return 0;
1267f3098750Schristos }
1268f3098750Schristos
1269f3098750Schristos /*
1270f3098750Schristos * Reverse the current thread order. If threaded, it only operates on
1271f3098750Schristos * the heads.
1272f3098750Schristos */
1273f3098750Schristos static void
reversecmd_core(struct thread_s * tp)1274f3098750Schristos reversecmd_core(struct thread_s *tp)
1275f3098750Schristos {
1276f3098750Schristos struct message *thread_start;
1277f3098750Schristos struct message *mp;
1278f3098750Schristos struct message *lastmp;
1279f3098750Schristos struct message *old_flink;
1280f3098750Schristos
1281f3098750Schristos thread_start = tp->t_head;
1282f3098750Schristos
1283f3098750Schristos assert(thread_start->m_blink == NULL);
1284f3098750Schristos
1285f3098750Schristos lastmp = NULL;
1286f3098750Schristos for (mp = thread_start; mp; mp = old_flink) {
1287f3098750Schristos old_flink = mp->m_flink;
1288f3098750Schristos mp->m_flink = mp->m_blink;
1289f3098750Schristos mp->m_blink = old_flink;
1290f3098750Schristos lastmp = mp;
1291f3098750Schristos }
1292f3098750Schristos if (thread_start->m_plink)
1293f3098750Schristos thread_start->m_plink->m_clink = lastmp;
1294f3098750Schristos
1295f3098750Schristos current_thread.t_head = lastmp;
1296f3098750Schristos reindex(tp);
1297f3098750Schristos }
1298f3098750Schristos
1299f3098750Schristos PUBLIC int
reversecmd(void * v)1300f3098750Schristos reversecmd(void *v)
1301f3098750Schristos {
1302f3098750Schristos reversecmd_core(¤t_thread);
1303f3098750Schristos thread_announce(v);
1304f3098750Schristos return 0;
1305f3098750Schristos }
1306f3098750Schristos
1307f3098750Schristos
1308f3098750Schristos /*
1309f3098750Schristos * Get threading and sorting modifiers.
1310f3098750Schristos */
1311f3098750Schristos #define MF_IGNCASE 1 /* ignore case when sorting */
1312f3098750Schristos #define MF_REVERSE 2 /* reverse sort direction */
1313f3098750Schristos #define MF_SKIN 4 /* "skin" the field to remove comments */
1314f3098750Schristos static int
get_modifiers(char ** str)1315f3098750Schristos get_modifiers(char **str)
1316f3098750Schristos {
1317f3098750Schristos int modflags;
1318f3098750Schristos char *p;
1319f3098750Schristos
1320f3098750Schristos modflags = 0;
1321f3098750Schristos for (p = *str; p && *p; p++) {
1322f3098750Schristos switch (*p) {
1323f3098750Schristos case '!':
1324f3098750Schristos modflags |= MF_REVERSE;
1325f3098750Schristos break;
1326f3098750Schristos case '^':
1327f3098750Schristos modflags |= MF_IGNCASE;
1328f3098750Schristos break;
1329f3098750Schristos case '-':
1330f3098750Schristos modflags |= MF_SKIN;
1331f3098750Schristos break;
1332f3098750Schristos case ' ':
1333f3098750Schristos case '\t':
1334f3098750Schristos break;
1335f3098750Schristos default:
1336f3098750Schristos goto done;
1337f3098750Schristos }
1338f3098750Schristos }
1339f3098750Schristos done:
1340f3098750Schristos *str = p;
1341f3098750Schristos return modflags;
1342f3098750Schristos }
1343f3098750Schristos
1344f3098750Schristos /************************************************************************/
1345f3098750Schristos /*
1346f3098750Schristos * The key_sort_s compare routines.
1347f3098750Schristos */
1348f3098750Schristos
1349f3098750Schristos static int
keystrcmp(const void * left,const void * right)1350f3098750Schristos keystrcmp(const void *left, const void *right)
1351f3098750Schristos {
1352f3098750Schristos const struct key_sort_s *lp = left;
1353f3098750Schristos const struct key_sort_s *rp = right;
1354f3098750Schristos
1355f3098750Schristos lp = left;
1356f3098750Schristos rp = right;
1357f3098750Schristos
1358f3098750Schristos if (rp->key.str == NULL && lp->key.str == NULL)
1359f3098750Schristos return 0;
1360f3098750Schristos else if (rp->key.str == NULL)
1361f3098750Schristos return -1;
1362f3098750Schristos else if (lp->key.str == NULL)
1363f3098750Schristos return 1;
1364f3098750Schristos else
1365f3098750Schristos return strcmp(lp->key.str, rp->key.str);
1366f3098750Schristos }
1367f3098750Schristos
1368f3098750Schristos static int
keystrcasecmp(const void * left,const void * right)1369f3098750Schristos keystrcasecmp(const void *left, const void *right)
1370f3098750Schristos {
1371f3098750Schristos const struct key_sort_s *lp = left;
1372f3098750Schristos const struct key_sort_s *rp = right;
1373f3098750Schristos
1374f3098750Schristos if (rp->key.str == NULL && lp->key.str == NULL)
1375f3098750Schristos return 0;
1376f3098750Schristos else if (rp->key.str == NULL)
1377f3098750Schristos return -1;
1378f3098750Schristos else if (lp->key.str == NULL)
1379f3098750Schristos return 1;
1380f3098750Schristos else
1381f3098750Schristos return strcasecmp(lp->key.str, rp->key.str);
1382f3098750Schristos }
1383f3098750Schristos
1384f3098750Schristos static int
keylongcmp(const void * left,const void * right)1385f3098750Schristos keylongcmp(const void *left, const void *right)
1386f3098750Schristos {
1387f3098750Schristos const struct key_sort_s *lp = left;
1388f3098750Schristos const struct key_sort_s *rp = right;
1389f3098750Schristos
1390f3098750Schristos if (lp->key.lines > rp->key.lines)
1391f3098750Schristos return 1;
1392f3098750Schristos
1393f3098750Schristos if (lp->key.lines < rp->key.lines)
1394f3098750Schristos return -1;
1395f3098750Schristos
1396f3098750Schristos return 0;
1397f3098750Schristos }
1398f3098750Schristos
1399f3098750Schristos static int
keyoffcmp(const void * left,const void * right)1400f3098750Schristos keyoffcmp(const void *left, const void *right)
1401f3098750Schristos {
1402f3098750Schristos const struct key_sort_s *lp = left;
1403f3098750Schristos const struct key_sort_s *rp = right;
1404f3098750Schristos
1405f3098750Schristos if (lp->key.size > rp->key.size)
1406f3098750Schristos return 1;
1407f3098750Schristos
1408f3098750Schristos if (lp->key.size < rp->key.size)
1409f3098750Schristos return -1;
1410f3098750Schristos
1411f3098750Schristos return 0;
1412f3098750Schristos }
1413f3098750Schristos
1414f3098750Schristos static int
keytimecmp(const void * left,const void * right)1415f3098750Schristos keytimecmp(const void *left, const void *right)
1416f3098750Schristos {
1417f3098750Schristos double delta;
1418f3098750Schristos const struct key_sort_s *lp = left;
1419f3098750Schristos const struct key_sort_s *rp = right;
1420f3098750Schristos
1421f3098750Schristos delta = difftime(lp->key.time, rp->key.time);
1422f3098750Schristos if (delta > 0)
1423f3098750Schristos return 1;
1424f3098750Schristos
1425f3098750Schristos if (delta < 0)
1426f3098750Schristos return -1;
1427f3098750Schristos
1428f3098750Schristos return 0;
1429f3098750Schristos }
1430f3098750Schristos
1431f3098750Schristos /************************************************************************
1432f3098750Schristos * key_sort_s loading routines.
1433f3098750Schristos */
1434f3098750Schristos static void
field_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key,int skin_it)1435f3098750Schristos field_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1436f3098750Schristos const char *key, int skin_it)
1437f3098750Schristos {
1438c172e3b9Slukem size_t i;
1439f3098750Schristos for (i = 0; i < mcount; i++) {
1440f3098750Schristos marray[i].mp = mp;
1441f3098750Schristos marray[i].key.str =
1442f3098750Schristos skin_it ? skin(hfield(key, mp)) : hfield(key, mp);
1443f3098750Schristos marray[i].index = mp->m_index;
1444f3098750Schristos mp = next_message(mp);
1445f3098750Schristos }
1446f3098750Schristos }
1447f3098750Schristos
1448f3098750Schristos static void
subj_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key __unused,int flags __unused)1449f3098750Schristos subj_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1450f3098750Schristos const char *key __unused, int flags __unused)
1451f3098750Schristos {
1452c172e3b9Slukem size_t i;
1453f3098750Schristos #ifdef __lint__
1454f3098750Schristos flags = flags;
1455f3098750Schristos key = key;
1456f3098750Schristos #endif
1457f3098750Schristos for (i = 0; i < mcount; i++) {
1458f3098750Schristos char *subj = hfield(key, mp);
1459f3098750Schristos while (strncasecmp(subj, "Re:", 3) == 0)
1460d727506fSchristos subj = skip_WSP(subj + 3);
1461f3098750Schristos marray[i].mp = mp;
1462f3098750Schristos marray[i].key.str = subj;
1463f3098750Schristos marray[i].index = mp->m_index;
1464f3098750Schristos mp = next_message(mp);
1465f3098750Schristos }
1466f3098750Schristos }
1467f3098750Schristos
1468f3098750Schristos
1469f3098750Schristos static void
lines_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key __unused,int flags)1470f3098750Schristos lines_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1471f3098750Schristos const char *key __unused, int flags)
1472f3098750Schristos {
1473c172e3b9Slukem size_t i;
1474f3098750Schristos int use_blines;
1475f3098750Schristos int use_hlines;
1476f3098750Schristos #ifdef __lint__
1477f3098750Schristos key = key;
1478f3098750Schristos #endif
1479f3098750Schristos #define HLINES 1
1480f3098750Schristos #define BLINES 2
1481f3098750Schristos #define TLINES 3
1482f3098750Schristos use_hlines = flags == HLINES;
1483f3098750Schristos use_blines = flags == BLINES;
1484f3098750Schristos
1485f3098750Schristos for (i = 0; i < mcount; i++) {
1486f3098750Schristos marray[i].mp = mp;
1487f3098750Schristos marray[i].key.lines = use_hlines ? mp->m_lines - mp->m_blines :
1488f3098750Schristos use_blines ? mp->m_blines : mp->m_lines;
1489f3098750Schristos marray[i].index = mp->m_index;
1490f3098750Schristos mp = next_message(mp);
1491f3098750Schristos }
1492f3098750Schristos }
1493f3098750Schristos
1494f3098750Schristos static void
size_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key __unused,int flags __unused)1495f3098750Schristos size_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1496f3098750Schristos const char *key __unused, int flags __unused)
1497f3098750Schristos {
1498c172e3b9Slukem size_t i;
1499f3098750Schristos #ifdef __lint__
1500f3098750Schristos flags = flags;
1501f3098750Schristos key = key;
1502f3098750Schristos #endif
1503f3098750Schristos for (i = 0; i < mcount; i++) {
1504f3098750Schristos marray[i].mp = mp;
1505f3098750Schristos marray[i].key.size = mp->m_size;
1506f3098750Schristos marray[i].index = mp->m_index;
1507f3098750Schristos mp = next_message(mp);
1508f3098750Schristos }
1509f3098750Schristos }
1510f3098750Schristos
1511f3098750Schristos static void __unused
date_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key __unused,int flags)1512f3098750Schristos date_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1513f3098750Schristos const char *key __unused, int flags)
1514f3098750Schristos {
1515c172e3b9Slukem size_t i;
1516f3098750Schristos int use_hl_date;
1517f3098750Schristos int zero_hour_min_sec;
1518f3098750Schristos #ifdef __lint__
1519f3098750Schristos key = key;
1520f3098750Schristos #endif
1521f3098750Schristos #define RDAY 1
1522f3098750Schristos #define SDAY 2
1523f3098750Schristos #define RDATE 3
1524f3098750Schristos #define SDATE 4
1525f3098750Schristos use_hl_date = (flags == RDAY || flags == RDATE);
1526f3098750Schristos zero_hour_min_sec = (flags == RDAY || flags == SDAY);
1527f3098750Schristos
1528f3098750Schristos for (i = 0; i < mcount; i++) {
1529f3098750Schristos struct tm tm;
1530f3098750Schristos (void)dateof(&tm, mp, use_hl_date);
1531f3098750Schristos if (zero_hour_min_sec) {
1532f3098750Schristos tm.tm_sec = 0;
1533f3098750Schristos tm.tm_min = 0;
1534f3098750Schristos tm.tm_hour = 0;
1535f3098750Schristos }
1536f3098750Schristos marray[i].mp = mp;
1537f3098750Schristos marray[i].key.time = mktime(&tm);
1538f3098750Schristos marray[i].index = mp->m_index;
1539f3098750Schristos mp = next_message(mp);
1540f3098750Schristos }
1541f3098750Schristos }
1542f3098750Schristos
1543f3098750Schristos static void
from_load(struct key_sort_s * marray,size_t mcount,struct message * mp,const char * key __unused,int flags __unused)1544f3098750Schristos from_load(struct key_sort_s *marray, size_t mcount, struct message *mp,
1545f3098750Schristos const char *key __unused, int flags __unused)
1546f3098750Schristos {
1547c172e3b9Slukem size_t i;
1548f3098750Schristos #ifdef __lint__
1549f3098750Schristos flags = flags;
1550f3098750Schristos key = key;
1551f3098750Schristos #endif
1552f3098750Schristos for (i = 0; i < mcount; i++) {
1553f3098750Schristos marray[i].mp = mp;
1554f3098750Schristos marray[i].key.str = nameof(mp, 0);
1555f3098750Schristos marray[i].index = mp->m_index;
1556f3098750Schristos mp = next_message(mp);
1557f3098750Schristos }
1558f3098750Schristos }
1559f3098750Schristos
1560f3098750Schristos /************************************************************************
1561f3098750Schristos * The master table that controls all sorting and threading.
1562f3098750Schristos */
1563f3098750Schristos static const struct key_tbl_s {
1564f3098750Schristos const char *key;
1565f3098750Schristos void (*loadfn)(struct key_sort_s *, size_t, struct message *, const char *, int);
1566f3098750Schristos int flags;
1567f3098750Schristos int (*cmpfn)(const void*, const void*);
1568f3098750Schristos int (*casecmpfn)(const void*, const void*);
1569f3098750Schristos } key_tbl[] = {
1570f3098750Schristos {"blines", lines_load, BLINES, keylongcmp, keylongcmp},
1571f3098750Schristos {"hlines", lines_load, HLINES, keylongcmp, keylongcmp},
1572f3098750Schristos {"tlines", lines_load, TLINES, keylongcmp, keylongcmp},
1573f3098750Schristos {"size", size_load, 0, keyoffcmp, keyoffcmp},
1574f3098750Schristos {"sday", date_load, SDAY, keytimecmp, keytimecmp},
1575f3098750Schristos {"rday", date_load, RDAY, keytimecmp, keytimecmp},
1576f3098750Schristos {"sdate", date_load, SDATE, keytimecmp, keytimecmp},
1577f3098750Schristos {"rdate", date_load, RDATE, keytimecmp, keytimecmp},
1578f3098750Schristos {"from", from_load, 0, keystrcasecmp, keystrcasecmp},
1579f3098750Schristos {"subject", subj_load, 0, keystrcmp, keystrcasecmp},
1580f3098750Schristos {NULL, field_load, 0, keystrcmp, keystrcasecmp},
1581f3098750Schristos };
1582f3098750Schristos
1583f3098750Schristos #ifdef USE_EDITLINE
1584f3098750Schristos /*
1585f3098750Schristos * This is for use in complete.c to get the list of threading key
1586f3098750Schristos * names without exposing the key_tbl[]. The first name is returned
1587f3098750Schristos * if called with a pointer to a NULL pointer. Subsequent calls with
1588f3098750Schristos * the same cookie give successive names. A NULL return indicates the
1589f3098750Schristos * end of the list.
1590f3098750Schristos */
1591f3098750Schristos PUBLIC const char *
thread_next_key_name(const void ** cookie)1592f3098750Schristos thread_next_key_name(const void **cookie)
1593f3098750Schristos {
1594f3098750Schristos const struct key_tbl_s *kp;
1595f3098750Schristos
1596f3098750Schristos kp = *cookie;
1597f3098750Schristos if (kp == NULL)
1598f3098750Schristos kp = key_tbl;
1599f3098750Schristos
1600f3098750Schristos *cookie = kp->key ? &kp[1] : NULL;
1601f3098750Schristos
1602f3098750Schristos return kp->key;
1603f3098750Schristos }
1604f3098750Schristos #endif /* USE_EDITLINE */
1605f3098750Schristos
1606f3098750Schristos static const struct key_tbl_s *
get_key(const char * key)1607f3098750Schristos get_key(const char *key)
1608f3098750Schristos {
1609f3098750Schristos const struct key_tbl_s *kp;
1610f3098750Schristos for (kp = key_tbl; kp->key != NULL; kp++)
1611f3098750Schristos if (strcmp(kp->key, key) == 0)
1612f3098750Schristos return kp;
1613f3098750Schristos return kp;
1614f3098750Schristos }
1615f3098750Schristos
1616f3098750Schristos static int (*
get_cmpfn(const struct key_tbl_s * kp,int ignorecase)1617f3098750Schristos get_cmpfn(const struct key_tbl_s *kp, int ignorecase)
1618f3098750Schristos )(const void*, const void*)
1619f3098750Schristos {
1620f3098750Schristos if (ignorecase)
1621f3098750Schristos return kp->casecmpfn;
1622f3098750Schristos else
1623f3098750Schristos return kp->cmpfn;
1624f3098750Schristos }
1625f3098750Schristos
1626f3098750Schristos static void
thread_current_on(char * str,int modflags,int cutit)1627f3098750Schristos thread_current_on(char *str, int modflags, int cutit)
1628f3098750Schristos {
1629f3098750Schristos const struct key_tbl_s *kp;
1630f3098750Schristos struct key_sort_s *marray;
1631f3098750Schristos size_t mcount;
1632f3098750Schristos state_t oldstate;
1633f3098750Schristos
1634f3098750Schristos oldstate = set_state(~(S_RESTRICT | S_EXPOSE), cutit ? S_EXPOSE : 0);
1635f3098750Schristos
1636f3098750Schristos kp = get_key(str);
1637f3098750Schristos mcount = get_msgCount();
1638f3098750Schristos marray = csalloc(mcount + 1, sizeof(*marray));
1639f3098750Schristos kp->loadfn(marray, mcount, current_thread.t_head, str,
1640f3098750Schristos kp->flags ? kp->flags : modflags & MF_SKIN);
1641f3098750Schristos cmp.fn = get_cmpfn(kp, modflags & MF_IGNCASE);
1642f3098750Schristos cmp.inv = modflags & MF_REVERSE;
1643f3098750Schristos thread_array(marray, mcount, cutit);
1644f3098750Schristos
1645f3098750Schristos if (!S_IS_EXPOSE(oldstate))
1646f3098750Schristos dot = thread_top(dot);
1647f3098750Schristos restore_state(oldstate);
1648f3098750Schristos }
1649f3098750Schristos
1650f3098750Schristos /*
1651f3098750Schristos * The thread command. Thread the current thread on its references or
1652f3098750Schristos * on a specified field.
1653f3098750Schristos */
1654f3098750Schristos PUBLIC int
threadcmd(void * v)1655f3098750Schristos threadcmd(void *v)
1656f3098750Schristos {
1657f3098750Schristos char *str;
1658f3098750Schristos
1659f3098750Schristos str = v;
1660f3098750Schristos if (*str == '\0')
1661f3098750Schristos thread_on_reference(current_thread.t_head);
1662f3098750Schristos else {
1663f3098750Schristos int modflags;
1664f3098750Schristos modflags = get_modifiers(&str);
1665f3098750Schristos thread_current_on(str, modflags, 1);
1666f3098750Schristos }
1667f3098750Schristos thread_announce(v);
1668f3098750Schristos return 0;
1669f3098750Schristos }
1670f3098750Schristos
1671f3098750Schristos /*
1672f3098750Schristos * Remove all threading information, reverting to the startup state.
1673f3098750Schristos */
1674f3098750Schristos PUBLIC int
unthreadcmd(void * v)1675f3098750Schristos unthreadcmd(void *v)
1676f3098750Schristos {
1677f3098750Schristos thread_fix_new_links(message_array.t_head, 0, message_array.t_msgCount);
1678f3098750Schristos thread_announce(v);
1679f3098750Schristos return 0;
1680f3098750Schristos }
1681f3098750Schristos
1682f3098750Schristos /*
1683f3098750Schristos * The sort command.
1684f3098750Schristos */
1685f3098750Schristos PUBLIC int
sortcmd(void * v)1686f3098750Schristos sortcmd(void *v)
1687f3098750Schristos {
1688f3098750Schristos int modflags;
1689f3098750Schristos char *str;
1690f3098750Schristos
1691f3098750Schristos str = v;
1692f3098750Schristos modflags = get_modifiers(&str);
1693f3098750Schristos if (*str != '\0')
1694f3098750Schristos thread_current_on(str, modflags, 0);
1695f3098750Schristos else {
1696f3098750Schristos if (modflags & MF_REVERSE)
1697f3098750Schristos reversecmd_core(¤t_thread);
1698f3098750Schristos else {
1699f3098750Schristos (void)printf("sort on what?\n");
1700f3098750Schristos return 0;
1701f3098750Schristos }
1702f3098750Schristos }
1703f3098750Schristos thread_announce(v);
1704f3098750Schristos return 0;
1705f3098750Schristos }
1706f3098750Schristos
1707f3098750Schristos
1708f3098750Schristos /*
1709f3098750Schristos * Delete duplicate messages (based on their "Message-Id" field).
1710f3098750Schristos */
1711f3098750Schristos /*ARGSUSED*/
1712f3098750Schristos PUBLIC int
deldupscmd(void * v __unused)1713f3098750Schristos deldupscmd(void *v __unused)
1714f3098750Schristos {
1715f3098750Schristos struct message *mp;
1716f3098750Schristos int depth;
1717f3098750Schristos state_t oldstate;
1718f3098750Schristos
171917a5e239Schristos oldstate = set_state(~(S_RESTRICT | S_EXPOSE), S_EXPOSE); /* restrict off, expose on */
1720f3098750Schristos
1721f3098750Schristos thread_current_on(__UNCONST("Message-Id"), 0, 1);
1722f3098750Schristos reindex(¤t_thread);
1723f3098750Schristos redepth(¤t_thread);
1724f3098750Schristos depth = current_thread.t_head->m_depth;
172517a5e239Schristos for (mp = first_message(current_thread.t_head); mp; mp = next_message(mp)) {
1726f3098750Schristos if (mp->m_depth > depth) {
1727f3098750Schristos mp->m_flag &= ~(MPRESERVE | MSAVED | MBOX);
1728f3098750Schristos mp->m_flag |= MDELETED | MTOUCH;
1729f3098750Schristos touch(mp);
1730f3098750Schristos }
173117a5e239Schristos }
173217a5e239Schristos dot = thread_top(dot); /* do this irrespective of the oldstate */
1733f3098750Schristos restore_state(oldstate);
173417a5e239Schristos /* thread_announce(v); */
1735f3098750Schristos return 0;
1736f3098750Schristos }
1737f3098750Schristos
1738f3098750Schristos #endif /* THREAD_SUPPORT */
1739