]>
diplodocus.org Git - nmh/blob - uip/mshcmds.c
dfa3bbeb623e91273be9ca913a3104fbb329fa2a
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>
19 #include <h/picksbr.h>
23 static char delim3
[] = "-------"; /* from burst.c */
29 * Type for a compare function for qsort. This keeps
32 typedef int (*qsort_comp
) (const void *, const void *);
37 static int burst (struct Msg
*, int, int, int, int);
38 static void forw (char *, char *, int, char **);
39 static void rmm (void);
40 static void show (int);
41 static int eom_action (int);
42 static FILE *mhl_action (char *);
44 static int is_nontext (int);
45 static int get_fields (char *, char *, int, struct Msg
*);
46 static int msgsort (struct Msg
*, struct Msg
*);
47 static int subsort (struct Msg
*, struct Msg
*);
48 static char *sosmash (char *, char *);
49 static int process (int, char *, int, char **);
50 static void copy_message (int, FILE *);
51 static void copy_digest (int, FILE *);
53 extern m_getfld_state_t gstate
; /* use the gstate in scansbr.c */
56 forkcmd (char **args
, char *pgm
)
61 vec
[0] = r1bindex (pgm
, '/');
62 copyip (args
, vec
+ 1, MAXARGS
- 1);
65 context_del (pfolder
);
66 context_replace (pfolder
, fmsh
);/* update current folder */
68 context_save (); /* save the context file */
71 switch (child_id
= fork ()) {
73 advise ("fork", "unable to");
78 SIGNAL (SIGINT
, istat
);
79 SIGNAL (SIGQUIT
, qstat
);
82 fprintf (stderr
, "unable to exec ");
87 pidXwait (child_id
, NULL
);
90 if (fmsh
) { /* assume the worst case */
91 mp
->msgflags
|= MODIFIED
;
97 #define DIST_SWITCHES \
98 X("annotate", 0, DIANSW) \
99 X("noannotate", 0, DINANSW) \
100 X("draftfolder +folder", 0, DIDFSW) \
101 X("draftmessage msg", 0, DIDMSW) \
102 X("nodraftfolder", 0, DINDFSW) \
103 X("editor editor", 0, DIEDTSW) \
104 X("noedit", 0, DINEDSW) \
105 X("form formfile", 0, DIFRMSW) \
106 X("inplace", 0, DIINSW) \
107 X("noinplace", 0, DININSW) \
108 X("whatnowproc program", 0, DIWHTSW) \
109 X("nowhatnowproc", 0, DINWTSW) \
110 X("help", 0, DIHELP) \
112 #define X(sw, minchars, id) id,
113 DEFINE_SWITCH_ENUM(DIST
);
116 #define X(sw, minchars, id) { sw, minchars, id },
117 DEFINE_SWITCH_ARRAY(DIST
, distswit
);
122 distcmd (char **args
)
125 char *cp
, *msg
= NULL
;
126 char buf
[BUFSIZ
], *vec
[MAXARGS
];
129 forkcmd (args
, cmd_name
);
133 while ((cp
= *args
++)) {
135 switch (smatch (++cp
, distswit
)) {
137 ambigsw (cp
, distswit
);
140 fprintf (stderr
, "-%s unknown\n", cp
);
143 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
144 print_help (buf
, distswit
, 1);
147 case DIANSW
: /* not implemented */
165 if (!(cp
= *args
++) || *cp
== '-') {
166 advise (NULL
, "missing argument to %s", args
[-2]);
172 if (*cp
== '+' || *cp
== '@') {
173 advise (NULL
, "sorry, no folders allowed!");
178 advise (NULL
, "only one message at a time!");
186 vec
[vecp
++] = "-file";
190 if (!m_convert (mp
, msg
))
194 if (mp
->numsel
> 1) {
195 advise (NULL
, "only one message at a time!");
198 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
199 seq_setcur (mp
, mp
->hghsel
);
203 #define EXPLODE_SWITCHES \
204 X("inplace", 0, EXINSW) \
205 X("noinplace", 0, EXNINSW) \
206 X("quiet", 0, EXQISW) \
207 X("noquiet", 0, EXNQISW) \
208 X("verbose", 0, EXVBSW) \
209 X("noverbose", 0, EXNVBSW) \
210 X("help", 0, EXHELP) \
212 #define X(sw, minchars, id) id,
213 DEFINE_SWITCH_ENUM(EXPLODE
);
216 #define X(sw, minchars, id) { sw, minchars, id },
217 DEFINE_SWITCH_ARRAY(EXPLODE
, explswit
);
222 explcmd (char **args
)
224 int inplace
= 0, quietsw
= 0, verbosw
= 0;
225 int msgp
= 0, hi
, msgnum
;
226 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
230 forkcmd (args
, cmd_name
);
234 while ((cp
= *args
++)) {
236 switch (smatch (++cp
, explswit
)) {
238 ambigsw (cp
, explswit
);
241 fprintf (stderr
, "-%s unknown\n", cp
);
244 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
245 print_help (buf
, explswit
, 1);
267 if (*cp
== '+' || *cp
== '@') {
268 advise (NULL
, "sorry, no folders allowed!");
276 msgs
[msgp
++] = "cur";
277 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
278 if (!m_convert (mp
, msgs
[msgnum
]))
282 smsgs
= (struct Msg
*)
283 calloc ((size_t) (MAXFOLDER
+ 2), sizeof *smsgs
);
285 adios (NULL
, "unable to allocate folder storage");
289 for (msgnum
= mp
->lowsel
;
290 msgnum
<= mp
->hghsel
&& !interrupted
;
292 if (is_selected (mp
, msgnum
))
293 if (burst (smsgs
, msgnum
, inplace
, quietsw
, verbosw
) != OK
)
296 free ((char *) smsgs
);
299 seq_setcur (mp
, mp
->lowsel
);
301 if (hi
<= mp
->hghmsg
)
304 mp
->msgflags
|= MODIFIED
;
310 burst (struct Msg
*smsgs
, int msgnum
, int inplace
, int quietsw
, int verbosw
)
312 int i
, j
, ld3
, wasdlm
, msgp
;
314 char c
, buffer
[BUFSIZ
];
317 ld3
= strlen (delim3
);
319 if (Msgs
[msgnum
].m_scanl
) {
320 free (Msgs
[msgnum
].m_scanl
);
321 Msgs
[msgnum
].m_scanl
= NULL
;
324 pos
= ftell (zp
= msh_ready (msgnum
, 1));
325 for (msgp
= 0; msgp
<= MAXFOLDER
;) {
326 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
328 && pos
< Msgs
[msgnum
].m_stop
)
329 pos
+= (long) strlen (buffer
);
330 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
)
332 fseek (zp
, pos
, SEEK_SET
);
333 smsgs
[msgp
].m_start
= pos
;
336 pos
< Msgs
[msgnum
].m_stop
337 && fgets (buffer
, sizeof buffer
, zp
) != NULL
;
339 if (strncmp (buffer
, delim3
, ld3
) == 0
340 && (msgp
== 1 || c
== '\n')
341 && peekc (zp
) == '\n')
344 pos
+= (long) strlen (buffer
);
346 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
347 if (smsgs
[msgp
].m_start
!= pos
)
348 smsgs
[msgp
++].m_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
349 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
) {
351 smsgs
[msgp
- 1].m_stop
-= ((long) strlen (buffer
) + 1);
354 pos
+= (long) strlen (buffer
);
357 switch (msgp
--) { /* toss "End of XXX Digest" */
359 adios (NULL
, "burst() botch -- you lose big");
363 printf ("message %d not in digest format\n", msgnum
);
368 printf ("%d message%s exploded from digest %d\n",
369 msgp
, msgp
!= 1 ? "s" : "", msgnum
);
373 if ((i
= msgp
+ mp
->hghmsg
) > MAXFOLDER
) {
374 advise (NULL
, "more than %d messages", MAXFOLDER
);
377 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, i
)))
378 adios (NULL
, "unable to allocate folder storage");
383 if (mp
->hghsel
> msgnum
)
387 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
389 printf ("message %d becomes message %d\n", j
, i
);
391 Msgs
[i
].m_bboard_id
= Msgs
[j
].m_bboard_id
;
392 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
393 Msgs
[i
].m_start
= Msgs
[j
].m_start
;
394 Msgs
[i
].m_stop
= Msgs
[j
].m_stop
;
395 Msgs
[i
].m_scanl
= NULL
;
396 if (Msgs
[j
].m_scanl
) {
397 free (Msgs
[j
].m_scanl
);
398 Msgs
[j
].m_scanl
= NULL
;
400 copy_msg_flags (mp
, i
, j
);
403 if (Msgs
[msgnum
].m_bboard_id
== 0)
406 unset_selected (mp
, msgnum
);
407 i
= inplace
? msgnum
+ msgp
: mp
->hghmsg
;
408 for (j
= msgp
; j
>= (inplace
? 0 : 1); i
--, j
--) {
409 if (verbosw
&& i
!= msgnum
)
410 printf ("message %d of digest %d becomes message %d\n",
413 Msgs
[i
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
414 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
415 Msgs
[i
].m_start
= smsgs
[j
].m_start
;
416 Msgs
[i
].m_stop
= smsgs
[j
].m_stop
;
417 Msgs
[i
].m_scanl
= NULL
;
418 copy_msg_flags (mp
, i
, msgnum
);
425 #define FILE_SWITCHES \
426 X("draft", 0, FIDRFT) \
427 X("link", 0, FILINK) \
428 X("nolink", 0, FINLINK) \
429 X("preserve", 0, FIPRES) \
430 X("nopreserve", 0, FINPRES) \
431 X("src +folder", 0, FISRC) \
432 X("file file", 0, FIFILE) \
433 X("rmmproc program", 0, FIPROC) \
434 X("normmproc", 0, FINPRC) \
435 X("help", 0, FIHELP) \
437 #define X(sw, minchars, id) id,
438 DEFINE_SWITCH_ENUM(FILE);
441 #define X(sw, minchars, id) { sw, minchars, id },
442 DEFINE_SWITCH_ARRAY(FILE, fileswit
);
447 filecmd (char **args
)
449 int linksw
= 0, msgp
= 0;
450 int vecp
= 1, i
, msgnum
;
451 char *cp
, buf
[BUFSIZ
];
452 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
455 forkcmd (args
, cmd_name
);
459 while ((cp
= *args
++)) {
461 switch (i
= smatch (++cp
, fileswit
)) {
463 ambigsw (cp
, fileswit
);
466 fprintf (stderr
, "-%s unknown\n", cp
);
469 snprintf (buf
, sizeof(buf
), "%s +folder... [msgs] [switches]", cmd_name
);
470 print_help (buf
, fileswit
, 1);
489 advise (NULL
, "sorry, -%s not allowed!", fileswit
[i
].sw
);
492 if (*cp
== '+' || *cp
== '@')
499 vec
[vecp
++] = "-file";
502 msgs
[msgp
++] = "cur";
503 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
504 if (!m_convert (mp
, msgs
[msgnum
]))
509 for (msgnum
= mp
->lowsel
;
510 msgnum
<= mp
->hghsel
&& !interrupted
;
512 if (is_selected (mp
, msgnum
))
513 if (process (msgnum
, fileproc
, vecp
, vec
)) {
514 unset_selected (mp
, msgnum
);
518 if (mp
->numsel
!= mp
->nummsg
|| linksw
)
519 seq_setcur (mp
, mp
->hghsel
);
526 filehak (char **args
)
528 int result
, vecp
= 0;
529 char *cp
, *cwd
, *vec
[MAXARGS
];
531 while ((cp
= *args
++)) {
533 switch (smatch (++cp
, fileswit
)) {
550 if (*cp
== '+' || *cp
== '@')
557 for (vecp
= 0; (cp
= vec
[vecp
]) && result
== NOTOK
; vecp
++) {
559 cwd
= getcpy (pwd ());
560 chdir (m_maildir (""));
562 if (access (m_maildir (cp
), F_OK
) == NOTOK
)
573 #define FOLDER_SWITCHES \
574 X("all", 0, FLALSW) \
575 X("fast", 0, FLFASW) \
576 X("nofast", 0, FLNFASW) \
577 X("header", 0, FLHDSW) \
578 X("noheader", 0, FLNHDSW) \
579 X("pack", 0, FLPKSW) \
580 X("nopack", 0, FLNPKSW) \
581 X("recurse", 0, FLRCSW) \
582 X("norecurse", 0, FLNRCSW) \
583 X("total", 0, FLTLSW) \
584 X("nototal", 0, FLNTLSW) \
585 X("print", 0, FLPRSW) \
586 X("push", 0, FLPUSW) \
587 X("pop", 0, FLPOSW) \
588 X("list", 0, FLLISW) \
589 X("help", 0, FLHELP) \
591 #define X(sw, minchars, id) id,
592 DEFINE_SWITCH_ENUM(FOLDER
);
595 #define X(sw, minchars, id) { sw, minchars, id },
596 DEFINE_SWITCH_ARRAY(FOLDER
, foldswit
);
601 foldcmd (char **args
)
603 int fastsw
= 0, headersw
= 0, packsw
= 0;
605 char *cp
, *folder
= NULL
, *msg
= NULL
;
606 char buf
[BUFSIZ
], **vec
= args
;
611 while ((cp
= *args
++)) {
613 switch (smatch (++cp
, foldswit
)) {
615 ambigsw (cp
, foldswit
);
618 fprintf (stderr
, "-%s unknown\n", cp
);
621 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]", cmd_name
);
622 print_help (buf
, foldswit
, 1);
625 case FLALSW
: /* not implemented */
655 if (*cp
== '+' || *cp
== '@') {
657 advise (NULL
, "only one folder at a time!\n");
661 folder
= fmsh
? pluspath (cp
)
666 advise (NULL
, "only one message at a time!\n");
675 advise (NULL
, "null folder names are not permitted");
679 if (access (m_maildir (folder
), R_OK
) == NOTOK
) {
680 advise (folder
, "unable to read");
685 strncpy (buf
, folder
, sizeof(buf
));
686 if (expand (buf
) == NOTOK
)
689 if (access (folder
, R_OK
) == NOTOK
) {
690 advise (folder
, "unable to read");
705 if (!m_convert (mp
, msg
))
709 if (mp
->numsel
> 1) {
710 advise (NULL
, "only one message at a time!");
713 seq_setcur (mp
, mp
->hghsel
);
718 forkcmd (vec
, cmd_name
);
722 if (mp
->lowoff
> 1 && !(mp
= folder_realloc (mp
, 1, mp
->hghmsg
)))
723 adios (NULL
, "unable to allocate folder storage");
725 for (msgnum
= mp
->lowmsg
, hole
= 1; msgnum
<= mp
->hghmsg
; msgnum
++)
726 if (does_exist (mp
, msgnum
)) {
727 if (msgnum
!= hole
) {
728 Msgs
[hole
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
729 Msgs
[hole
].m_top
= Msgs
[msgnum
].m_top
;
730 Msgs
[hole
].m_start
= Msgs
[msgnum
].m_start
;
731 Msgs
[hole
].m_stop
= Msgs
[msgnum
].m_stop
;
732 Msgs
[hole
].m_scanl
= NULL
;
733 if (Msgs
[msgnum
].m_scanl
) {
734 free (Msgs
[msgnum
].m_scanl
);
735 Msgs
[msgnum
].m_scanl
= NULL
;
737 copy_msg_flags (mp
, hole
, msgnum
);
738 if (mp
->curmsg
== msgnum
)
739 seq_setcur (mp
, hole
);
743 if (mp
->nummsg
> 0) {
745 mp
->hghmsg
= hole
- 1;
747 mp
->msgflags
|= MODIFIED
;
753 printf ("%s\n", fmsh
? fmsh
: mp
->foldpath
);
756 printf ("\t\tFolder %*s# of messages (%*srange%*s); cur%*smsg\n",
757 DMAXFOLDER
, "", DMAXFOLDER
- 2, "", DMAXFOLDER
- 2, "",
759 printf (args
? "%22s " : "%s ", fmsh
? fmsh
: mp
->foldpath
);
761 /* check for empty folder */
762 if (mp
->nummsg
== 0) {
763 printf ("has no messages%*s",
764 mp
->msgflags
& OTHERS
? DMAXFOLDER
* 2 + 4 : 0, "");
766 printf ("has %*d message%s (%*d-%*d)",
767 DMAXFOLDER
, mp
->nummsg
, mp
->nummsg
!= 1 ? "s" : "",
768 DMAXFOLDER
, mp
->lowmsg
, DMAXFOLDER
, mp
->hghmsg
);
769 if (mp
->curmsg
>= mp
->lowmsg
770 && mp
->curmsg
<= mp
->hghmsg
)
771 printf ("; cur=%*d", DMAXFOLDER
, mp
->curmsg
);
778 #define FORW_SWITCHES \
779 X("annotate", 0, FOANSW) \
780 X("noannotate", 0, FONANSW) \
781 X("draftfolder +folder", 0, FODFSW) \
782 X("draftmessage msg", 0, FODMSW) \
783 X("nodraftfolder", 0, FONDFSW) \
784 X("editor editor", 0, FOEDTSW) \
785 X("noedit", 0, FONEDSW) \
786 X("filter filterfile", 0, FOFTRSW) \
787 X("form formfile", 0, FOFRMSW) \
788 X("format", 5, FOFTSW) \
789 X("noformat", 7, FONFTSW) \
790 X("inplace", 0, FOINSW) \
791 X("noinplace", 0, FONINSW) \
792 X("mime", 0, FOMISW) \
793 X("nomime", 0, FONMISW) \
794 X("whatnowproc program", 0, FOWHTSW) \
795 X("nowhatnow", 0, FONWTSW) \
796 X("help", 0, FOHELP) \
798 #define X(sw, minchars, id) id,
799 DEFINE_SWITCH_ENUM(FORW
);
802 #define X(sw, minchars, id) { sw, minchars, id },
803 DEFINE_SWITCH_ARRAY(FORW
, forwswit
);
808 forwcmd (char **args
)
810 int msgp
= 0, vecp
= 1, msgnum
;
811 char *cp
, *filter
= NULL
, buf
[BUFSIZ
];
812 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
817 forkcmd (args
, cmd_name
);
821 while ((cp
= *args
++)) {
823 switch (smatch (++cp
, forwswit
)) {
825 ambigsw (cp
, forwswit
);
828 fprintf (stderr
, "-%s unknown\n", cp
);
831 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
832 print_help (buf
, forwswit
, 1);
835 case FOANSW
: /* not implemented */
855 if (!(cp
= *args
++) || *cp
== '-') {
856 advise (NULL
, "missing argument to %s", args
[-2]);
862 if (!(filter
= *args
++) || *filter
== '-') {
863 advise (NULL
, "missing argument to %s", args
[-2]);
868 if (access (filter
= myfilter
, R_OK
) == NOTOK
) {
869 advise (filter
, "unable to read default filter file");
877 if (*cp
== '+' || *cp
== '@') {
878 advise (NULL
, "sorry, no folders allowed!");
885 /* foil search of .mh_profile */
886 snprintf (buf
, sizeof(buf
), "%sXXXXXX", invo_name
);
888 tfile
= m_mktemp(buf
, NULL
, NULL
);
889 if (tfile
== NULL
) adios("forwcmd", "unable to create temporary file");
890 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
893 vec
[vecp
++] = "-file";
896 msgs
[msgp
++] = "cur";
897 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
898 if (!m_convert (mp
, msgs
[msgnum
]))
903 strncpy (buf
, filter
, sizeof(buf
));
904 if (expand (buf
) == NOTOK
)
906 if (access (filter
= getcpy (etcpath (buf
)), R_OK
) == NOTOK
) {
907 advise (filter
, "unable to read");
912 forw (cmd_name
, filter
, vecp
, vec
);
913 seq_setcur (mp
, mp
->hghsel
);
920 forw (char *proc
, char *filter
, int vecp
, char **vec
)
922 int i
, child_id
, msgnum
, msgcnt
;
923 char tmpfil
[BUFSIZ
], *args
[MAXARGS
];
927 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
928 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
929 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
933 switch (child_id
= fork ()) {
935 advise ("fork", "unable to");
938 case OK
: /* "trust me" */
939 if (freopen (tmpfil
, "w", stdout
) == NULL
) {
940 fprintf (stderr
, "unable to create ");
944 args
[0] = r1bindex (mhlproc
, '/');
946 args
[i
++] = "-forwall";
949 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
950 if (is_selected (mp
, msgnum
))
951 args
[i
++] = getcpy (m_name (msgnum
));
953 mhlsbr (i
, args
, mhl_action
);
954 scan_eom_action ((int (*) ()) 0);
959 if (pidXwait (child_id
, NULL
))
964 if ((out
= fopen (tmpfil
, "w")) == NULL
) {
965 advise (tmpfil
, "unable to create temporary file");
970 for (msgnum
= mp
->lowsel
;
971 msgnum
<= mp
->hghsel
&& !interrupted
;
973 if (is_selected (mp
, msgnum
)) {
974 fprintf (out
, "\n\n-------");
975 if (msgnum
== mp
->lowsel
)
976 fprintf (out
, " Forwarded Message%s",
977 mp
->numsel
> 1 ? "s" : "");
979 fprintf (out
, " Message %d", msgcnt
);
980 fprintf (out
, "\n\n");
981 copy_digest (msgnum
, out
);
985 fprintf (out
, "\n\n------- End of Forwarded Message%s\n",
986 mp
->numsel
> 1 ? "s" : "");
992 switch (child_id
= fork ()) {
994 advise ("fork", "unable to");
999 SIGNAL (SIGINT
, istat
);
1000 SIGNAL (SIGQUIT
, qstat
);
1002 vec
[vecp
++] = tmpfil
;
1006 fprintf (stderr
, "unable to exec ");
1011 pidXwait (child_id
, NULL
);
1019 static char *hlpmsg
[] = {
1020 "The %s program emulates many of the commands found in the nmh",
1021 "system. Instead of operating on nmh folders, commands to %s concern",
1024 "To see the list of commands available, just type a ``?'' followed by",
1025 "the RETURN key. To find out what switches each command takes, type",
1026 "the name of the command followed by ``-help''. To leave %s, use the",
1027 "``quit'' command.",
1029 "Although a lot of nmh commands are found in %s, not all are fully",
1030 "implemented. %s will always recognize all legal switches for a",
1031 "given command though, and will let you know when you ask for an",
1032 "option that it is unable to perform.",
1034 "Running %s is fun, but using nmh from your shell is far superior.",
1035 "After you have familiarized yourself with the nmh style by using %s,",
1036 "you should try using nmh from the shell. You can still use %s for",
1037 "message files that aren't in nmh format, such as BBoard files.",
1043 helpcmd (char **args
)
1048 for (i
= 0; hlpmsg
[i
]; i
++) {
1049 printf (hlpmsg
[i
], invo_name
);
1055 #define MARK_SWITCHES \
1056 X("add", 0, MADDSW) \
1057 X("delete", 0, MDELSW) \
1058 X("list", 0, MLSTSW) \
1059 X("sequence name", 0, MSEQSW) \
1060 X("public", 0, MPUBSW) \
1061 X("nopublic", 0, MNPUBSW) \
1062 X("zero", 0, MZERSW) \
1063 X("nozero", 0, MNZERSW) \
1064 X("help", 0, MHELP) \
1065 X("debug", -5, MDBUGSW) \
1067 #define X(sw, minchars, id) id,
1068 DEFINE_SWITCH_ENUM(MARK
);
1071 #define X(sw, minchars, id) { sw, minchars, id },
1072 DEFINE_SWITCH_ARRAY(MARK
, markswit
);
1077 markcmd (char **args
)
1079 int addsw
= 0, deletesw
= 0, debugsw
= 0;
1080 int listsw
= 0, zerosw
= 0;
1082 int msgp
= 0, msgnum
;
1083 char *cp
, buf
[BUFSIZ
];
1084 char *seqs
[NUMATTRS
+ 1], *msgs
[MAXARGS
];
1086 while ((cp
= *args
++)) {
1088 switch (smatch (++cp
, markswit
)) {
1090 ambigsw (cp
, markswit
);
1093 fprintf (stderr
, "-%s unknown\n", cp
);
1096 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1097 print_help (buf
, markswit
, 1);
1102 deletesw
= listsw
= 0;
1110 addsw
= deletesw
= 0;
1114 if (!(cp
= *args
++) || *cp
== '-') {
1115 advise (NULL
, "missing argument to %s", args
[-2]);
1118 if (seqp
< NUMATTRS
)
1121 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1126 case MPUBSW
: /* not implemented */
1142 if (*cp
== '+' || *cp
== '@') {
1143 advise (NULL
, "sorry, no folders allowed!");
1150 if (!addsw
&& !deletesw
&& !listsw
) {
1157 seqs
[seqp
++] = "unseen";
1161 msgs
[msgp
++] = "all";
1166 msgs
[msgp
++] = listsw
? "all" :"cur";
1167 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1168 if (!m_convert (mp
, msgs
[msgnum
]))
1172 printf ("invo_name=%s mypath=%s defpath=%s\n",
1173 invo_name
, mypath
, defpath
);
1174 printf ("ctxpath=%s context flags=%s\n",
1175 ctxpath
, snprintb (buf
, sizeof(buf
), (unsigned) ctxflags
, DBITS
));
1176 printf ("foldpath=%s flags=%s\n",
1178 snprintb (buf
, sizeof(buf
), (unsigned) mp
->msgflags
, FBITS
));
1179 printf ("hghmsg=%d lowmsg=%d nummsg=%d curmsg=%d\n",
1180 mp
->hghmsg
, mp
->lowmsg
, mp
->nummsg
, mp
->curmsg
);
1181 printf ("lowsel=%d hghsel=%d numsel=%d\n",
1182 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
1183 printf ("lowoff=%d hghoff=%d\n", mp
->lowoff
, mp
->hghoff
);
1186 if (seqp
== 0 && (addsw
|| deletesw
)) {
1187 advise (NULL
, "-%s requires at least one -sequence argument",
1188 addsw
? "add" : "delete");
1194 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1195 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1200 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1201 if (!seq_delsel (mp
, seqs
[seqp
], 0, zerosw
))
1205 /* Listing messages in sequences */
1208 /* list the given sequences */
1209 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1210 seq_print (mp
, seqs
[seqp
]);
1212 /* else list them all */
1218 for (msgnum
= mp
->lowsel
;
1219 msgnum
<= mp
->hghsel
&& !interrupted
;
1221 if (is_selected (mp
, msgnum
)) {
1222 printf ("%*d: id=%d top=%d start=%ld stop=%ld %s\n",
1225 Msgs
[msgnum
].m_bboard_id
,
1227 (long) Msgs
[msgnum
].m_start
,
1228 (long) Msgs
[msgnum
].m_stop
,
1229 snprintb (buf
, sizeof(buf
),
1230 (unsigned) *bvector_bits (msgstat (mp
,
1233 if (Msgs
[msgnum
].m_scanl
)
1234 printf ("%s", Msgs
[msgnum
].m_scanl
);
1240 #define MHN_SWITCHES \
1241 X("auto", 0, MHNAUTOSW) \
1242 X("noauto", 0, MHNNAUTOSW) \
1243 X("debug", -5, MHNDEBUGSW) \
1244 X("form formfile", 4, MHNFORMSW) \
1245 X("headers", 0, MHNHEADSW) \
1246 X("noheaders", 0, MHNNHEADSW) \
1247 X("list", 0, MHNLISTSW) \
1248 X("nolist", 0, MHNNLISTSW) \
1249 X("part number", 0, MHNPARTSW) \
1250 X("realsize", 0, MHNSIZESW) \
1251 X("norealsize", 0, MHNNSIZESW) \
1252 X("rfc934mode", 0, MHNRFC934SW) \
1253 X("norfc934mode", 0, MHNNRFC934SW) \
1254 X("serialonly", 0, MHNSERIALSW) \
1255 X("noserialonly", 0, MHNNSERIALSW) \
1256 X("show", 0, MHNSHOWSW) \
1257 X("noshow", 0, MHNNSHOWSW) \
1258 X("store", 0, MHNSTORESW) \
1259 X("nostore", 0, MHNNSTORESW) \
1260 X("type content", 0, MHNTYPESW) \
1261 X("verbose", 0, MHNVERBSW) \
1262 X("noverbose", 0, MHNNVERBSW) \
1263 X("help", 0, MHNHELPSW) \
1264 X("moreproc program", -4, MHNPROGSW) \
1265 X("nomoreproc", -3, MHNNPROGSW) \
1266 X("length lines", -4, MHNLENSW) \
1267 X("width columns", -4, MHNWIDSW) \
1269 #define X(sw, minchars, id) id,
1270 DEFINE_SWITCH_ENUM(MHN
);
1273 #define X(sw, minchars, id) { sw, minchars, id },
1274 DEFINE_SWITCH_ARRAY(MHN
, mhnswit
);
1279 mhncmd (char **args
)
1281 int msgp
= 0, vecp
= 1;
1283 char *cp
, buf
[BUFSIZ
];
1284 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
1287 forkcmd (args
, cmd_name
);
1290 while ((cp
= *args
++)) {
1292 switch (smatch (++cp
, mhnswit
)) {
1294 ambigsw (cp
, mhnswit
);
1297 fprintf (stderr
, "-%s unknown\n", cp
);
1300 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1301 print_help (buf
, mhnswit
, 1);
1334 if (!(cp
= *args
++) || *cp
== '-') {
1335 advise (NULL
, "missing argument to %s", args
[-2]);
1342 if (*cp
== '+' || *cp
== '@') {
1343 advise (NULL
, "sorry, no folders allowed!");
1351 vec
[vecp
++] = "-file";
1354 msgs
[msgp
++] = "cur";
1355 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1356 if (!m_convert (mp
, msgs
[msgnum
]))
1361 for (msgnum
= mp
->lowsel
;
1362 msgnum
<= mp
->hghsel
&& !interrupted
;
1364 if (is_selected (mp
, msgnum
))
1365 if (process (msgnum
, cmd_name
, vecp
, vec
)) {
1366 unset_selected (mp
, msgnum
);
1370 seq_setcur (mp
, mp
->hghsel
);
1374 #define PACK_SWITCHES \
1375 X("file name", 0, PAFISW) \
1376 X("help", 0, PAHELP) \
1378 #define X(sw, minchars, id) id,
1379 DEFINE_SWITCH_ENUM(PACK
);
1382 #define X(sw, minchars, id) { sw, minchars, id },
1383 DEFINE_SWITCH_ARRAY(PACK
, packswit
);
1386 static int mbx_style
= MMDF_FORMAT
;
1389 packcmd (char **args
)
1391 int msgp
= 0, md
, msgnum
;
1392 char *cp
, *file
= NULL
;
1393 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
1397 forkcmd (args
, cmd_name
);
1401 while ((cp
= *args
++)) {
1403 switch (smatch (++cp
, packswit
)) {
1405 ambigsw (cp
, packswit
);
1408 fprintf (stderr
, "-%s unknown\n", cp
);
1411 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1412 print_help (buf
, packswit
, 1);
1416 if (!(file
= *args
++) || *file
== '-') {
1417 advise (NULL
, "missing argument to %s", args
[-2]);
1422 if (*cp
== '+' || *cp
== '@') {
1423 advise (NULL
, "sorry, no folders allowed!");
1432 file
= path (file
, TFILE
);
1433 if (stat (file
, &st
) == NOTOK
) {
1434 if (errno
!= ENOENT
) {
1435 advise (file
, "error on file");
1438 md
= getanswer (cp
= concat ("Create file \"", file
, "\"? ", NULL
));
1445 msgs
[msgp
++] = "all";
1446 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1447 if (!m_convert (mp
, msgs
[msgnum
]))
1451 if ((md
= mbx_open (file
, mbx_style
, getuid (), getgid (), m_gmprot ())) == NOTOK
) {
1452 advise (file
, "unable to open");
1455 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1456 if (is_selected (mp
, msgnum
))
1457 if (pack (file
, md
, msgnum
) == NOTOK
)
1459 mbx_close (file
, md
);
1461 if (mp
->hghsel
!= mp
->curmsg
)
1462 seq_setcur (mp
, mp
->lowsel
);
1470 pack (char *mailbox
, int md
, int msgnum
)
1474 if (Msgs
[msgnum
].m_bboard_id
== 0)
1477 zp
= msh_ready (msgnum
, 1);
1478 return mbx_write (mailbox
, md
, zp
, Msgs
[msgnum
].m_bboard_id
,
1479 0L, ftell (zp
), Msgs
[msgnum
].m_stop
, 1, 1);
1484 packhak (char **args
)
1487 char *cp
, *file
= NULL
;
1489 while ((cp
= *args
++)) {
1491 switch (smatch (++cp
, packswit
)) {
1498 if (!(file
= *args
++) || *file
== '-')
1502 if (*cp
== '+' || *cp
== '@')
1506 file
= path (file
? file
: "./msgbox", TFILE
);
1507 result
= access (file
, F_OK
) == NOTOK
? OK
: NOTOK
;
1514 #define PICK_SWITCHES \
1515 X("and", 0, PIANSW) \
1516 X("or", 0, PIORSW) \
1517 X("not", 0, PINTSW) \
1518 X("lbrace", 0, PILBSW) \
1519 X("rbrace", 0, PIRBSW) \
1520 X("cc pattern", 0, PICCSW) \
1521 X("date pattern", 0, PIDASW) \
1522 X("from pattern", 0, PIFRSW) \
1523 X("search pattern", 0, PISESW) \
1524 X("subject pattern", 0, PISUSW) \
1525 X("to pattern", 0, PITOSW) \
1526 X("-othercomponent pattern", 15, PIOTSW) \
1527 X("after date", 0, PIAFSW) \
1528 X("before date", 0, PIBFSW) \
1529 X("datefield field", 5, PIDFSW) \
1530 X("sequence name", 0, PISQSW) \
1531 X("public", 0, PIPUSW) \
1532 X("nopublic", 0, PINPUSW) \
1533 X("zero", 0, PIZRSW) \
1534 X("nozero", 0, PINZRSW) \
1535 X("list", 0, PILISW) \
1536 X("nolist", 0, PINLISW) \
1537 X("help", 0, PIHELP) \
1539 #define X(sw, minchars, id) id,
1540 DEFINE_SWITCH_ENUM(PICK
);
1543 #define X(sw, minchars, id) { sw, minchars, id },
1544 DEFINE_SWITCH_ARRAY(PICK
, pickswit
);
1549 pickcmd (char **args
)
1551 int zerosw
= 1, msgp
= 0;
1553 int vecp
= 0, hi
, lo
, msgnum
;
1554 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1555 char *seqs
[NUMATTRS
], *vec
[MAXARGS
];
1558 while ((cp
= *args
++)) {
1564 switch (smatch (cp
, pickswit
)) {
1566 ambigsw (cp
, pickswit
);
1569 fprintf (stderr
, "-%s unknown\n", cp
);
1572 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1573 print_help (buf
, pickswit
, 1);
1587 if (!(cp
= *args
++)) {/* allow -xyz arguments */
1588 advise (NULL
, "missing argument to %s", args
[-2]);
1594 advise (NULL
, "internal error!");
1605 if (!(cp
= *args
++) || *cp
== '-') {
1606 advise (NULL
, "missing argument to %s", args
[-2]);
1609 if (seqp
< NUMATTRS
)
1612 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1623 case PIPUSW
: /* not implemented */
1630 if (*cp
== '+' || *cp
== '@') {
1631 advise (NULL
, "sorry, no folders allowed!");
1640 msgs
[msgp
++] = "all";
1641 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1642 if (!m_convert (mp
, msgs
[msgnum
]))
1647 if (!pcompile (vec
, NULL
))
1653 for (msgnum
= mp
->lowsel
;
1654 msgnum
<= mp
->hghsel
&& !interrupted
;
1656 if (is_selected (mp
, msgnum
)) {
1657 zp
= msh_ready (msgnum
, 1);
1658 if (pmatches (zp
, msgnum
, fmsh
? 0L : Msgs
[msgnum
].m_start
,
1659 fmsh
? 0L : Msgs
[msgnum
].m_stop
)) {
1666 unset_selected (mp
, msgnum
);
1677 if (mp
->numsel
<= 0) {
1678 advise (NULL
, "no messages match specification");
1683 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1684 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1687 printf ("%d hit%s\n", mp
->numsel
, mp
->numsel
== 1 ? "" : "s");
1691 #define REPL_SWITCHES \
1692 X("annotate", 0, REANSW) \
1693 X("noannotate", 0, RENANSW) \
1694 X("cc type", 0, RECCSW) \
1695 X("nocc type", 0, RENCCSW) \
1696 X("draftfolder +folder", 0, REDFSW) \
1697 X("draftmessage msg", 0, REDMSW) \
1698 X("nodraftfolder", 0, RENDFSW) \
1699 X("editor editor", 0, REEDTSW) \
1700 X("noedit", 0, RENEDSW) \
1701 X("fcc +folder", 0, REFCCSW) \
1702 X("filter filterfile", 0, REFLTSW) \
1703 X("form formfile", 0, REFRMSW) \
1704 X("inplace", 0, REINSW) \
1705 X("noinplace", 0, RENINSW) \
1706 X("query", 0, REQUSW) \
1707 X("noquery", 0, RENQUSW) \
1708 X("whatnowproc program", 0, REWHTSW) \
1709 X("nowhatnow", 0, RENWTSW) \
1710 X("width columns", 0, REWIDSW) \
1711 X("help", 0, REHELP) \
1713 #define X(sw, minchars, id) id,
1714 DEFINE_SWITCH_ENUM(REPL
);
1717 #define X(sw, minchars, id) { sw, minchars, id },
1718 DEFINE_SWITCH_ARRAY(REPL
, replswit
);
1723 replcmd (char **args
)
1726 char *cp
, *msg
= NULL
;
1727 char buf
[BUFSIZ
], *vec
[MAXARGS
];
1730 forkcmd (args
, cmd_name
);
1734 while ((cp
= *args
++)) {
1736 switch (smatch (++cp
, replswit
)) {
1738 ambigsw (cp
, replswit
);
1741 fprintf (stderr
, "-%s unknown\n", cp
);
1744 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1745 print_help (buf
, replswit
, 1);
1748 case REANSW
: /* not implemented */
1773 if (!(cp
= *args
++) || *cp
== '-') {
1774 advise (NULL
, "missing argument to %s", args
[-2]);
1780 if (*cp
== '+' || *cp
== '@') {
1781 advise (NULL
, "sorry, no folders allowed!");
1786 advise (NULL
, "only one message at a time!");
1794 vec
[vecp
++] = "-file";
1798 if (!m_convert (mp
, msg
))
1802 if (mp
->numsel
> 1) {
1803 advise (NULL
, "only one message at a time!");
1806 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
1807 seq_setcur (mp
, mp
->hghsel
);
1811 #define RMM_SWITCHES \
1812 X("help", 0, RMHELP) \
1814 #define X(sw, minchars, id) id,
1815 DEFINE_SWITCH_ENUM(RMM
);
1818 #define X(sw, minchars, id) { sw, minchars, id },
1819 DEFINE_SWITCH_ARRAY(RMM
, rmmswit
);
1824 rmmcmd (char **args
)
1826 int msgp
= 0, msgnum
;
1827 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1829 while ((cp
= *args
++)) {
1831 switch (smatch (++cp
, rmmswit
)) {
1833 ambigsw (cp
, rmmswit
);
1836 fprintf (stderr
, "-%s unknown\n", cp
);
1839 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1840 print_help (buf
, rmmswit
, 1);
1843 if (*cp
== '+' || *cp
== '@') {
1844 advise (NULL
, "sorry, no folders allowed!");
1852 msgs
[msgp
++] = "cur";
1853 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1854 if (!m_convert (mp
, msgs
[msgnum
]))
1865 register int msgnum
, vecp
;
1867 char buffer
[BUFSIZ
], *vec
[MAXARGS
];
1871 if (mp
->numsel
> MAXARGS
- 1) {
1872 advise (NULL
, "more than %d messages for %s exec",
1873 MAXARGS
- 1, rmmproc
);
1877 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1878 if (is_selected (mp
, msgnum
))
1879 vec
[vecp
++] = getcpy (m_name (msgnum
));
1881 forkcmd (vec
, rmmproc
);
1882 for (vecp
= 0; vec
[vecp
]; vecp
++)
1886 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1887 if (is_selected (mp
, msgnum
)) {
1888 strncpy (buffer
, m_backup (cp
= m_name (msgnum
)), sizeof(buffer
));
1889 if (rename (cp
, buffer
) == NOTOK
)
1890 admonish (buffer
, "unable to rename %s to", cp
);
1894 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1895 if (is_selected (mp
, msgnum
)) {
1896 set_deleted (mp
, msgnum
);
1897 unset_exists (mp
, msgnum
);
1900 if ((mp
->nummsg
-= mp
->numsel
) <= 0) {
1902 admonish (NULL
, "no messages remaining in +%s", fmsh
);
1904 admonish (NULL
, "no messages remaining in %s", mp
->foldpath
);
1905 mp
->lowmsg
= mp
->hghmsg
= mp
->nummsg
= 0;
1907 if (mp
->lowsel
== mp
->lowmsg
) {
1908 for (msgnum
= mp
->lowmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1909 if (does_exist (mp
, msgnum
))
1911 mp
->lowmsg
= msgnum
;
1913 if (mp
->hghsel
== mp
->hghmsg
) {
1914 for (msgnum
= mp
->hghmsg
- 1; msgnum
>= mp
->lowmsg
; msgnum
--)
1915 if (does_exist (mp
, msgnum
))
1917 mp
->hghmsg
= msgnum
;
1920 mp
->msgflags
|= MODIFIED
;
1925 #define SCAN_SWITCHES \
1926 X("clear", 0, SCCLR) \
1927 X("noclear", 0, SCNCLR) \
1928 X("form formatfile", 0, SCFORM) \
1929 X("format string", 5, SCFMT) \
1930 X("header", 0, SCHEAD) \
1931 X("noheader", 0, SCNHEAD) \
1932 X("width columns", 0, SCWID) \
1933 X("help", 0, SCHELP) \
1935 #define X(sw, minchars, id) id,
1936 DEFINE_SWITCH_ENUM(SCAN
);
1939 #define X(sw, minchars, id) { sw, minchars, id },
1940 DEFINE_SWITCH_ARRAY(SCAN
, scanswit
);
1945 scancmd (char **args
)
1947 #define equiv(a,b) (a ? b && !strcmp (a, b) : !b)
1949 int clearsw
= 0, headersw
= 0, width
= 0, msgp
= 0;
1950 int msgnum
, optim
, state
;
1951 char *cp
, *form
= NULL
, *format
= NULL
;
1952 char buf
[BUFSIZ
], *nfs
, *msgs
[MAXARGS
];
1954 static int s_optim
= 0;
1955 static char *s_form
= NULL
, *s_format
= NULL
;
1957 while ((cp
= *args
++)) {
1959 switch (smatch (++cp
, scanswit
)) {
1961 ambigsw (cp
, scanswit
);
1964 fprintf (stderr
, "-%s unknown\n", cp
);
1967 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1968 print_help (buf
, scanswit
, 1);
1984 if (!(form
= *args
++) || *form
== '-') {
1985 advise (NULL
, "missing argument to %s", args
[-2]);
1991 if (!(format
= *args
++) || *format
== '-') {
1992 advise (NULL
, "missing argument to %s", args
[-2]);
1998 if (!(cp
= *args
++) || *cp
== '-') {
1999 advise (NULL
, "missing argument to %s", args
[-2]);
2005 if (*cp
== '+' || *cp
== '@') {
2006 advise (NULL
, "sorry, no folders allowed!");
2014 msgs
[msgp
++] = "all";
2015 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2016 if (!m_convert (mp
, msgs
[msgnum
]))
2020 /* Get new format string */
2021 nfs
= new_fs (form
, format
, FORMAT
);
2023 /* force scansbr to (re)compile format */
2030 s_optim
= optim
= 1;
2031 s_form
= form
? getcpy (form
) : NULL
;
2032 s_format
= format
? getcpy (format
) : NULL
;
2036 optim
= equiv (s_form
, form
) && equiv (s_format
, format
);
2039 for (msgnum
= mp
->lowsel
;
2040 msgnum
<= mp
->hghsel
&& !interrupted
;
2042 if (is_selected (mp
, msgnum
)) {
2043 if (optim
&& Msgs
[msgnum
].m_scanl
)
2044 printf ("%s", Msgs
[msgnum
].m_scanl
);
2047 zp
= msh_ready (msgnum
, 0);
2048 switch (state
= scan (zp
, msgnum
, 0, nfs
, width
,
2049 msgnum
== mp
->curmsg
,
2050 is_unseen (mp
, msgnum
),
2051 headersw
? (fmsh
? fmsh
: mp
->foldpath
) : NULL
,
2052 fmsh
? 0L : (long) (Msgs
[msgnum
].m_stop
- Msgs
[msgnum
].m_start
),
2058 Msgs
[msgnum
].m_scanl
= getcpy (scanl
);
2062 advise (NULL
, "scan() botch (%d)", state
);
2066 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
2078 #define SHOW_SWITCHES \
2079 X("draft", 5, SHDRAFT) \
2080 X("form formfile", 4, SHFORM) \
2081 X("moreproc program", 4, SHPROG) \
2082 X("nomoreproc", 3, SHNPROG) \
2083 X("length lines", 4, SHLEN) \
2084 X("width columns", 4, SHWID) \
2085 X("showproc program", 4, SHSHOW) \
2086 X("noshowproc", 3, SHNSHOW) \
2087 X("header", 4, SHHEAD) \
2088 X("noheader", 3, SHNHEAD) \
2089 X("help", 0, SHHELP) \
2091 #define X(sw, minchars, id) id,
2092 DEFINE_SWITCH_ENUM(SHOW
);
2095 #define X(sw, minchars, id) { sw, minchars, id },
2096 DEFINE_SWITCH_ARRAY(SHOW
, showswit
);
2101 showcmd (char **args
)
2103 int headersw
= 1, nshow
= 0, msgp
= 0, vecp
= 1;
2104 int mhl
= 0, seqnum
= -1, mode
= 0, i
, msgnum
;
2105 char *cp
, *proc
= showproc
, buf
[BUFSIZ
];
2106 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
2108 if (!strcasecmp (cmd_name
, "next"))
2111 if (!strcasecmp (cmd_name
, "prev"))
2113 while ((cp
= *args
++)) {
2115 switch (i
= smatch (++cp
, showswit
)) {
2117 ambigsw (cp
, showswit
);
2124 snprintf (buf
, sizeof(buf
), "%s %s[switches] [switches for showproc]",
2125 cmd_name
, mode
? NULL
: "[msgs] ");
2126 print_help (buf
, showswit
, 1);
2134 if (!(cp
= *args
++) || *cp
== '-') {
2135 advise (NULL
, "missing argument to %s", args
[-2]);
2147 if (!(proc
= *args
++) || *proc
== '-') {
2148 advise (NULL
, "missing argument to %s", args
[-2]);
2158 advise (NULL
, "sorry, -%s not allowed!", showswit
[i
].sw
);
2161 if (*cp
== '+' || *cp
== '@') {
2162 advise (NULL
, "sorry, no folders allowed!");
2168 "usage: %s [switches] [switches for showproc]\n",
2178 msgs
[msgp
++] = mode
> 0 ? "next" : mode
< 0 ? "prev" : "cur";
2179 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2180 if (!m_convert (mp
, msgs
[msgnum
]))
2185 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2186 if (is_selected (mp
, msgnum
) && is_nontext (msgnum
)) {
2187 proc
= showmimeproc
;
2188 vec
[vecp
++] = "-file";
2196 if (strcmp (showproc
, "mhl") == 0) {
2202 seqnum
= seq_getnum (mp
, "unseen");
2203 vec
[0] = r1bindex (proc
, '/');
2206 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2207 if (is_selected (mp
, msgnum
)) {
2208 vec
[vecp
++] = getcpy (m_name (msgnum
));
2210 seq_delmsg (mp
, "unseen", msgnum
);
2213 if (mp
->numsel
== 1 && headersw
)
2215 mhlsbr (vecp
, vec
, mhl_action
);
2216 scan_eom_action ((int (*)()) 0);
2221 for (msgnum
= mp
->lowsel
;
2222 msgnum
<= mp
->hghsel
&& !interrupted
;
2224 if (is_selected (mp
, msgnum
)) {
2225 switch (ask (msgnum
)) {
2226 case NOTOK
: /* QUIT */
2233 if (mp
->numsel
== 1 && headersw
)
2236 copy_message (msgnum
, stdout
);
2238 process (msgnum
, proc
, vecp
, vec
);
2241 seq_delmsg (mp
, "unseen", msgnum
);
2248 seq_setcur (mp
, mp
->hghsel
);
2255 if (Msgs
[msgnum
].m_bboard_id
== 0)
2258 printf ("(Message %d", msgnum
);
2259 if (Msgs
[msgnum
].m_bboard_id
> 0)
2260 printf (", %s: %d", BBoard_ID
, Msgs
[msgnum
].m_bboard_id
);
2271 return (ftell (mhlfp
) >= Msgs
[mhlnum
].m_stop
);
2276 mhl_action (char *name
)
2280 if ((msgnum
= m_atoi (name
)) < mp
->lowmsg
2281 || msgnum
> mp
->hghmsg
2282 || !does_exist (mp
, msgnum
))
2286 mhlfp
= msh_ready (msgnum
, 1);
2288 scan_eom_action (eom_action
);
2300 if (mp
->numsel
== 1 || !interactive
|| redirected
)
2303 if (SOprintf ("Press <return> to list \"%d\"...", msgnum
)) {
2304 if (mp
->lowsel
!= msgnum
)
2306 printf ("Press <return> to list \"%d\"...", msgnum
);
2311 read (fileno (stdout
), buf
, sizeof buf
);
2313 if (strchr(buf
, '\n') == NULL
)
2317 told_to_quit
= interrupted
= 0;
2332 is_nontext (int msgnum
)
2336 char buf
[BUFSIZ
], name
[NAMESZ
];
2339 if (Msgs
[msgnum
].m_flags
& MHNCHK
)
2340 return (Msgs
[msgnum
].m_flags
& MHNYES
);
2341 Msgs
[msgnum
].m_flags
|= MHNCHK
;
2343 fp
= msh_ready (msgnum
, 1);
2346 int bufsz
= sizeof buf
;
2347 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
2351 * Check Content-Type field
2353 if (!strcasecmp (name
, TYPE_FIELD
)) {
2357 cp
= add (buf
, NULL
);
2358 while (state
== FLDPLUS
) {
2360 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2367 for (; isspace ((unsigned char) *bp
); bp
++)
2372 for (bp
++, i
= 0;;) {
2402 for (dp
= bp
; istoken (*dp
); dp
++)
2409 if ((result
= (strcasecmp (bp
, "plain") != 0)))
2412 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
2415 if ((result
= !uprf (dp
, "charset")))
2417 dp
+= sizeof "charset" - 1;
2418 while (isspace ((unsigned char) *dp
))
2422 while (isspace ((unsigned char) *dp
))
2425 if ((bp
= strchr(++dp
, '"')))
2428 for (bp
= dp
; *bp
; bp
++)
2429 if (isspace ((unsigned char) *bp
)) {
2435 /* Default character set */
2438 /* Check the character set */
2439 result
= !check_charset (dp
, strlen (dp
));
2441 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
2451 Msgs
[msgnum
].m_flags
|= MHNYES
;
2458 * Check Content-Transfer-Encoding field
2460 if (!strcasecmp (name
, ENCODING_FIELD
)) {
2461 cp
= add (buf
, NULL
);
2462 while (state
== FLDPLUS
) {
2464 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2467 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
2469 for (dp
= bp
; istoken (*dp
); dp
++)
2472 result
= (strcasecmp (bp
, "7bit")
2473 && strcasecmp (bp
, "8bit")
2474 && strcasecmp (bp
, "binary"));
2478 Msgs
[msgnum
].m_flags
|= MHNYES
;
2485 * Just skip the rest of this header
2486 * field and go to next one.
2488 while (state
== FLDPLUS
) {
2490 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2495 * We've passed the message header,
2496 * so message is just text.
2505 #define SORT_SWITCHES \
2506 X("datefield field", 0, SODATE) \
2507 X("textfield field", 0, SOSUBJ) \
2508 X("notextfield", 0, SONSUBJ) \
2509 X("limit days", 0, SOLIMT) \
2510 X("nolimit", 0, SONLIMT) \
2511 X("verbose", 0, SOVERB) \
2512 X("noverbose", 0, SONVERB) \
2513 X("help", 0, SOHELP) \
2515 #define X(sw, minchars, id) id,
2516 DEFINE_SWITCH_ENUM(SORT
);
2519 #define X(sw, minchars, id) { sw, minchars, id },
2520 DEFINE_SWITCH_ARRAY(SORT
, sortswit
);
2525 sortcmd (char **args
)
2527 int msgp
= 0, msgnum
;
2528 char *cp
, *datesw
= NULL
, *subjsw
= NULL
;
2529 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
2533 forkcmd (args
, cmd_name
);
2537 while ((cp
= *args
++)) {
2539 switch (smatch (++cp
, sortswit
)) {
2541 ambigsw (cp
, sortswit
);
2544 fprintf (stderr
, "-%s unknown\n", cp
);
2547 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
2548 print_help (buf
, sortswit
, 1);
2553 advise (NULL
, "only one date field at a time!");
2556 if (!(datesw
= *args
++) || *datesw
== '-') {
2557 advise (NULL
, "missing argument to %s", args
[-2]);
2564 advise (NULL
, "only one text field at a time!");
2567 if (!(subjsw
= *args
++) || *subjsw
== '-') {
2568 advise (NULL
, "missing argument to %s", args
[-2]);
2576 case SOLIMT
: /* too hard */
2577 if (!(cp
= *args
++) || *cp
== '-') {
2578 advise (NULL
, "missing argument to %s", args
[-2]);
2582 case SOVERB
: /* not implemented */
2586 if (*cp
== '+' || *cp
== '@') {
2587 advise (NULL
, "sorry, no folders allowed!");
2595 msgs
[msgp
++] = "all";
2598 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2599 if (!m_convert (mp
, msgs
[msgnum
]))
2603 twscopy (&tb
, dlocaltimenow ());
2605 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2606 if (Msgs
[msgnum
].m_scanl
) {
2607 free (Msgs
[msgnum
].m_scanl
);
2608 Msgs
[msgnum
].m_scanl
= NULL
;
2610 if (is_selected (mp
, msgnum
)) {
2611 if (get_fields (datesw
, subjsw
, msgnum
, &Msgs
[msgnum
]))
2612 twscopy (&Msgs
[msgnum
].m_tb
,
2613 msgnum
!= mp
->lowsel
? &Msgs
[msgnum
- 1].m_tb
: &tb
);
2615 else /* m_scaln is already NULL */
2616 twscopy (&Msgs
[msgnum
].m_tb
, &tb
);
2617 Msgs
[msgnum
].m_stats
= bvector_create (0);
2618 bvector_copy (Msgs
[msgnum
].m_stats
, msgstat (mp
, msgnum
));
2619 if (mp
->curmsg
== msgnum
)
2620 bvector_set (Msgs
[msgnum
].m_stats
, CUR
);
2623 qsort ((char *) &Msgs
[mp
->lowsel
], mp
->hghsel
- mp
->lowsel
+ 1,
2624 sizeof(struct Msg
), (qsort_comp
) (subjsw
? subsort
: msgsort
));
2626 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2627 if (subjsw
&& Msgs
[msgnum
].m_scanl
) {
2628 free (Msgs
[msgnum
].m_scanl
); /* from subjsort */
2629 Msgs
[msgnum
].m_scanl
= NULL
;
2631 bvector_clear (Msgs
[msgnum
].m_stats
, CUR
);
2632 bvector_copy (msgstat (mp
, msgnum
), Msgs
[msgnum
].m_stats
);
2633 if (bvector_at (Msgs
[msgnum
].m_stats
, CUR
))
2634 seq_setcur (mp
, msgnum
);
2637 mp
->msgflags
|= MODIFIED
;
2643 * get_fields - parse message, and get date and subject if needed.
2644 * We'll use the msgp->m_tb tws struct for the date, and overload
2645 * the msgp->m_scanl field with our subject string.
2648 get_fields (char *datesw
, char *subjsw
, int msgnum
, struct Msg
*msgp
)
2650 int state
, gotdate
= 0;
2651 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
2652 struct tws
*tw
= (struct tws
*) 0;
2655 zp
= msh_ready (msgnum
, 0);
2658 int bufsz
= sizeof buf
;
2659 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
2662 if (!strcasecmp (name
, datesw
)) {
2664 while (state
== FLDPLUS
) {
2666 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2669 if ((tw
= dparsetime (bp
)) == NULL
)
2671 "unable to parse %s field in message %d",
2674 twscopy (&(msgp
->m_tb
), tw
);
2676 if (!subjsw
) /* not using this, or already done */
2677 break; /* all done! */
2680 else if (subjsw
&& !strcasecmp(name
, subjsw
)) {
2682 while (state
== FLDPLUS
) {
2684 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2687 msgp
->m_scanl
= sosmash(subjsw
, bp
);
2689 break; /* date done so we're done */
2691 subjsw
= (char *)0;/* subject done, need date */
2693 while (state
== FLDPLUS
) { /* flush this one */
2695 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2706 admonish (NULL
, "format error in message %d", msgnum
);
2707 if (msgp
->m_scanl
) { /* this might need free'd */
2708 free (msgp
->m_scanl
); /* probably can't use subj anyway */
2709 msgp
->m_scanl
= NULL
;
2714 adios (NULL
, "internal error -- you lose");
2719 return OK
; /* not an error if subj not found */
2721 admonish (NULL
, "no %s field in message %d", datesw
, msgnum
);
2722 return NOTOK
; /* NOTOK means use some other date */
2731 msgsort (struct Msg
*a
, struct Msg
*b
)
2733 return twsort (&a
->m_tb
, &b
->m_tb
);
2738 subsort (struct Msg
*a
, struct Msg
*b
)
2742 if (a
->m_scanl
&& b
->m_scanl
)
2743 if ((i
= strcmp (a
->m_scanl
, b
->m_scanl
)))
2746 return twsort (&a
->m_tb
, &b
->m_tb
);
2751 * try to make the subject "canonical": delete leading "re:", everything
2752 * but letters & smash letters to lower case.
2755 sosmash (char *subj
, char *s
)
2757 register char *cp
, *dp
;
2758 register unsigned char c
;
2762 dp
= s
; /* dst pointer */
2763 if (!strcasecmp (subj
, "subject"))
2770 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2776 while ((c
= *cp
++)) {
2778 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2788 process (int msgnum
, char *proc
, int vecp
, char **vec
)
2790 int child_id
, status
;
2791 char tmpfil
[BUFSIZ
];
2796 strncpy (tmpfil
, m_name (msgnum
), sizeof(tmpfil
));
2797 context_del (pfolder
);
2798 context_replace (pfolder
, fmsh
);/* update current folder */
2800 context_save (); /* save the context file */
2804 cp
= m_mktemp(invo_name
, NULL
, &out
);
2806 /* Try again, but try to create under /tmp */
2808 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
2811 advise (NULL
, "unable to create temporary file");
2815 copy_message (msgnum
, out
);
2817 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
2821 switch (child_id
= fork ()) {
2823 advise ("fork", "unable to");
2829 SIGNAL (SIGINT
, istat
);
2830 SIGNAL (SIGQUIT
, qstat
);
2832 vec
[vecp
++] = tmpfil
;
2836 fprintf (stderr
, "unable to exec ");
2841 status
= pidXwait (child_id
, NULL
);
2852 copy_message (int msgnum
, FILE *out
)
2855 static char buffer
[BUFSIZ
];
2858 zp
= msh_ready (msgnum
, 1);
2860 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
) {
2861 fputs (buffer
, out
);
2862 if (interrupted
&& out
== stdout
)
2868 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2869 && pos
< Msgs
[msgnum
].m_stop
) {
2870 fputs (buffer
, out
);
2871 pos
+= (long) strlen (buffer
);
2872 if (interrupted
&& out
== stdout
)
2880 copy_digest (int msgnum
, FILE *out
)
2884 static char buffer
[BUFSIZ
];
2888 zp
= msh_ready (msgnum
, 1);
2891 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2892 && !fmsh
&& pos
< Msgs
[msgnum
].m_stop
) {
2893 if (c
== '\n' && *buffer
== '-')
2895 fputs (buffer
, out
);
2896 c
= buffer
[strlen (buffer
) - 1];
2898 pos
+= (long) strlen (buffer
);
2899 if (interrupted
&& out
== stdout
)