]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/uip/sortm.c
1 /* sortm.c - sort messages in a folder by date/time */
2 /* 21Apr90 do subject sorts too - from V. Jacobson */
4 static char ident
[] = "@(#)$Id: sortm.c,v 1.20 1995/12/06 23:46:23 jromine Exp $";
8 #include "../zotnet/tws.h"
12 #include <sys/types.h>
18 #if defined(SYS5) && defined(AUX)
19 #define u_short ushort
24 static struct swit switches
[] = {
34 "subject", -3, /* backward-compatibility */
53 unsigned long s_clock
;
57 static struct smsg
*smsgs
;
60 char *subjsort
= (char *)0; /* sort on subject if != 0 */
62 int submajor
= 0; /* if true, sort on subject-major */
66 static int getws (char *datesw
, int msg
, struct smsg
*smsg
);
71 static int dsort(), read_hdrs (), subsort(), txtsort();
72 static void rename_chain(), rename_msgs();
96 setlocale(LC_ALL
, "");
98 invo_name
= r1bindex (argv
[0], '/');
99 if ((cp
= m_find (invo_name
)) != NULL
) {
100 ap
= brkstring (cp
= getcpy (cp
), " ", "\n");
101 ap
= copyip (ap
, arguments
);
105 (void) copyip (argv
+ 1, ap
);
108 while (cp
= *argp
++) {
110 switch (smatch (++cp
, switches
)) {
112 ambigsw (cp
, switches
);
115 adios (NULLCP
, "-%s unknown", cp
);
117 (void) sprintf(buf
, "%s [+folder] [msgs] [switches]",
119 help (buf
, switches
);
124 adios (NULLCP
, "only one date field at a time");
125 if (!(datesw
= *argp
++) || *datesw
== '-')
126 adios (NULLCP
, "missing argument to %s", argp
[-2]);
131 adios (NULLCP
, "only one text field at a time");
132 if (!(subjsort
= *argp
++) || *subjsort
== '-')
133 adios (NULLCP
, "missing argument to %s", argp
[-2]);
137 subjsort
= "subject";
140 subjsort
= (char *)0;
144 if (!(cp
= *argp
++) || *cp
== '-')
145 adios (NULLCP
, "missing argument to %s", argp
[-2]);
147 cp
++; /* skip any leading zeros */
148 if (!*cp
) { /* hit end of string */
149 submajor
++; /* sort subject-major */
152 if (!isdigit(*cp
) || !(datelimit
= atoi(cp
)))
153 adios (NULLCP
, "impossible limit %s", cp
);
154 datelimit
*= 60*60*24;
157 submajor
= 0; /* use date-major, but */
158 datelimit
= 0; /* use no limit */
168 if (*cp
== '+' || *cp
== '@') {
170 adios (NULLCP
, "only one folder at a time!");
172 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
178 if (!m_find ("path"))
179 free (path ("./", TFOLDER
));
181 msgs
[msgp
++] = "all";
185 folder
= m_getfolder ();
186 maildir
= m_maildir (folder
);
188 if (chdir (maildir
) == NOTOK
)
189 adios (maildir
, "unable to change directory to");
190 if (!(mp
= m_gmsg (folder
)))
191 adios (NULLCP
, "unable to read folder %s", folder
);
193 adios (NULLCP
, "no messages in %s", folder
);
195 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
196 if (!m_convert (mp
, msgs
[msgnum
]))
200 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
201 adios (NULLCP
, "no messages to sort");
204 * sort a list of pointers to our "messages to be sorted".
206 dlist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*dlist
));
208 adios (NULLCP
, "couldn't allocate sort memory");
209 for (i
= 0; i
< nmsgs
; i
++)
210 dlist
[i
] = &smsgs
[i
];
213 if (verbose
) /* announce what we're doing */
215 printf ("sorting by %s-major %s-minor\n",
216 submajor
? subjsort
: datesw
,
217 submajor
? datesw
: subjsort
);
219 printf ("sorting by datefield %s\n", datesw
);
221 /* first sort by date, or by subject-major, date-minor */
222 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
223 submajor
&& subjsort
? txtsort
: dsort
);
226 * if we're sorting on subject, we need another list
227 * in subject order, then a merge pass to collate the
230 if (!submajor
&& subjsort
) { /* already date sorted */
233 register struct smsg
***il
,
237 slist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*slist
));
239 adios (NULLCP
, "couldn't allocate sort memory");
240 bcopy ((char *)dlist
, (char *)slist
, (nmsgs
+1)*sizeof(*slist
));
241 qsort ((char *)slist
, nmsgs
, sizeof(*slist
), subsort
);
244 * make an inversion list so we can quickly find
245 * the collection of messages with the same subj
246 * given a message number.
248 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
250 adios (NULLCP
, "couldn't allocate msg list");
251 for (i
= 0; i
< nmsgs
; i
++)
252 il
[slist
[i
]->s_msg
] = &slist
[i
];
254 * make up the final list, chronological but with
255 * all the same subjects grouped together.
257 flist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*flist
));
259 adios (NULLCP
, "couldn't allocate msg list");
261 for (dp
= dlist
; *dp
;) {
262 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
264 /* see if we already did this guy */
270 * take the next message(s) if there is one,
271 * its subject isn't null and its subject
272 * is the same as this one and it's not too
275 while (*s
&& (*s
)->s_subj
[0] &&
276 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
278 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
288 rename_msgs (mp
, dlist
);
290 m_replace (pfolder
, folder
);
297 read_hdrs (mp
, datesw
)
298 register struct msgs
*mp
;
299 register char *datesw
;
303 register struct smsg
*s
;
305 twscopy (&tb
, dtwstime ());
307 smsgs
= (struct smsg
*)
308 calloc ((unsigned) (mp
->hghsel
- mp
->lowsel
+ 2),
311 adios (NULLCP
, "unable to allocate sort storage");
314 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
315 if (mp
->msgstats
[msgnum
] & SELECTED
) {
316 if (getws (datesw
, msgnum
, s
)) {
327 getws (datesw
, msg
, smsg
)
328 register char *datesw
;
330 register struct smsg
*smsg
;
337 register struct tws
*tw
;
338 register char *datecomp
= NULLCP
,
342 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
343 admonish (msgnam
, "unable to read message");
346 for (compnum
= 1, state
= FLD
;;) {
347 switch (state
= m_getfld (state
, nam
, buf
, sizeof buf
, in
)) {
352 if (uleq (nam
, datesw
)) {
353 datecomp
= add (buf
, datecomp
);
354 while (state
== FLDPLUS
) {
355 state
= m_getfld (state
, nam
, buf
, sizeof buf
, in
);
356 datecomp
= add (buf
, datecomp
);
358 if (!subjsort
|| subjcomp
)
361 else if (subjsort
&& uleq (nam
, subjsort
)) {
362 subjcomp
= add (buf
, subjcomp
);
363 while (state
== FLDPLUS
) {
364 state
= m_getfld (state
, nam
, buf
, sizeof buf
, in
);
365 subjcomp
= add (buf
, subjcomp
);
371 /* just flush this guy */
372 while (state
== FLDPLUS
)
373 state
= m_getfld (state
, nam
, buf
, sizeof buf
, in
);
384 if (state
== LENERR
|| state
== FMTERR
)
385 admonish (NULLCP
, "format error in message %d (header #%d)",
395 adios (NULLCP
, "internal error -- you lose");
400 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
403 admonish (NULLCP
, "can't parse %s field in message %d",
406 /* use the modify time of the file as its date */
407 (void) fstat (fileno (in
), &st
);
408 smsg
->s_clock
= st
.st_mtime
;
411 smsg
->s_clock
= twclock (tw
);
416 * try to make the subject "canonical": delete
417 * leading "re:", everything but letters & smash
418 * letters to lower case.
426 if (strcmp (subjsort
, "subject") == 0)
433 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
441 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
449 smsg
->s_subj
= subjcomp
;
463 register struct smsg
**a
,
466 if ((*a
)->s_clock
< (*b
)->s_clock
)
468 else if ((*a
)->s_clock
> (*b
)->s_clock
)
470 else if ((*a
)->s_msg
< (*b
)->s_msg
)
481 register struct smsg
**a
,
486 if (i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
))
489 return (dsort (a
, b
));
494 register struct smsg
**a
,
499 if (i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
))
501 else if ((*a
)->s_msg
< (*b
)->s_msg
)
507 static void rename_chain (mp
, mlist
, msg
, endmsg
)
508 register struct msgs
*mp
;
520 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
521 mlist
[msg
] = (struct smsg
*)0;
522 old
= smsgs
[nxt
].s_msg
;
523 new = smsgs
[msg
].s_msg
;
524 (void) strcpy (oldname
, m_name (old
));
525 newname
= m_name (new);
527 printf ("message %d becomes message %d\n", old
, new);
529 if (rename (oldname
, newname
) == NOTOK
)
530 adios (newname
, "unable to rename %s to", oldname
);
532 mp
->msgstats
[new] = mp
->msgstats
[old
];
533 if (mp
->curmsg
== old
)
541 /* if (nxt != endmsg); */
542 /* rename_chain (mp, mlist, nxt, endmsg); */
546 rename_msgs (mp
, mlist
)
547 register struct msgs
*mp
;
548 register struct smsg
**mlist
;
557 register struct smsg
*sp
;
559 (void) strcpy (tmpfil
, m_name (mp
->hghmsg
+ 1));
561 for (i
= 0; i
< nmsgs
; i
++) {
562 if (! (sp
= mlist
[i
]))
563 continue; /* did this one */
567 continue; /* this one doesn't move */
570 * the guy that was msg j is about to become msg i.
571 * rename 'j' to make a hole, then recursively rename
572 * guys to fill up the hole.
574 old
= smsgs
[j
].s_msg
;
575 new = smsgs
[i
].s_msg
;
576 (void) strcpy (f1
, m_name (old
));
579 printf ("renaming message chain from %d to %d\n", old
, new);
581 if (rename (f1
, tmpfil
) == NOTOK
)
582 adios (tmpfil
, "unable to rename %s to ", f1
);
583 stats
= mp
->msgstats
[old
];
585 rename_chain (mp
, mlist
, j
, i
);
586 if (rename (tmpfil
, m_name(new)) == NOTOK
)
587 adios (m_name(new), "unable to rename %s to", tmpfil
);
589 mp
->msgstats
[new] = stats
;
590 mp
->msgflags
|= SEQMOD
;