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