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