]>
diplodocus.org Git - nmh/blob - uip/mshcmds.c
3 * mshcmds.c -- command handlers in msh
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
12 #include <h/dropsbr.h>
13 #include <h/fmt_scan.h>
14 #include <h/scansbr.h>
20 #include <h/picksbr.h>
24 static char delim3
[] = "-------"; /* from burst.c */
30 * Type for a compare function for qsort. This keeps
33 typedef int (*qsort_comp
) (const void *, const void *);
38 static int burst (struct Msg
*, int, int, int, int);
39 static void forw (char *, char *, int, char **);
40 static void rmm (void);
41 static void show (int);
42 static int eom_action (int);
43 static FILE *mhl_action (char *);
45 static int is_nontext (int);
46 static int get_fields (char *, char *, int, struct Msg
*);
47 static int msgsort (struct Msg
*, struct Msg
*);
48 static int subsort (struct Msg
*, struct Msg
*);
49 static char *sosmash (char *, char *);
50 static int process (int, char *, int, char **);
51 static void copy_message (int, FILE *);
52 static void copy_digest (int, FILE *);
54 extern m_getfld_state_t gstate
;
57 forkcmd (char **args
, char *pgm
)
62 vec
[0] = r1bindex (pgm
, '/');
63 copyip (args
, vec
+ 1, MAXARGS
- 1);
66 context_del (pfolder
);
67 context_replace (pfolder
, fmsh
);/* update current folder */
69 context_save (); /* save the context file */
72 switch (child_id
= fork ()) {
74 advise ("fork", "unable to");
79 SIGNAL (SIGINT
, istat
);
80 SIGNAL (SIGQUIT
, qstat
);
83 fprintf (stderr
, "unable to exec ");
88 pidXwait (child_id
, NULL
);
91 if (fmsh
) { /* assume the worst case */
92 mp
->msgflags
|= MODIFIED
;
98 static struct swit distswit
[] = {
104 { "draftfolder +folder", 0 },
106 { "draftmessage msg", 0 },
108 { "nodraftfolder", 0 },
110 { "editor editor", 0 },
114 { "form formfile", 0 },
120 { "whatnowproc program", 0 },
122 { "nowhatnowproc", 0 },
130 distcmd (char **args
)
133 char *cp
, *msg
= NULL
;
134 char buf
[BUFSIZ
], *vec
[MAXARGS
];
137 forkcmd (args
, cmd_name
);
141 while ((cp
= *args
++)) {
143 switch (smatch (++cp
, distswit
)) {
145 ambigsw (cp
, distswit
);
148 fprintf (stderr
, "-%s unknown\n", cp
);
151 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
152 print_help (buf
, distswit
, 1);
155 case DIANSW
: /* not implemented */
173 if (!(cp
= *args
++) || *cp
== '-') {
174 advise (NULL
, "missing argument to %s", args
[-2]);
180 if (*cp
== '+' || *cp
== '@') {
181 advise (NULL
, "sorry, no folders allowed!");
186 advise (NULL
, "only one message at a time!");
194 vec
[vecp
++] = "-file";
198 if (!m_convert (mp
, msg
))
202 if (mp
->numsel
> 1) {
203 advise (NULL
, "only one message at a time!");
206 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
207 seq_setcur (mp
, mp
->hghsel
);
211 static struct swit explswit
[] = {
231 explcmd (char **args
)
233 int inplace
= 0, quietsw
= 0, verbosw
= 0;
234 int msgp
= 0, hi
, msgnum
;
235 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
239 forkcmd (args
, cmd_name
);
243 while ((cp
= *args
++)) {
245 switch (smatch (++cp
, explswit
)) {
247 ambigsw (cp
, explswit
);
250 fprintf (stderr
, "-%s unknown\n", cp
);
253 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
254 print_help (buf
, explswit
, 1);
276 if (*cp
== '+' || *cp
== '@') {
277 advise (NULL
, "sorry, no folders allowed!");
285 msgs
[msgp
++] = "cur";
286 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
287 if (!m_convert (mp
, msgs
[msgnum
]))
291 smsgs
= (struct Msg
*)
292 calloc ((size_t) (MAXFOLDER
+ 2), sizeof *smsgs
);
294 adios (NULL
, "unable to allocate folder storage");
298 for (msgnum
= mp
->lowsel
;
299 msgnum
<= mp
->hghsel
&& !interrupted
;
301 if (is_selected (mp
, msgnum
))
302 if (burst (smsgs
, msgnum
, inplace
, quietsw
, verbosw
) != OK
)
305 free ((char *) smsgs
);
308 seq_setcur (mp
, mp
->lowsel
);
310 if (hi
<= mp
->hghmsg
)
313 mp
->msgflags
|= MODIFIED
;
319 burst (struct Msg
*smsgs
, int msgnum
, int inplace
, int quietsw
, int verbosw
)
321 int i
, j
, ld3
, wasdlm
, msgp
;
323 char c
, buffer
[BUFSIZ
];
326 ld3
= strlen (delim3
);
328 if (Msgs
[msgnum
].m_scanl
) {
329 free (Msgs
[msgnum
].m_scanl
);
330 Msgs
[msgnum
].m_scanl
= NULL
;
333 pos
= ftell (zp
= msh_ready (msgnum
, 1));
334 for (msgp
= 0; msgp
<= MAXFOLDER
;) {
335 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
337 && pos
< Msgs
[msgnum
].m_stop
)
338 pos
+= (long) strlen (buffer
);
339 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
)
341 fseek (zp
, pos
, SEEK_SET
);
342 smsgs
[msgp
].m_start
= pos
;
345 pos
< Msgs
[msgnum
].m_stop
346 && fgets (buffer
, sizeof buffer
, zp
) != NULL
;
348 if (strncmp (buffer
, delim3
, ld3
) == 0
349 && (msgp
== 1 || c
== '\n')
350 && peekc (zp
) == '\n')
353 pos
+= (long) strlen (buffer
);
355 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
356 if (smsgs
[msgp
].m_start
!= pos
)
357 smsgs
[msgp
++].m_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
358 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
) {
360 smsgs
[msgp
- 1].m_stop
-= ((long) strlen (buffer
) + 1);
363 pos
+= (long) strlen (buffer
);
366 switch (msgp
--) { /* toss "End of XXX Digest" */
368 adios (NULL
, "burst() botch -- you lose big");
372 printf ("message %d not in digest format\n", msgnum
);
377 printf ("%d message%s exploded from digest %d\n",
378 msgp
, msgp
!= 1 ? "s" : "", msgnum
);
382 if ((i
= msgp
+ mp
->hghmsg
) > MAXFOLDER
) {
383 advise (NULL
, "more than %d messages", MAXFOLDER
);
386 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, i
)))
387 adios (NULL
, "unable to allocate folder storage");
392 if (mp
->hghsel
> msgnum
)
396 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
398 printf ("message %d becomes message %d\n", j
, i
);
400 Msgs
[i
].m_bboard_id
= Msgs
[j
].m_bboard_id
;
401 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
402 Msgs
[i
].m_start
= Msgs
[j
].m_start
;
403 Msgs
[i
].m_stop
= Msgs
[j
].m_stop
;
404 Msgs
[i
].m_scanl
= NULL
;
405 if (Msgs
[j
].m_scanl
) {
406 free (Msgs
[j
].m_scanl
);
407 Msgs
[j
].m_scanl
= NULL
;
409 copy_msg_flags (mp
, i
, j
);
412 if (Msgs
[msgnum
].m_bboard_id
== 0)
415 unset_selected (mp
, msgnum
);
416 i
= inplace
? msgnum
+ msgp
: mp
->hghmsg
;
417 for (j
= msgp
; j
>= (inplace
? 0 : 1); i
--, j
--) {
418 if (verbosw
&& i
!= msgnum
)
419 printf ("message %d of digest %d becomes message %d\n",
422 Msgs
[i
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
423 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
424 Msgs
[i
].m_start
= smsgs
[j
].m_start
;
425 Msgs
[i
].m_stop
= smsgs
[j
].m_stop
;
426 Msgs
[i
].m_scanl
= NULL
;
427 copy_msg_flags (mp
, i
, msgnum
);
434 static struct swit fileswit
[] = {
446 { "src +folder", 0 },
450 { "rmmproc program", 0 },
460 filecmd (char **args
)
462 int linksw
= 0, msgp
= 0;
463 int vecp
= 1, i
, msgnum
;
464 char *cp
, buf
[BUFSIZ
];
465 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
468 forkcmd (args
, cmd_name
);
472 while ((cp
= *args
++)) {
474 switch (i
= smatch (++cp
, fileswit
)) {
476 ambigsw (cp
, fileswit
);
479 fprintf (stderr
, "-%s unknown\n", cp
);
482 snprintf (buf
, sizeof(buf
), "%s +folder... [msgs] [switches]", cmd_name
);
483 print_help (buf
, fileswit
, 1);
502 advise (NULL
, "sorry, -%s not allowed!", fileswit
[i
].sw
);
505 if (*cp
== '+' || *cp
== '@')
512 vec
[vecp
++] = "-file";
515 msgs
[msgp
++] = "cur";
516 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
517 if (!m_convert (mp
, msgs
[msgnum
]))
522 for (msgnum
= mp
->lowsel
;
523 msgnum
<= mp
->hghsel
&& !interrupted
;
525 if (is_selected (mp
, msgnum
))
526 if (process (msgnum
, fileproc
, vecp
, vec
)) {
527 unset_selected (mp
, msgnum
);
531 if (mp
->numsel
!= mp
->nummsg
|| linksw
)
532 seq_setcur (mp
, mp
->hghsel
);
539 filehak (char **args
)
541 int result
, vecp
= 0;
542 char *cp
, *cwd
, *vec
[MAXARGS
];
544 while ((cp
= *args
++)) {
546 switch (smatch (++cp
, fileswit
)) {
563 if (*cp
== '+' || *cp
== '@')
570 for (vecp
= 0; (cp
= vec
[vecp
]) && result
== NOTOK
; vecp
++) {
572 cwd
= getcpy (pwd ());
573 chdir (m_maildir (""));
575 if (access (m_maildir (cp
), F_OK
) == NOTOK
)
586 static struct swit foldswit
[] = {
624 foldcmd (char **args
)
626 int fastsw
= 0, headersw
= 0, packsw
= 0;
628 char *cp
, *folder
= NULL
, *msg
= NULL
;
629 char buf
[BUFSIZ
], **vec
= args
;
634 while ((cp
= *args
++)) {
636 switch (smatch (++cp
, foldswit
)) {
638 ambigsw (cp
, foldswit
);
641 fprintf (stderr
, "-%s unknown\n", cp
);
644 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]", cmd_name
);
645 print_help (buf
, foldswit
, 1);
648 case FLALSW
: /* not implemented */
678 if (*cp
== '+' || *cp
== '@') {
680 advise (NULL
, "only one folder at a time!\n");
684 folder
= fmsh
? pluspath (cp
)
689 advise (NULL
, "only one message at a time!\n");
698 advise (NULL
, "null folder names are not permitted");
702 if (access (m_maildir (folder
), R_OK
) == NOTOK
) {
703 advise (folder
, "unable to read");
708 strncpy (buf
, folder
, sizeof(buf
));
709 if (expand (buf
) == NOTOK
)
712 if (access (folder
, R_OK
) == NOTOK
) {
713 advise (folder
, "unable to read");
728 if (!m_convert (mp
, msg
))
732 if (mp
->numsel
> 1) {
733 advise (NULL
, "only one message at a time!");
736 seq_setcur (mp
, mp
->hghsel
);
741 forkcmd (vec
, cmd_name
);
745 if (mp
->lowoff
> 1 && !(mp
= folder_realloc (mp
, 1, mp
->hghmsg
)))
746 adios (NULL
, "unable to allocate folder storage");
748 for (msgnum
= mp
->lowmsg
, hole
= 1; msgnum
<= mp
->hghmsg
; msgnum
++)
749 if (does_exist (mp
, msgnum
)) {
750 if (msgnum
!= hole
) {
751 Msgs
[hole
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
752 Msgs
[hole
].m_top
= Msgs
[msgnum
].m_top
;
753 Msgs
[hole
].m_start
= Msgs
[msgnum
].m_start
;
754 Msgs
[hole
].m_stop
= Msgs
[msgnum
].m_stop
;
755 Msgs
[hole
].m_scanl
= NULL
;
756 if (Msgs
[msgnum
].m_scanl
) {
757 free (Msgs
[msgnum
].m_scanl
);
758 Msgs
[msgnum
].m_scanl
= NULL
;
760 copy_msg_flags (mp
, hole
, msgnum
);
761 if (mp
->curmsg
== msgnum
)
762 seq_setcur (mp
, hole
);
766 if (mp
->nummsg
> 0) {
768 mp
->hghmsg
= hole
- 1;
770 mp
->msgflags
|= MODIFIED
;
776 printf ("%s\n", fmsh
? fmsh
: mp
->foldpath
);
779 printf ("\t\tFolder %*s# of messages (%*srange%*s); cur%*smsg\n",
780 DMAXFOLDER
, "", DMAXFOLDER
- 2, "", DMAXFOLDER
- 2, "",
782 printf (args
? "%22s " : "%s ", fmsh
? fmsh
: mp
->foldpath
);
784 /* check for empty folder */
785 if (mp
->nummsg
== 0) {
786 printf ("has no messages%*s",
787 mp
->msgflags
& OTHERS
? DMAXFOLDER
* 2 + 4 : 0, "");
789 printf ("has %*d message%s (%*d-%*d)",
790 DMAXFOLDER
, mp
->nummsg
, mp
->nummsg
!= 1 ? "s" : "",
791 DMAXFOLDER
, mp
->lowmsg
, DMAXFOLDER
, mp
->hghmsg
);
792 if (mp
->curmsg
>= mp
->lowmsg
793 && mp
->curmsg
<= mp
->hghmsg
)
794 printf ("; cur=%*d", DMAXFOLDER
, mp
->curmsg
);
801 static struct swit forwswit
[] = {
807 { "draftfolder +folder", 0 },
809 { "draftmessage msg", 0 },
811 { "nodraftfolder", 0 },
813 { "editor editor", 0 },
817 { "filter filterfile", 0 },
819 { "form formfile", 0 },
833 { "whatnowproc program", 0 },
843 forwcmd (char **args
)
845 int msgp
= 0, vecp
= 1, msgnum
;
846 char *cp
, *filter
= NULL
, buf
[BUFSIZ
];
847 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
852 forkcmd (args
, cmd_name
);
856 while ((cp
= *args
++)) {
858 switch (smatch (++cp
, forwswit
)) {
860 ambigsw (cp
, forwswit
);
863 fprintf (stderr
, "-%s unknown\n", cp
);
866 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
867 print_help (buf
, forwswit
, 1);
870 case FOANSW
: /* not implemented */
890 if (!(cp
= *args
++) || *cp
== '-') {
891 advise (NULL
, "missing argument to %s", args
[-2]);
897 if (!(filter
= *args
++) || *filter
== '-') {
898 advise (NULL
, "missing argument to %s", args
[-2]);
903 if (access (filter
= myfilter
, R_OK
) == NOTOK
) {
904 advise (filter
, "unable to read default filter file");
912 if (*cp
== '+' || *cp
== '@') {
913 advise (NULL
, "sorry, no folders allowed!");
920 /* foil search of .mh_profile */
921 snprintf (buf
, sizeof(buf
), "%sXXXXXX", invo_name
);
923 tfile
= m_mktemp(buf
, NULL
, NULL
);
924 if (tfile
== NULL
) adios("forwcmd", "unable to create temporary file");
925 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
928 vec
[vecp
++] = "-file";
931 msgs
[msgp
++] = "cur";
932 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
933 if (!m_convert (mp
, msgs
[msgnum
]))
938 strncpy (buf
, filter
, sizeof(buf
));
939 if (expand (buf
) == NOTOK
)
941 if (access (filter
= getcpy (etcpath (buf
)), R_OK
) == NOTOK
) {
942 advise (filter
, "unable to read");
947 forw (cmd_name
, filter
, vecp
, vec
);
948 seq_setcur (mp
, mp
->hghsel
);
955 forw (char *proc
, char *filter
, int vecp
, char **vec
)
957 int i
, child_id
, msgnum
, msgcnt
;
958 char tmpfil
[BUFSIZ
], *args
[MAXARGS
];
962 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
963 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
964 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
968 switch (child_id
= fork ()) {
970 advise ("fork", "unable to");
973 case OK
: /* "trust me" */
974 if (freopen (tmpfil
, "w", stdout
) == NULL
) {
975 fprintf (stderr
, "unable to create ");
979 args
[0] = r1bindex (mhlproc
, '/');
981 args
[i
++] = "-forwall";
984 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
985 if (is_selected (mp
, msgnum
))
986 args
[i
++] = getcpy (m_name (msgnum
));
988 mhlsbr (i
, args
, mhl_action
);
989 m_eomsbr (gstate
, (int (*) ()) 0);
994 if (pidXwait (child_id
, NULL
))
999 if ((out
= fopen (tmpfil
, "w")) == NULL
) {
1000 advise (tmpfil
, "unable to create temporary file");
1005 for (msgnum
= mp
->lowsel
;
1006 msgnum
<= mp
->hghsel
&& !interrupted
;
1008 if (is_selected (mp
, msgnum
)) {
1009 fprintf (out
, "\n\n-------");
1010 if (msgnum
== mp
->lowsel
)
1011 fprintf (out
, " Forwarded Message%s",
1012 mp
->numsel
> 1 ? "s" : "");
1014 fprintf (out
, " Message %d", msgcnt
);
1015 fprintf (out
, "\n\n");
1016 copy_digest (msgnum
, out
);
1020 fprintf (out
, "\n\n------- End of Forwarded Message%s\n",
1021 mp
->numsel
> 1 ? "s" : "");
1027 switch (child_id
= fork ()) {
1029 advise ("fork", "unable to");
1034 SIGNAL (SIGINT
, istat
);
1035 SIGNAL (SIGQUIT
, qstat
);
1037 vec
[vecp
++] = tmpfil
;
1041 fprintf (stderr
, "unable to exec ");
1046 pidXwait (child_id
, NULL
);
1054 static char *hlpmsg
[] = {
1055 "The %s program emulates many of the commands found in the nmh",
1056 "system. Instead of operating on nmh folders, commands to %s concern",
1059 "To see the list of commands available, just type a ``?'' followed by",
1060 "the RETURN key. To find out what switches each command takes, type",
1061 "the name of the command followed by ``-help''. To leave %s, use the",
1062 "``quit'' command.",
1064 "Although a lot of nmh commands are found in %s, not all are fully",
1065 "implemented. %s will always recognize all legal switches for a",
1066 "given command though, and will let you know when you ask for an",
1067 "option that it is unable to perform.",
1069 "Running %s is fun, but using nmh from your shell is far superior.",
1070 "After you have familiarized yourself with the nmh style by using %s,",
1071 "you should try using nmh from the shell. You can still use %s for",
1072 "message files that aren't in nmh format, such as BBoard files.",
1078 helpcmd (char **args
)
1083 for (i
= 0; hlpmsg
[i
]; i
++) {
1084 printf (hlpmsg
[i
], invo_name
);
1090 static struct swit markswit
[] = {
1098 { "sequence name", 0 },
1116 markcmd (char **args
)
1118 int addsw
= 0, deletesw
= 0, debugsw
= 0;
1119 int listsw
= 0, zerosw
= 0;
1121 int msgp
= 0, msgnum
;
1122 char *cp
, buf
[BUFSIZ
];
1123 char *seqs
[NUMATTRS
+ 1], *msgs
[MAXARGS
];
1125 while ((cp
= *args
++)) {
1127 switch (smatch (++cp
, markswit
)) {
1129 ambigsw (cp
, markswit
);
1132 fprintf (stderr
, "-%s unknown\n", cp
);
1135 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1136 print_help (buf
, markswit
, 1);
1141 deletesw
= listsw
= 0;
1149 addsw
= deletesw
= 0;
1153 if (!(cp
= *args
++) || *cp
== '-') {
1154 advise (NULL
, "missing argument to %s", args
[-2]);
1157 if (seqp
< NUMATTRS
)
1160 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1165 case MPUBSW
: /* not implemented */
1181 if (*cp
== '+' || *cp
== '@') {
1182 advise (NULL
, "sorry, no folders allowed!");
1189 if (!addsw
&& !deletesw
&& !listsw
) {
1196 seqs
[seqp
++] = "unseen";
1200 msgs
[msgp
++] = "all";
1205 msgs
[msgp
++] = listsw
? "all" :"cur";
1206 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1207 if (!m_convert (mp
, msgs
[msgnum
]))
1211 printf ("invo_name=%s mypath=%s defpath=%s\n",
1212 invo_name
, mypath
, defpath
);
1213 printf ("ctxpath=%s context flags=%s\n",
1214 ctxpath
, snprintb (buf
, sizeof(buf
), (unsigned) ctxflags
, DBITS
));
1215 printf ("foldpath=%s flags=%s\n",
1217 snprintb (buf
, sizeof(buf
), (unsigned) mp
->msgflags
, FBITS
));
1218 printf ("hghmsg=%d lowmsg=%d nummsg=%d curmsg=%d\n",
1219 mp
->hghmsg
, mp
->lowmsg
, mp
->nummsg
, mp
->curmsg
);
1220 printf ("lowsel=%d hghsel=%d numsel=%d\n",
1221 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
1222 printf ("lowoff=%d hghoff=%d\n", mp
->lowoff
, mp
->hghoff
);
1225 if (seqp
== 0 && (addsw
|| deletesw
)) {
1226 advise (NULL
, "-%s requires at least one -sequence argument",
1227 addsw
? "add" : "delete");
1233 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1234 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1239 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1240 if (!seq_delsel (mp
, seqs
[seqp
], 0, zerosw
))
1244 /* Listing messages in sequences */
1247 /* list the given sequences */
1248 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1249 seq_print (mp
, seqs
[seqp
]);
1251 /* else list them all */
1257 for (msgnum
= mp
->lowsel
;
1258 msgnum
<= mp
->hghsel
&& !interrupted
;
1260 if (is_selected (mp
, msgnum
)) {
1261 printf ("%*d: id=%d top=%d start=%ld stop=%ld %s\n",
1264 Msgs
[msgnum
].m_bboard_id
,
1266 (long) Msgs
[msgnum
].m_start
,
1267 (long) Msgs
[msgnum
].m_stop
,
1268 snprintb (buf
, sizeof(buf
),
1269 (unsigned) mp
->msgstats
[msgnum
- mp
->lowoff
],
1271 if (Msgs
[msgnum
].m_scanl
)
1272 printf ("%s", Msgs
[msgnum
].m_scanl
);
1278 static struct swit mhnswit
[] = {
1281 #define MHNNAUTOSW 1
1283 #define MHNDEBUGSW 2
1285 #define MHNEBCDICSW 3
1286 { "ebcdicsafe", 0 },
1287 #define MHNNEBCDICSW 4
1288 { "noebcdicsafe", 0 },
1290 { "form formfile", 4 },
1293 #define MHNNHEADSW 7
1297 #define MHNNLISTSW 9
1299 #define MHNPARTSW 10
1300 { "part number", 0 },
1301 #define MHNSIZESW 11
1303 #define MHNNSIZESW 12
1304 { "norealsize", 0 },
1305 #define MHNRFC934SW 13
1306 { "rfc934mode", 0 },
1307 #define MHNNRFC934SW 14
1308 { "norfc934mode", 0 },
1309 #define MHNSERIALSW 15
1310 { "serialonly", 0 },
1311 #define MHNNSERIALSW 16
1312 { "noserialonly", 0 },
1313 #define MHNSHOWSW 17
1315 #define MHNNSHOWSW 18
1317 #define MHNSTORESW 19
1319 #define MHNNSTORESW 20
1321 #define MHNTYPESW 21
1322 { "type content", 0 },
1323 #define MHNVERBSW 22
1325 #define MHNNVERBSW 23
1327 #define MHNHELPSW 24
1329 #define MHNPROGSW 25
1330 { "moreproc program", -4 },
1331 #define MHNNPROGSW 26
1332 { "nomoreproc", -3 },
1334 { "length lines", -4 },
1336 { "width columns", -4 },
1342 mhncmd (char **args
)
1344 int msgp
= 0, vecp
= 1;
1346 char *cp
, buf
[BUFSIZ
];
1347 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
1350 forkcmd (args
, cmd_name
);
1353 while ((cp
= *args
++)) {
1355 switch (smatch (++cp
, mhnswit
)) {
1357 ambigsw (cp
, mhnswit
);
1360 fprintf (stderr
, "-%s unknown\n", cp
);
1363 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1364 print_help (buf
, mhnswit
, 1);
1399 if (!(cp
= *args
++) || *cp
== '-') {
1400 advise (NULL
, "missing argument to %s", args
[-2]);
1407 if (*cp
== '+' || *cp
== '@') {
1408 advise (NULL
, "sorry, no folders allowed!");
1416 vec
[vecp
++] = "-file";
1419 msgs
[msgp
++] = "cur";
1420 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1421 if (!m_convert (mp
, msgs
[msgnum
]))
1426 for (msgnum
= mp
->lowsel
;
1427 msgnum
<= mp
->hghsel
&& !interrupted
;
1429 if (is_selected (mp
, msgnum
))
1430 if (process (msgnum
, cmd_name
, vecp
, vec
)) {
1431 unset_selected (mp
, msgnum
);
1435 seq_setcur (mp
, mp
->hghsel
);
1439 static struct swit packswit
[] = {
1447 static int mbx_style
= MMDF_FORMAT
;
1450 packcmd (char **args
)
1452 int msgp
= 0, md
, msgnum
;
1453 char *cp
, *file
= NULL
;
1454 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
1458 forkcmd (args
, cmd_name
);
1462 while ((cp
= *args
++)) {
1464 switch (smatch (++cp
, packswit
)) {
1466 ambigsw (cp
, packswit
);
1469 fprintf (stderr
, "-%s unknown\n", cp
);
1472 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1473 print_help (buf
, packswit
, 1);
1477 if (!(file
= *args
++) || *file
== '-') {
1478 advise (NULL
, "missing argument to %s", args
[-2]);
1483 if (*cp
== '+' || *cp
== '@') {
1484 advise (NULL
, "sorry, no folders allowed!");
1493 file
= path (file
, TFILE
);
1494 if (stat (file
, &st
) == NOTOK
) {
1495 if (errno
!= ENOENT
) {
1496 advise (file
, "error on file");
1499 md
= getanswer (cp
= concat ("Create file \"", file
, "\"? ", NULL
));
1506 msgs
[msgp
++] = "all";
1507 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1508 if (!m_convert (mp
, msgs
[msgnum
]))
1512 if ((md
= mbx_open (file
, mbx_style
, getuid (), getgid (), m_gmprot ())) == NOTOK
) {
1513 advise (file
, "unable to open");
1516 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1517 if (is_selected (mp
, msgnum
))
1518 if (pack (file
, md
, msgnum
) == NOTOK
)
1520 mbx_close (file
, md
);
1522 if (mp
->hghsel
!= mp
->curmsg
)
1523 seq_setcur (mp
, mp
->lowsel
);
1531 pack (char *mailbox
, int md
, int msgnum
)
1535 if (Msgs
[msgnum
].m_bboard_id
== 0)
1538 zp
= msh_ready (msgnum
, 1);
1539 return mbx_write (mailbox
, md
, zp
, Msgs
[msgnum
].m_bboard_id
,
1540 0L, ftell (zp
), Msgs
[msgnum
].m_stop
, 1, 1);
1545 packhak (char **args
)
1548 char *cp
, *file
= NULL
;
1550 while ((cp
= *args
++)) {
1552 switch (smatch (++cp
, packswit
)) {
1559 if (!(file
= *args
++) || *file
== '-')
1563 if (*cp
== '+' || *cp
== '@')
1567 file
= path (file
? file
: "./msgbox", TFILE
);
1568 result
= access (file
, F_OK
) == NOTOK
? OK
: NOTOK
;
1575 static struct swit pickswit
[] = {
1587 { "cc pattern", 0 },
1589 { "date pattern", 0 },
1591 { "from pattern", 0 },
1593 { "search pattern", 0 },
1595 { "subject pattern", 0 },
1597 { "to pattern", 0 },
1599 { "-othercomponent pattern", 15 },
1601 { "after date", 0 },
1603 { "before date", 0 },
1605 { "datefield field", 5 },
1607 { "sequence name", 0 },
1627 pickcmd (char **args
)
1629 int zerosw
= 1, msgp
= 0;
1631 int vecp
= 0, hi
, lo
, msgnum
;
1632 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1633 char *seqs
[NUMATTRS
], *vec
[MAXARGS
];
1636 while ((cp
= *args
++)) {
1642 switch (smatch (cp
, pickswit
)) {
1644 ambigsw (cp
, pickswit
);
1647 fprintf (stderr
, "-%s unknown\n", cp
);
1650 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1651 print_help (buf
, pickswit
, 1);
1665 if (!(cp
= *args
++)) {/* allow -xyz arguments */
1666 advise (NULL
, "missing argument to %s", args
[-2]);
1672 advise (NULL
, "internal error!");
1683 if (!(cp
= *args
++) || *cp
== '-') {
1684 advise (NULL
, "missing argument to %s", args
[-2]);
1687 if (seqp
< NUMATTRS
)
1690 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1701 case PIPUSW
: /* not implemented */
1708 if (*cp
== '+' || *cp
== '@') {
1709 advise (NULL
, "sorry, no folders allowed!");
1718 msgs
[msgp
++] = "all";
1719 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1720 if (!m_convert (mp
, msgs
[msgnum
]))
1725 if (!pcompile (vec
, NULL
))
1731 for (msgnum
= mp
->lowsel
;
1732 msgnum
<= mp
->hghsel
&& !interrupted
;
1734 if (is_selected (mp
, msgnum
)) {
1735 zp
= msh_ready (msgnum
, 1);
1736 if (pmatches (zp
, msgnum
, fmsh
? 0L : Msgs
[msgnum
].m_start
,
1737 fmsh
? 0L : Msgs
[msgnum
].m_stop
)) {
1744 unset_selected (mp
, msgnum
);
1755 if (mp
->numsel
<= 0) {
1756 advise (NULL
, "no messages match specification");
1761 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1762 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1765 printf ("%d hit%s\n", mp
->numsel
, mp
->numsel
== 1 ? "" : "s");
1769 static struct swit replswit
[] = {
1773 { "noannotate", 0 },
1779 { "draftfolder +folder", 0 },
1781 { "draftmessage msg", 0 },
1783 { "nodraftfolder", 0 },
1785 { "editor editor", 0 },
1789 { "fcc +folder", 0 },
1791 { "filter filterfile", 0 },
1793 { "form formfile", 0 },
1803 { "whatnowproc program", 0 },
1807 { "width columns", 0 },
1815 replcmd (char **args
)
1818 char *cp
, *msg
= NULL
;
1819 char buf
[BUFSIZ
], *vec
[MAXARGS
];
1822 forkcmd (args
, cmd_name
);
1826 while ((cp
= *args
++)) {
1828 switch (smatch (++cp
, replswit
)) {
1830 ambigsw (cp
, replswit
);
1833 fprintf (stderr
, "-%s unknown\n", cp
);
1836 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1837 print_help (buf
, replswit
, 1);
1840 case REANSW
: /* not implemented */
1865 if (!(cp
= *args
++) || *cp
== '-') {
1866 advise (NULL
, "missing argument to %s", args
[-2]);
1872 if (*cp
== '+' || *cp
== '@') {
1873 advise (NULL
, "sorry, no folders allowed!");
1878 advise (NULL
, "only one message at a time!");
1886 vec
[vecp
++] = "-file";
1890 if (!m_convert (mp
, msg
))
1894 if (mp
->numsel
> 1) {
1895 advise (NULL
, "only one message at a time!");
1898 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
1899 seq_setcur (mp
, mp
->hghsel
);
1903 static struct swit rmmswit
[] = {
1911 rmmcmd (char **args
)
1913 int msgp
= 0, msgnum
;
1914 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1916 while ((cp
= *args
++)) {
1918 switch (smatch (++cp
, rmmswit
)) {
1920 ambigsw (cp
, rmmswit
);
1923 fprintf (stderr
, "-%s unknown\n", cp
);
1926 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1927 print_help (buf
, rmmswit
, 1);
1930 if (*cp
== '+' || *cp
== '@') {
1931 advise (NULL
, "sorry, no folders allowed!");
1939 msgs
[msgp
++] = "cur";
1940 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1941 if (!m_convert (mp
, msgs
[msgnum
]))
1952 register int msgnum
, vecp
;
1954 char buffer
[BUFSIZ
], *vec
[MAXARGS
];
1958 if (mp
->numsel
> MAXARGS
- 1) {
1959 advise (NULL
, "more than %d messages for %s exec",
1960 MAXARGS
- 1, rmmproc
);
1964 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1965 if (is_selected (mp
, msgnum
))
1966 vec
[vecp
++] = getcpy (m_name (msgnum
));
1968 forkcmd (vec
, rmmproc
);
1969 for (vecp
= 0; vec
[vecp
]; vecp
++)
1973 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1974 if (is_selected (mp
, msgnum
)) {
1975 strncpy (buffer
, m_backup (cp
= m_name (msgnum
)), sizeof(buffer
));
1976 if (rename (cp
, buffer
) == NOTOK
)
1977 admonish (buffer
, "unable to rename %s to", cp
);
1981 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1982 if (is_selected (mp
, msgnum
)) {
1983 set_deleted (mp
, msgnum
);
1984 unset_exists (mp
, msgnum
);
1987 if ((mp
->nummsg
-= mp
->numsel
) <= 0) {
1989 admonish (NULL
, "no messages remaining in +%s", fmsh
);
1991 admonish (NULL
, "no messages remaining in %s", mp
->foldpath
);
1992 mp
->lowmsg
= mp
->hghmsg
= mp
->nummsg
= 0;
1994 if (mp
->lowsel
== mp
->lowmsg
) {
1995 for (msgnum
= mp
->lowmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1996 if (does_exist (mp
, msgnum
))
1998 mp
->lowmsg
= msgnum
;
2000 if (mp
->hghsel
== mp
->hghmsg
) {
2001 for (msgnum
= mp
->hghmsg
- 1; msgnum
>= mp
->lowmsg
; msgnum
--)
2002 if (does_exist (mp
, msgnum
))
2004 mp
->hghmsg
= msgnum
;
2007 mp
->msgflags
|= MODIFIED
;
2012 static struct swit scanswit
[] = {
2018 { "form formatfile", 0 },
2020 { "format string", 5 },
2026 { "width columns", 0 },
2034 scancmd (char **args
)
2036 #define equiv(a,b) (a ? b && !strcmp (a, b) : !b)
2038 int clearsw
= 0, headersw
= 0, width
= 0, msgp
= 0;
2039 int msgnum
, optim
, state
;
2040 char *cp
, *form
= NULL
, *format
= NULL
;
2041 char buf
[BUFSIZ
], *nfs
, *msgs
[MAXARGS
];
2043 static int s_optim
= 0;
2044 static char *s_form
= NULL
, *s_format
= NULL
;
2046 while ((cp
= *args
++)) {
2048 switch (smatch (++cp
, scanswit
)) {
2050 ambigsw (cp
, scanswit
);
2053 fprintf (stderr
, "-%s unknown\n", cp
);
2056 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
2057 print_help (buf
, scanswit
, 1);
2073 if (!(form
= *args
++) || *form
== '-') {
2074 advise (NULL
, "missing argument to %s", args
[-2]);
2080 if (!(format
= *args
++) || *format
== '-') {
2081 advise (NULL
, "missing argument to %s", args
[-2]);
2087 if (!(cp
= *args
++) || *cp
== '-') {
2088 advise (NULL
, "missing argument to %s", args
[-2]);
2094 if (*cp
== '+' || *cp
== '@') {
2095 advise (NULL
, "sorry, no folders allowed!");
2103 msgs
[msgp
++] = "all";
2104 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2105 if (!m_convert (mp
, msgs
[msgnum
]))
2109 /* Get new format string */
2110 nfs
= new_fs (form
, format
, FORMAT
);
2112 /* force scansbr to (re)compile format */
2119 s_optim
= optim
= 1;
2120 s_form
= form
? getcpy (form
) : NULL
;
2121 s_format
= format
? getcpy (format
) : NULL
;
2125 optim
= equiv (s_form
, form
) && equiv (s_format
, format
);
2128 for (msgnum
= mp
->lowsel
;
2129 msgnum
<= mp
->hghsel
&& !interrupted
;
2131 if (is_selected (mp
, msgnum
)) {
2132 if (optim
&& Msgs
[msgnum
].m_scanl
)
2133 printf ("%s", Msgs
[msgnum
].m_scanl
);
2136 zp
= msh_ready (msgnum
, 0);
2137 switch (state
= scan (zp
, msgnum
, 0, nfs
, width
,
2138 msgnum
== mp
->curmsg
,
2139 is_unseen (mp
, msgnum
),
2140 headersw
? (fmsh
? fmsh
: mp
->foldpath
) : NULL
,
2141 fmsh
? 0L : (long) (Msgs
[msgnum
].m_stop
- Msgs
[msgnum
].m_start
),
2147 Msgs
[msgnum
].m_scanl
= getcpy (scanl
);
2151 advise (NULL
, "scan() botch (%d)", state
);
2155 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
2167 static struct swit showswit
[] = {
2171 { "form formfile", 4 },
2173 { "moreproc program", 4 },
2175 { "nomoreproc", 3 },
2177 { "length lines", 4 },
2179 { "width columns", 4 },
2181 { "showproc program", 4 },
2183 { "noshowproc", 3 },
2195 showcmd (char **args
)
2197 int headersw
= 1, nshow
= 0, msgp
= 0, vecp
= 1;
2198 int mhl
= 0, seqnum
= -1, mode
= 0, i
, msgnum
;
2199 char *cp
, *proc
= showproc
, buf
[BUFSIZ
];
2200 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
2202 if (!mh_strcasecmp (cmd_name
, "next"))
2205 if (!mh_strcasecmp (cmd_name
, "prev"))
2207 while ((cp
= *args
++)) {
2209 switch (i
= smatch (++cp
, showswit
)) {
2211 ambigsw (cp
, showswit
);
2218 snprintf (buf
, sizeof(buf
), "%s %s[switches] [switches for showproc]",
2219 cmd_name
, mode
? NULL
: "[msgs] ");
2220 print_help (buf
, showswit
, 1);
2228 if (!(cp
= *args
++) || *cp
== '-') {
2229 advise (NULL
, "missing argument to %s", args
[-2]);
2241 if (!(proc
= *args
++) || *proc
== '-') {
2242 advise (NULL
, "missing argument to %s", args
[-2]);
2252 advise (NULL
, "sorry, -%s not allowed!", showswit
[i
].sw
);
2255 if (*cp
== '+' || *cp
== '@') {
2256 advise (NULL
, "sorry, no folders allowed!");
2262 "usage: %s [switches] [switches for showproc]\n",
2272 msgs
[msgp
++] = mode
> 0 ? "next" : mode
< 0 ? "prev" : "cur";
2273 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2274 if (!m_convert (mp
, msgs
[msgnum
]))
2279 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2280 if (is_selected (mp
, msgnum
) && is_nontext (msgnum
)) {
2281 proc
= showmimeproc
;
2282 vec
[vecp
++] = "-file";
2290 if (strcmp (showproc
, "mhl") == 0) {
2296 seqnum
= seq_getnum (mp
, "unseen");
2297 vec
[0] = r1bindex (proc
, '/');
2300 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2301 if (is_selected (mp
, msgnum
)) {
2302 vec
[vecp
++] = getcpy (m_name (msgnum
));
2304 seq_delmsg (mp
, "unseen", msgnum
);
2307 if (mp
->numsel
== 1 && headersw
)
2309 mhlsbr (vecp
, vec
, mhl_action
);
2310 m_eomsbr (gstate
, (int (*)()) 0);
2315 for (msgnum
= mp
->lowsel
;
2316 msgnum
<= mp
->hghsel
&& !interrupted
;
2318 if (is_selected (mp
, msgnum
)) {
2319 switch (ask (msgnum
)) {
2320 case NOTOK
: /* QUIT */
2327 if (mp
->numsel
== 1 && headersw
)
2330 copy_message (msgnum
, stdout
);
2332 process (msgnum
, proc
, vecp
, vec
);
2335 seq_delmsg (mp
, "unseen", msgnum
);
2342 seq_setcur (mp
, mp
->hghsel
);
2349 if (Msgs
[msgnum
].m_bboard_id
== 0)
2352 printf ("(Message %d", msgnum
);
2353 if (Msgs
[msgnum
].m_bboard_id
> 0)
2354 printf (", %s: %d", BBoard_ID
, Msgs
[msgnum
].m_bboard_id
);
2365 return (ftell (mhlfp
) >= Msgs
[mhlnum
].m_stop
);
2370 mhl_action (char *name
)
2374 if ((msgnum
= m_atoi (name
)) < mp
->lowmsg
2375 || msgnum
> mp
->hghmsg
2376 || !does_exist (mp
, msgnum
))
2380 mhlfp
= msh_ready (msgnum
, 1);
2382 m_eomsbr (gstate
, eom_action
);
2394 if (mp
->numsel
== 1 || !interactive
|| redirected
)
2397 if (SOprintf ("Press <return> to list \"%d\"...", msgnum
)) {
2398 if (mp
->lowsel
!= msgnum
)
2400 printf ("Press <return> to list \"%d\"...", msgnum
);
2405 read (fileno (stdout
), buf
, sizeof buf
);
2407 if (strchr(buf
, '\n') == NULL
)
2411 told_to_quit
= interrupted
= 0;
2426 is_nontext (int msgnum
)
2429 unsigned char *bp
, *dp
;
2431 char buf
[BUFSIZ
], name
[NAMESZ
];
2434 if (Msgs
[msgnum
].m_flags
& MHNCHK
)
2435 return (Msgs
[msgnum
].m_flags
& MHNYES
);
2436 Msgs
[msgnum
].m_flags
|= MHNCHK
;
2438 fp
= msh_ready (msgnum
, 1);
2441 int bufsz
= sizeof buf
;
2442 switch (state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
)) {
2446 * Check Content-Type field
2448 if (!mh_strcasecmp (name
, TYPE_FIELD
)) {
2452 cp
= add (buf
, NULL
);
2453 while (state
== FLDPLUS
) {
2455 state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
);
2462 for (; isspace (*bp
); bp
++)
2467 for (bp
++, i
= 0;;) {
2497 for (dp
= bp
; istoken (*dp
); dp
++)
2504 if ((result
= (mh_strcasecmp (bp
, "plain") != 0)))
2507 for (dp
++; isspace (*dp
); dp
++)
2510 if ((result
= !uprf (dp
, "charset")))
2512 dp
+= sizeof "charset" - 1;
2513 while (isspace (*dp
))
2517 while (isspace (*dp
))
2520 if ((bp
= strchr(++dp
, '"')))
2523 for (bp
= dp
; *bp
; bp
++)
2524 if (isspace (*bp
)) {
2530 /* Default character set */
2533 /* Check the character set */
2534 result
= !check_charset (dp
, strlen (dp
));
2536 if (!(result
= (mh_strcasecmp (bp
, "text") != 0))) {
2546 Msgs
[msgnum
].m_flags
|= MHNYES
;
2553 * Check Content-Transfer-Encoding field
2555 if (!mh_strcasecmp (name
, ENCODING_FIELD
)) {
2556 cp
= add (buf
, NULL
);
2557 while (state
== FLDPLUS
) {
2559 state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
);
2562 for (bp
= cp
; isspace (*bp
); bp
++)
2564 for (dp
= bp
; istoken (*dp
); dp
++)
2567 result
= (mh_strcasecmp (bp
, "7bit")
2568 && mh_strcasecmp (bp
, "8bit")
2569 && mh_strcasecmp (bp
, "binary"));
2573 Msgs
[msgnum
].m_flags
|= MHNYES
;
2580 * Just skip the rest of this header
2581 * field and go to next one.
2583 while (state
== FLDPLUS
) {
2585 state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
);
2590 * We've passed the message header,
2591 * so message is just text.
2600 static struct swit sortswit
[] = {
2602 { "datefield field", 0 },
2604 { "textfield field", 0 },
2606 { "notextfield", 0 },
2608 { "limit days", 0 },
2622 sortcmd (char **args
)
2624 int msgp
= 0, msgnum
;
2625 char *cp
, *datesw
= NULL
, *subjsw
= NULL
;
2626 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
2630 forkcmd (args
, cmd_name
);
2634 while ((cp
= *args
++)) {
2636 switch (smatch (++cp
, sortswit
)) {
2638 ambigsw (cp
, sortswit
);
2641 fprintf (stderr
, "-%s unknown\n", cp
);
2644 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
2645 print_help (buf
, sortswit
, 1);
2650 advise (NULL
, "only one date field at a time!");
2653 if (!(datesw
= *args
++) || *datesw
== '-') {
2654 advise (NULL
, "missing argument to %s", args
[-2]);
2661 advise (NULL
, "only one text field at a time!");
2664 if (!(subjsw
= *args
++) || *subjsw
== '-') {
2665 advise (NULL
, "missing argument to %s", args
[-2]);
2673 case SOLIMT
: /* too hard */
2674 if (!(cp
= *args
++) || *cp
== '-') {
2675 advise (NULL
, "missing argument to %s", args
[-2]);
2679 case SOVERB
: /* not implemented */
2683 if (*cp
== '+' || *cp
== '@') {
2684 advise (NULL
, "sorry, no folders allowed!");
2692 msgs
[msgp
++] = "all";
2695 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2696 if (!m_convert (mp
, msgs
[msgnum
]))
2700 twscopy (&tb
, dlocaltimenow ());
2702 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2703 if (Msgs
[msgnum
].m_scanl
) {
2704 free (Msgs
[msgnum
].m_scanl
);
2705 Msgs
[msgnum
].m_scanl
= NULL
;
2707 if (is_selected (mp
, msgnum
)) {
2708 if (get_fields (datesw
, subjsw
, msgnum
, &Msgs
[msgnum
]))
2709 twscopy (&Msgs
[msgnum
].m_tb
,
2710 msgnum
!= mp
->lowsel
? &Msgs
[msgnum
- 1].m_tb
: &tb
);
2712 else /* m_scaln is already NULL */
2713 twscopy (&Msgs
[msgnum
].m_tb
, &tb
);
2714 Msgs
[msgnum
].m_stats
= mp
->msgstats
[msgnum
- mp
->lowoff
];
2715 if (mp
->curmsg
== msgnum
)
2716 Msgs
[msgnum
].m_stats
|= CUR
;
2719 qsort ((char *) &Msgs
[mp
->lowsel
], mp
->hghsel
- mp
->lowsel
+ 1,
2720 sizeof(struct Msg
), (qsort_comp
) (subjsw
? subsort
: msgsort
));
2722 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2723 if (subjsw
&& Msgs
[msgnum
].m_scanl
) {
2724 free (Msgs
[msgnum
].m_scanl
); /* from subjsort */
2725 Msgs
[msgnum
].m_scanl
= NULL
;
2727 mp
->msgstats
[msgnum
- mp
->lowoff
] = Msgs
[msgnum
].m_stats
& ~CUR
;
2728 if (Msgs
[msgnum
].m_stats
& CUR
)
2729 seq_setcur (mp
, msgnum
);
2732 mp
->msgflags
|= MODIFIED
;
2738 * get_fields - parse message, and get date and subject if needed.
2739 * We'll use the msgp->m_tb tws struct for the date, and overload
2740 * the msgp->m_scanl field with our subject string.
2743 get_fields (char *datesw
, char *subjsw
, int msgnum
, struct Msg
*msgp
)
2745 int state
, gotdate
= 0;
2746 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
2747 struct tws
*tw
= (struct tws
*) 0;
2750 zp
= msh_ready (msgnum
, 0);
2753 int bufsz
= sizeof buf
;
2754 switch (state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
)) {
2757 if (!mh_strcasecmp (name
, datesw
)) {
2759 while (state
== FLDPLUS
) {
2761 state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
);
2764 if ((tw
= dparsetime (bp
)) == NULL
)
2766 "unable to parse %s field in message %d",
2769 twscopy (&(msgp
->m_tb
), tw
);
2771 if (!subjsw
) /* not using this, or already done */
2772 break; /* all done! */
2775 else if (subjsw
&& !mh_strcasecmp(name
, subjsw
)) {
2777 while (state
== FLDPLUS
) {
2779 state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
);
2782 msgp
->m_scanl
= sosmash(subjsw
, bp
);
2784 break; /* date done so we're done */
2786 subjsw
= (char *)0;/* subject done, need date */
2788 while (state
== FLDPLUS
) { /* flush this one */
2790 state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
);
2801 admonish (NULL
, "format error in message %d", msgnum
);
2802 if (msgp
->m_scanl
) { /* this might need free'd */
2803 free (msgp
->m_scanl
); /* probably can't use subj anyway */
2804 msgp
->m_scanl
= NULL
;
2809 adios (NULL
, "internal error -- you lose");
2814 return OK
; /* not an error if subj not found */
2816 admonish (NULL
, "no %s field in message %d", datesw
, msgnum
);
2817 return NOTOK
; /* NOTOK means use some other date */
2826 msgsort (struct Msg
*a
, struct Msg
*b
)
2828 return twsort (&a
->m_tb
, &b
->m_tb
);
2833 subsort (struct Msg
*a
, struct Msg
*b
)
2837 if (a
->m_scanl
&& b
->m_scanl
)
2838 if ((i
= strcmp (a
->m_scanl
, b
->m_scanl
)))
2841 return twsort (&a
->m_tb
, &b
->m_tb
);
2846 * try to make the subject "canonical": delete leading "re:", everything
2847 * but letters & smash letters to lower case.
2850 sosmash (char *subj
, char *s
)
2852 register char *cp
, *dp
;
2853 register unsigned char c
;
2857 dp
= s
; /* dst pointer */
2858 if (!mh_strcasecmp (subj
, "subject"))
2865 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2871 while ((c
= *cp
++)) {
2873 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2883 process (int msgnum
, char *proc
, int vecp
, char **vec
)
2885 int child_id
, status
;
2886 char tmpfil
[BUFSIZ
];
2891 strncpy (tmpfil
, m_name (msgnum
), sizeof(tmpfil
));
2892 context_del (pfolder
);
2893 context_replace (pfolder
, fmsh
);/* update current folder */
2895 context_save (); /* save the context file */
2899 cp
= m_mktemp(invo_name
, NULL
, &out
);
2901 /* Try again, but try to create under /tmp */
2903 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
2906 advise (NULL
, "unable to create temporary file");
2910 copy_message (msgnum
, out
);
2912 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
2916 switch (child_id
= fork ()) {
2918 advise ("fork", "unable to");
2924 SIGNAL (SIGINT
, istat
);
2925 SIGNAL (SIGQUIT
, qstat
);
2927 vec
[vecp
++] = tmpfil
;
2931 fprintf (stderr
, "unable to exec ");
2936 status
= pidXwait (child_id
, NULL
);
2947 copy_message (int msgnum
, FILE *out
)
2950 static char buffer
[BUFSIZ
];
2953 zp
= msh_ready (msgnum
, 1);
2955 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
) {
2956 fputs (buffer
, out
);
2957 if (interrupted
&& out
== stdout
)
2963 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2964 && pos
< Msgs
[msgnum
].m_stop
) {
2965 fputs (buffer
, out
);
2966 pos
+= (long) strlen (buffer
);
2967 if (interrupted
&& out
== stdout
)
2975 copy_digest (int msgnum
, FILE *out
)
2979 static char buffer
[BUFSIZ
];
2983 zp
= msh_ready (msgnum
, 1);
2986 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2987 && !fmsh
&& pos
< Msgs
[msgnum
].m_stop
) {
2988 if (c
== '\n' && *buffer
== '-')
2990 fputs (buffer
, out
);
2991 c
= buffer
[strlen (buffer
) - 1];
2993 pos
+= (long) strlen (buffer
);
2994 if (interrupted
&& out
== stdout
)