]>
diplodocus.org Git - nmh/blob - uip/mshcmds.c
acd7671fa112bb85d7fcbcb471cd288e4bc1aeed
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
; /* use the gstate in scansbr.c */
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 #define DIST_SWITCHES \
99 X("annotate", 0, DIANSW) \
100 X("noannotate", 0, DINANSW) \
101 X("draftfolder +folder", 0, DIDFSW) \
102 X("draftmessage msg", 0, DIDMSW) \
103 X("nodraftfolder", 0, DINDFSW) \
104 X("editor editor", 0, DIEDTSW) \
105 X("noedit", 0, DINEDSW) \
106 X("form formfile", 0, DIFRMSW) \
107 X("inplace", 0, DIINSW) \
108 X("noinplace", 0, DININSW) \
109 X("whatnowproc program", 0, DIWHTSW) \
110 X("nowhatnowproc", 0, DINWTSW) \
111 X("help", 0, DIHELP) \
113 #define X(sw, minchars, id) id,
114 DEFINE_SWITCH_ENUM(DIST
);
117 #define X(sw, minchars, id) { sw, minchars, id },
118 DEFINE_SWITCH_ARRAY(DIST
, distswit
);
123 distcmd (char **args
)
126 char *cp
, *msg
= NULL
;
127 char buf
[BUFSIZ
], *vec
[MAXARGS
];
130 forkcmd (args
, cmd_name
);
134 while ((cp
= *args
++)) {
136 switch (smatch (++cp
, distswit
)) {
138 ambigsw (cp
, distswit
);
141 fprintf (stderr
, "-%s unknown\n", cp
);
144 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
145 print_help (buf
, distswit
, 1);
148 case DIANSW
: /* not implemented */
166 if (!(cp
= *args
++) || *cp
== '-') {
167 advise (NULL
, "missing argument to %s", args
[-2]);
173 if (*cp
== '+' || *cp
== '@') {
174 advise (NULL
, "sorry, no folders allowed!");
179 advise (NULL
, "only one message at a time!");
187 vec
[vecp
++] = "-file";
191 if (!m_convert (mp
, msg
))
195 if (mp
->numsel
> 1) {
196 advise (NULL
, "only one message at a time!");
199 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
200 seq_setcur (mp
, mp
->hghsel
);
204 #define EXPLODE_SWITCHES \
205 X("inplace", 0, EXINSW) \
206 X("noinplace", 0, EXNINSW) \
207 X("quiet", 0, EXQISW) \
208 X("noquiet", 0, EXNQISW) \
209 X("verbose", 0, EXVBSW) \
210 X("noverbose", 0, EXNVBSW) \
211 X("help", 0, EXHELP) \
213 #define X(sw, minchars, id) id,
214 DEFINE_SWITCH_ENUM(EXPLODE
);
217 #define X(sw, minchars, id) { sw, minchars, id },
218 DEFINE_SWITCH_ARRAY(EXPLODE
, explswit
);
223 explcmd (char **args
)
225 int inplace
= 0, quietsw
= 0, verbosw
= 0;
226 int msgp
= 0, hi
, msgnum
;
227 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
231 forkcmd (args
, cmd_name
);
235 while ((cp
= *args
++)) {
237 switch (smatch (++cp
, explswit
)) {
239 ambigsw (cp
, explswit
);
242 fprintf (stderr
, "-%s unknown\n", cp
);
245 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
246 print_help (buf
, explswit
, 1);
268 if (*cp
== '+' || *cp
== '@') {
269 advise (NULL
, "sorry, no folders allowed!");
277 msgs
[msgp
++] = "cur";
278 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
279 if (!m_convert (mp
, msgs
[msgnum
]))
283 smsgs
= (struct Msg
*)
284 calloc ((size_t) (MAXFOLDER
+ 2), sizeof *smsgs
);
286 adios (NULL
, "unable to allocate folder storage");
290 for (msgnum
= mp
->lowsel
;
291 msgnum
<= mp
->hghsel
&& !interrupted
;
293 if (is_selected (mp
, msgnum
))
294 if (burst (smsgs
, msgnum
, inplace
, quietsw
, verbosw
) != OK
)
297 free ((char *) smsgs
);
300 seq_setcur (mp
, mp
->lowsel
);
302 if (hi
<= mp
->hghmsg
)
305 mp
->msgflags
|= MODIFIED
;
311 burst (struct Msg
*smsgs
, int msgnum
, int inplace
, int quietsw
, int verbosw
)
313 int i
, j
, ld3
, wasdlm
, msgp
;
315 char c
, buffer
[BUFSIZ
];
318 ld3
= strlen (delim3
);
320 if (Msgs
[msgnum
].m_scanl
) {
321 free (Msgs
[msgnum
].m_scanl
);
322 Msgs
[msgnum
].m_scanl
= NULL
;
325 pos
= ftell (zp
= msh_ready (msgnum
, 1));
326 for (msgp
= 0; msgp
<= MAXFOLDER
;) {
327 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
329 && pos
< Msgs
[msgnum
].m_stop
)
330 pos
+= (long) strlen (buffer
);
331 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
)
333 fseek (zp
, pos
, SEEK_SET
);
334 smsgs
[msgp
].m_start
= pos
;
337 pos
< Msgs
[msgnum
].m_stop
338 && fgets (buffer
, sizeof buffer
, zp
) != NULL
;
340 if (strncmp (buffer
, delim3
, ld3
) == 0
341 && (msgp
== 1 || c
== '\n')
342 && peekc (zp
) == '\n')
345 pos
+= (long) strlen (buffer
);
347 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
348 if (smsgs
[msgp
].m_start
!= pos
)
349 smsgs
[msgp
++].m_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
350 if (feof (zp
) || pos
>= Msgs
[msgnum
].m_stop
) {
352 smsgs
[msgp
- 1].m_stop
-= ((long) strlen (buffer
) + 1);
355 pos
+= (long) strlen (buffer
);
358 switch (msgp
--) { /* toss "End of XXX Digest" */
360 adios (NULL
, "burst() botch -- you lose big");
364 printf ("message %d not in digest format\n", msgnum
);
369 printf ("%d message%s exploded from digest %d\n",
370 msgp
, msgp
!= 1 ? "s" : "", msgnum
);
374 if ((i
= msgp
+ mp
->hghmsg
) > MAXFOLDER
) {
375 advise (NULL
, "more than %d messages", MAXFOLDER
);
378 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, i
)))
379 adios (NULL
, "unable to allocate folder storage");
384 if (mp
->hghsel
> msgnum
)
388 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
390 printf ("message %d becomes message %d\n", j
, i
);
392 Msgs
[i
].m_bboard_id
= Msgs
[j
].m_bboard_id
;
393 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
394 Msgs
[i
].m_start
= Msgs
[j
].m_start
;
395 Msgs
[i
].m_stop
= Msgs
[j
].m_stop
;
396 Msgs
[i
].m_scanl
= NULL
;
397 if (Msgs
[j
].m_scanl
) {
398 free (Msgs
[j
].m_scanl
);
399 Msgs
[j
].m_scanl
= NULL
;
401 copy_msg_flags (mp
, i
, j
);
404 if (Msgs
[msgnum
].m_bboard_id
== 0)
407 unset_selected (mp
, msgnum
);
408 i
= inplace
? msgnum
+ msgp
: mp
->hghmsg
;
409 for (j
= msgp
; j
>= (inplace
? 0 : 1); i
--, j
--) {
410 if (verbosw
&& i
!= msgnum
)
411 printf ("message %d of digest %d becomes message %d\n",
414 Msgs
[i
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
415 Msgs
[i
].m_top
= Msgs
[j
].m_top
;
416 Msgs
[i
].m_start
= smsgs
[j
].m_start
;
417 Msgs
[i
].m_stop
= smsgs
[j
].m_stop
;
418 Msgs
[i
].m_scanl
= NULL
;
419 copy_msg_flags (mp
, i
, msgnum
);
426 #define FILE_SWITCHES \
427 X("draft", 0, FIDRFT) \
428 X("link", 0, FILINK) \
429 X("nolink", 0, FINLINK) \
430 X("preserve", 0, FIPRES) \
431 X("nopreserve", 0, FINPRES) \
432 X("src +folder", 0, FISRC) \
433 X("file file", 0, FIFILE) \
434 X("rmmproc program", 0, FIPROC) \
435 X("normmproc", 0, FINPRC) \
436 X("help", 0, FIHELP) \
438 #define X(sw, minchars, id) id,
439 DEFINE_SWITCH_ENUM(FILE);
442 #define X(sw, minchars, id) { sw, minchars, id },
443 DEFINE_SWITCH_ARRAY(FILE, fileswit
);
448 filecmd (char **args
)
450 int linksw
= 0, msgp
= 0;
451 int vecp
= 1, i
, msgnum
;
452 char *cp
, buf
[BUFSIZ
];
453 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
456 forkcmd (args
, cmd_name
);
460 while ((cp
= *args
++)) {
462 switch (i
= smatch (++cp
, fileswit
)) {
464 ambigsw (cp
, fileswit
);
467 fprintf (stderr
, "-%s unknown\n", cp
);
470 snprintf (buf
, sizeof(buf
), "%s +folder... [msgs] [switches]", cmd_name
);
471 print_help (buf
, fileswit
, 1);
490 advise (NULL
, "sorry, -%s not allowed!", fileswit
[i
].sw
);
493 if (*cp
== '+' || *cp
== '@')
500 vec
[vecp
++] = "-file";
503 msgs
[msgp
++] = "cur";
504 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
505 if (!m_convert (mp
, msgs
[msgnum
]))
510 for (msgnum
= mp
->lowsel
;
511 msgnum
<= mp
->hghsel
&& !interrupted
;
513 if (is_selected (mp
, msgnum
))
514 if (process (msgnum
, fileproc
, vecp
, vec
)) {
515 unset_selected (mp
, msgnum
);
519 if (mp
->numsel
!= mp
->nummsg
|| linksw
)
520 seq_setcur (mp
, mp
->hghsel
);
527 filehak (char **args
)
529 int result
, vecp
= 0;
530 char *cp
, *cwd
, *vec
[MAXARGS
];
532 while ((cp
= *args
++)) {
534 switch (smatch (++cp
, fileswit
)) {
551 if (*cp
== '+' || *cp
== '@')
558 for (vecp
= 0; (cp
= vec
[vecp
]) && result
== NOTOK
; vecp
++) {
560 cwd
= getcpy (pwd ());
561 chdir (m_maildir (""));
563 if (access (m_maildir (cp
), F_OK
) == NOTOK
)
574 #define FOLDER_SWITCHES \
575 X("all", 0, FLALSW) \
576 X("fast", 0, FLFASW) \
577 X("nofast", 0, FLNFASW) \
578 X("header", 0, FLHDSW) \
579 X("noheader", 0, FLNHDSW) \
580 X("pack", 0, FLPKSW) \
581 X("nopack", 0, FLNPKSW) \
582 X("recurse", 0, FLRCSW) \
583 X("norecurse", 0, FLNRCSW) \
584 X("total", 0, FLTLSW) \
585 X("nototal", 0, FLNTLSW) \
586 X("print", 0, FLPRSW) \
587 X("push", 0, FLPUSW) \
588 X("pop", 0, FLPOSW) \
589 X("list", 0, FLLISW) \
590 X("help", 0, FLHELP) \
592 #define X(sw, minchars, id) id,
593 DEFINE_SWITCH_ENUM(FOLDER
);
596 #define X(sw, minchars, id) { sw, minchars, id },
597 DEFINE_SWITCH_ARRAY(FOLDER
, foldswit
);
602 foldcmd (char **args
)
604 int fastsw
= 0, headersw
= 0, packsw
= 0;
606 char *cp
, *folder
= NULL
, *msg
= NULL
;
607 char buf
[BUFSIZ
], **vec
= args
;
612 while ((cp
= *args
++)) {
614 switch (smatch (++cp
, foldswit
)) {
616 ambigsw (cp
, foldswit
);
619 fprintf (stderr
, "-%s unknown\n", cp
);
622 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]", cmd_name
);
623 print_help (buf
, foldswit
, 1);
626 case FLALSW
: /* not implemented */
656 if (*cp
== '+' || *cp
== '@') {
658 advise (NULL
, "only one folder at a time!\n");
662 folder
= fmsh
? pluspath (cp
)
667 advise (NULL
, "only one message at a time!\n");
676 advise (NULL
, "null folder names are not permitted");
680 if (access (m_maildir (folder
), R_OK
) == NOTOK
) {
681 advise (folder
, "unable to read");
686 strncpy (buf
, folder
, sizeof(buf
));
687 if (expand (buf
) == NOTOK
)
690 if (access (folder
, R_OK
) == NOTOK
) {
691 advise (folder
, "unable to read");
706 if (!m_convert (mp
, msg
))
710 if (mp
->numsel
> 1) {
711 advise (NULL
, "only one message at a time!");
714 seq_setcur (mp
, mp
->hghsel
);
719 forkcmd (vec
, cmd_name
);
723 if (mp
->lowoff
> 1 && !(mp
= folder_realloc (mp
, 1, mp
->hghmsg
)))
724 adios (NULL
, "unable to allocate folder storage");
726 for (msgnum
= mp
->lowmsg
, hole
= 1; msgnum
<= mp
->hghmsg
; msgnum
++)
727 if (does_exist (mp
, msgnum
)) {
728 if (msgnum
!= hole
) {
729 Msgs
[hole
].m_bboard_id
= Msgs
[msgnum
].m_bboard_id
;
730 Msgs
[hole
].m_top
= Msgs
[msgnum
].m_top
;
731 Msgs
[hole
].m_start
= Msgs
[msgnum
].m_start
;
732 Msgs
[hole
].m_stop
= Msgs
[msgnum
].m_stop
;
733 Msgs
[hole
].m_scanl
= NULL
;
734 if (Msgs
[msgnum
].m_scanl
) {
735 free (Msgs
[msgnum
].m_scanl
);
736 Msgs
[msgnum
].m_scanl
= NULL
;
738 copy_msg_flags (mp
, hole
, msgnum
);
739 if (mp
->curmsg
== msgnum
)
740 seq_setcur (mp
, hole
);
744 if (mp
->nummsg
> 0) {
746 mp
->hghmsg
= hole
- 1;
748 mp
->msgflags
|= MODIFIED
;
754 printf ("%s\n", fmsh
? fmsh
: mp
->foldpath
);
757 printf ("\t\tFolder %*s# of messages (%*srange%*s); cur%*smsg\n",
758 DMAXFOLDER
, "", DMAXFOLDER
- 2, "", DMAXFOLDER
- 2, "",
760 printf (args
? "%22s " : "%s ", fmsh
? fmsh
: mp
->foldpath
);
762 /* check for empty folder */
763 if (mp
->nummsg
== 0) {
764 printf ("has no messages%*s",
765 mp
->msgflags
& OTHERS
? DMAXFOLDER
* 2 + 4 : 0, "");
767 printf ("has %*d message%s (%*d-%*d)",
768 DMAXFOLDER
, mp
->nummsg
, mp
->nummsg
!= 1 ? "s" : "",
769 DMAXFOLDER
, mp
->lowmsg
, DMAXFOLDER
, mp
->hghmsg
);
770 if (mp
->curmsg
>= mp
->lowmsg
771 && mp
->curmsg
<= mp
->hghmsg
)
772 printf ("; cur=%*d", DMAXFOLDER
, mp
->curmsg
);
779 #define FORW_SWITCHES \
780 X("annotate", 0, FOANSW) \
781 X("noannotate", 0, FONANSW) \
782 X("draftfolder +folder", 0, FODFSW) \
783 X("draftmessage msg", 0, FODMSW) \
784 X("nodraftfolder", 0, FONDFSW) \
785 X("editor editor", 0, FOEDTSW) \
786 X("noedit", 0, FONEDSW) \
787 X("filter filterfile", 0, FOFTRSW) \
788 X("form formfile", 0, FOFRMSW) \
789 X("format", 5, FOFTSW) \
790 X("noformat", 7, FONFTSW) \
791 X("inplace", 0, FOINSW) \
792 X("noinplace", 0, FONINSW) \
793 X("mime", 0, FOMISW) \
794 X("nomime", 0, FONMISW) \
795 X("whatnowproc program", 0, FOWHTSW) \
796 X("nowhatnow", 0, FONWTSW) \
797 X("help", 0, FOHELP) \
799 #define X(sw, minchars, id) id,
800 DEFINE_SWITCH_ENUM(FORW
);
803 #define X(sw, minchars, id) { sw, minchars, id },
804 DEFINE_SWITCH_ARRAY(FORW
, forwswit
);
809 forwcmd (char **args
)
811 int msgp
= 0, vecp
= 1, msgnum
;
812 char *cp
, *filter
= NULL
, buf
[BUFSIZ
];
813 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
818 forkcmd (args
, cmd_name
);
822 while ((cp
= *args
++)) {
824 switch (smatch (++cp
, forwswit
)) {
826 ambigsw (cp
, forwswit
);
829 fprintf (stderr
, "-%s unknown\n", cp
);
832 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
833 print_help (buf
, forwswit
, 1);
836 case FOANSW
: /* not implemented */
856 if (!(cp
= *args
++) || *cp
== '-') {
857 advise (NULL
, "missing argument to %s", args
[-2]);
863 if (!(filter
= *args
++) || *filter
== '-') {
864 advise (NULL
, "missing argument to %s", args
[-2]);
869 if (access (filter
= myfilter
, R_OK
) == NOTOK
) {
870 advise (filter
, "unable to read default filter file");
878 if (*cp
== '+' || *cp
== '@') {
879 advise (NULL
, "sorry, no folders allowed!");
886 /* foil search of .mh_profile */
887 snprintf (buf
, sizeof(buf
), "%sXXXXXX", invo_name
);
889 tfile
= m_mktemp(buf
, NULL
, NULL
);
890 if (tfile
== NULL
) adios("forwcmd", "unable to create temporary file");
891 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
894 vec
[vecp
++] = "-file";
897 msgs
[msgp
++] = "cur";
898 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
899 if (!m_convert (mp
, msgs
[msgnum
]))
904 strncpy (buf
, filter
, sizeof(buf
));
905 if (expand (buf
) == NOTOK
)
907 if (access (filter
= getcpy (etcpath (buf
)), R_OK
) == NOTOK
) {
908 advise (filter
, "unable to read");
913 forw (cmd_name
, filter
, vecp
, vec
);
914 seq_setcur (mp
, mp
->hghsel
);
921 forw (char *proc
, char *filter
, int vecp
, char **vec
)
923 int i
, child_id
, msgnum
, msgcnt
;
924 char tmpfil
[BUFSIZ
], *args
[MAXARGS
];
928 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
929 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
930 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
934 switch (child_id
= fork ()) {
936 advise ("fork", "unable to");
939 case OK
: /* "trust me" */
940 if (freopen (tmpfil
, "w", stdout
) == NULL
) {
941 fprintf (stderr
, "unable to create ");
945 args
[0] = r1bindex (mhlproc
, '/');
947 args
[i
++] = "-forwall";
950 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
951 if (is_selected (mp
, msgnum
))
952 args
[i
++] = getcpy (m_name (msgnum
));
954 mhlsbr (i
, args
, mhl_action
);
955 scan_eom_action ((int (*) ()) 0);
960 if (pidXwait (child_id
, NULL
))
965 if ((out
= fopen (tmpfil
, "w")) == NULL
) {
966 advise (tmpfil
, "unable to create temporary file");
971 for (msgnum
= mp
->lowsel
;
972 msgnum
<= mp
->hghsel
&& !interrupted
;
974 if (is_selected (mp
, msgnum
)) {
975 fprintf (out
, "\n\n-------");
976 if (msgnum
== mp
->lowsel
)
977 fprintf (out
, " Forwarded Message%s",
978 mp
->numsel
> 1 ? "s" : "");
980 fprintf (out
, " Message %d", msgcnt
);
981 fprintf (out
, "\n\n");
982 copy_digest (msgnum
, out
);
986 fprintf (out
, "\n\n------- End of Forwarded Message%s\n",
987 mp
->numsel
> 1 ? "s" : "");
993 switch (child_id
= fork ()) {
995 advise ("fork", "unable to");
1000 SIGNAL (SIGINT
, istat
);
1001 SIGNAL (SIGQUIT
, qstat
);
1003 vec
[vecp
++] = tmpfil
;
1007 fprintf (stderr
, "unable to exec ");
1012 pidXwait (child_id
, NULL
);
1020 static char *hlpmsg
[] = {
1021 "The %s program emulates many of the commands found in the nmh",
1022 "system. Instead of operating on nmh folders, commands to %s concern",
1025 "To see the list of commands available, just type a ``?'' followed by",
1026 "the RETURN key. To find out what switches each command takes, type",
1027 "the name of the command followed by ``-help''. To leave %s, use the",
1028 "``quit'' command.",
1030 "Although a lot of nmh commands are found in %s, not all are fully",
1031 "implemented. %s will always recognize all legal switches for a",
1032 "given command though, and will let you know when you ask for an",
1033 "option that it is unable to perform.",
1035 "Running %s is fun, but using nmh from your shell is far superior.",
1036 "After you have familiarized yourself with the nmh style by using %s,",
1037 "you should try using nmh from the shell. You can still use %s for",
1038 "message files that aren't in nmh format, such as BBoard files.",
1044 helpcmd (char **args
)
1049 for (i
= 0; hlpmsg
[i
]; i
++) {
1050 printf (hlpmsg
[i
], invo_name
);
1056 #define MARK_SWITCHES \
1057 X("add", 0, MADDSW) \
1058 X("delete", 0, MDELSW) \
1059 X("list", 0, MLSTSW) \
1060 X("sequence name", 0, MSEQSW) \
1061 X("public", 0, MPUBSW) \
1062 X("nopublic", 0, MNPUBSW) \
1063 X("zero", 0, MZERSW) \
1064 X("nozero", 0, MNZERSW) \
1065 X("help", 0, MHELP) \
1066 X("debug", -5, MDBUGSW) \
1068 #define X(sw, minchars, id) id,
1069 DEFINE_SWITCH_ENUM(MARK
);
1072 #define X(sw, minchars, id) { sw, minchars, id },
1073 DEFINE_SWITCH_ARRAY(MARK
, markswit
);
1078 markcmd (char **args
)
1080 int addsw
= 0, deletesw
= 0, debugsw
= 0;
1081 int listsw
= 0, zerosw
= 0;
1083 int msgp
= 0, msgnum
;
1084 char *cp
, buf
[BUFSIZ
];
1085 char *seqs
[NUMATTRS
+ 1], *msgs
[MAXARGS
];
1087 while ((cp
= *args
++)) {
1089 switch (smatch (++cp
, markswit
)) {
1091 ambigsw (cp
, markswit
);
1094 fprintf (stderr
, "-%s unknown\n", cp
);
1097 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1098 print_help (buf
, markswit
, 1);
1103 deletesw
= listsw
= 0;
1111 addsw
= deletesw
= 0;
1115 if (!(cp
= *args
++) || *cp
== '-') {
1116 advise (NULL
, "missing argument to %s", args
[-2]);
1119 if (seqp
< NUMATTRS
)
1122 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1127 case MPUBSW
: /* not implemented */
1143 if (*cp
== '+' || *cp
== '@') {
1144 advise (NULL
, "sorry, no folders allowed!");
1151 if (!addsw
&& !deletesw
&& !listsw
) {
1158 seqs
[seqp
++] = "unseen";
1162 msgs
[msgp
++] = "all";
1167 msgs
[msgp
++] = listsw
? "all" :"cur";
1168 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1169 if (!m_convert (mp
, msgs
[msgnum
]))
1173 printf ("invo_name=%s mypath=%s defpath=%s\n",
1174 invo_name
, mypath
, defpath
);
1175 printf ("ctxpath=%s context flags=%s\n",
1176 ctxpath
, snprintb (buf
, sizeof(buf
), (unsigned) ctxflags
, DBITS
));
1177 printf ("foldpath=%s flags=%s\n",
1179 snprintb (buf
, sizeof(buf
), (unsigned) mp
->msgflags
, FBITS
));
1180 printf ("hghmsg=%d lowmsg=%d nummsg=%d curmsg=%d\n",
1181 mp
->hghmsg
, mp
->lowmsg
, mp
->nummsg
, mp
->curmsg
);
1182 printf ("lowsel=%d hghsel=%d numsel=%d\n",
1183 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
1184 printf ("lowoff=%d hghoff=%d\n", mp
->lowoff
, mp
->hghoff
);
1187 if (seqp
== 0 && (addsw
|| deletesw
)) {
1188 advise (NULL
, "-%s requires at least one -sequence argument",
1189 addsw
? "add" : "delete");
1195 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1196 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1201 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1202 if (!seq_delsel (mp
, seqs
[seqp
], 0, zerosw
))
1206 /* Listing messages in sequences */
1209 /* list the given sequences */
1210 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1211 seq_print (mp
, seqs
[seqp
]);
1213 /* else list them all */
1219 for (msgnum
= mp
->lowsel
;
1220 msgnum
<= mp
->hghsel
&& !interrupted
;
1222 if (is_selected (mp
, msgnum
)) {
1223 printf ("%*d: id=%d top=%d start=%ld stop=%ld %s\n",
1226 Msgs
[msgnum
].m_bboard_id
,
1228 (long) Msgs
[msgnum
].m_start
,
1229 (long) Msgs
[msgnum
].m_stop
,
1230 snprintb (buf
, sizeof(buf
),
1231 (unsigned) *bvector_bits (msgstat (mp
,
1234 if (Msgs
[msgnum
].m_scanl
)
1235 printf ("%s", Msgs
[msgnum
].m_scanl
);
1241 #define MHN_SWITCHES \
1242 X("auto", 0, MHNAUTOSW) \
1243 X("noauto", 0, MHNNAUTOSW) \
1244 X("debug", -5, MHNDEBUGSW) \
1245 X("form formfile", 4, MHNFORMSW) \
1246 X("headers", 0, MHNHEADSW) \
1247 X("noheaders", 0, MHNNHEADSW) \
1248 X("list", 0, MHNLISTSW) \
1249 X("nolist", 0, MHNNLISTSW) \
1250 X("part number", 0, MHNPARTSW) \
1251 X("realsize", 0, MHNSIZESW) \
1252 X("norealsize", 0, MHNNSIZESW) \
1253 X("rfc934mode", 0, MHNRFC934SW) \
1254 X("norfc934mode", 0, MHNNRFC934SW) \
1255 X("serialonly", 0, MHNSERIALSW) \
1256 X("noserialonly", 0, MHNNSERIALSW) \
1257 X("show", 0, MHNSHOWSW) \
1258 X("noshow", 0, MHNNSHOWSW) \
1259 X("store", 0, MHNSTORESW) \
1260 X("nostore", 0, MHNNSTORESW) \
1261 X("type content", 0, MHNTYPESW) \
1262 X("verbose", 0, MHNVERBSW) \
1263 X("noverbose", 0, MHNNVERBSW) \
1264 X("help", 0, MHNHELPSW) \
1265 X("moreproc program", -4, MHNPROGSW) \
1266 X("nomoreproc", -3, MHNNPROGSW) \
1267 X("length lines", -4, MHNLENSW) \
1268 X("width columns", -4, MHNWIDSW) \
1270 #define X(sw, minchars, id) id,
1271 DEFINE_SWITCH_ENUM(MHN
);
1274 #define X(sw, minchars, id) { sw, minchars, id },
1275 DEFINE_SWITCH_ARRAY(MHN
, mhnswit
);
1280 mhncmd (char **args
)
1282 int msgp
= 0, vecp
= 1;
1284 char *cp
, buf
[BUFSIZ
];
1285 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
1288 forkcmd (args
, cmd_name
);
1291 while ((cp
= *args
++)) {
1293 switch (smatch (++cp
, mhnswit
)) {
1295 ambigsw (cp
, mhnswit
);
1298 fprintf (stderr
, "-%s unknown\n", cp
);
1301 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1302 print_help (buf
, mhnswit
, 1);
1335 if (!(cp
= *args
++) || *cp
== '-') {
1336 advise (NULL
, "missing argument to %s", args
[-2]);
1343 if (*cp
== '+' || *cp
== '@') {
1344 advise (NULL
, "sorry, no folders allowed!");
1352 vec
[vecp
++] = "-file";
1355 msgs
[msgp
++] = "cur";
1356 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1357 if (!m_convert (mp
, msgs
[msgnum
]))
1362 for (msgnum
= mp
->lowsel
;
1363 msgnum
<= mp
->hghsel
&& !interrupted
;
1365 if (is_selected (mp
, msgnum
))
1366 if (process (msgnum
, cmd_name
, vecp
, vec
)) {
1367 unset_selected (mp
, msgnum
);
1371 seq_setcur (mp
, mp
->hghsel
);
1375 #define PACK_SWITCHES \
1376 X("file name", 0, PAFISW) \
1377 X("help", 0, PAHELP) \
1379 #define X(sw, minchars, id) id,
1380 DEFINE_SWITCH_ENUM(PACK
);
1383 #define X(sw, minchars, id) { sw, minchars, id },
1384 DEFINE_SWITCH_ARRAY(PACK
, packswit
);
1387 static int mbx_style
= MMDF_FORMAT
;
1390 packcmd (char **args
)
1392 int msgp
= 0, md
, msgnum
;
1393 char *cp
, *file
= NULL
;
1394 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
1398 forkcmd (args
, cmd_name
);
1402 while ((cp
= *args
++)) {
1404 switch (smatch (++cp
, packswit
)) {
1406 ambigsw (cp
, packswit
);
1409 fprintf (stderr
, "-%s unknown\n", cp
);
1412 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1413 print_help (buf
, packswit
, 1);
1417 if (!(file
= *args
++) || *file
== '-') {
1418 advise (NULL
, "missing argument to %s", args
[-2]);
1423 if (*cp
== '+' || *cp
== '@') {
1424 advise (NULL
, "sorry, no folders allowed!");
1433 file
= path (file
, TFILE
);
1434 if (stat (file
, &st
) == NOTOK
) {
1435 if (errno
!= ENOENT
) {
1436 advise (file
, "error on file");
1439 md
= getanswer (cp
= concat ("Create file \"", file
, "\"? ", NULL
));
1446 msgs
[msgp
++] = "all";
1447 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1448 if (!m_convert (mp
, msgs
[msgnum
]))
1452 if ((md
= mbx_open (file
, mbx_style
, getuid (), getgid (), m_gmprot ())) == NOTOK
) {
1453 advise (file
, "unable to open");
1456 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1457 if (is_selected (mp
, msgnum
))
1458 if (pack (file
, md
, msgnum
) == NOTOK
)
1460 mbx_close (file
, md
);
1462 if (mp
->hghsel
!= mp
->curmsg
)
1463 seq_setcur (mp
, mp
->lowsel
);
1471 pack (char *mailbox
, int md
, int msgnum
)
1475 if (Msgs
[msgnum
].m_bboard_id
== 0)
1478 zp
= msh_ready (msgnum
, 1);
1479 return mbx_write (mailbox
, md
, zp
, Msgs
[msgnum
].m_bboard_id
,
1480 0L, ftell (zp
), Msgs
[msgnum
].m_stop
, 1, 1);
1485 packhak (char **args
)
1488 char *cp
, *file
= NULL
;
1490 while ((cp
= *args
++)) {
1492 switch (smatch (++cp
, packswit
)) {
1499 if (!(file
= *args
++) || *file
== '-')
1503 if (*cp
== '+' || *cp
== '@')
1507 file
= path (file
? file
: "./msgbox", TFILE
);
1508 result
= access (file
, F_OK
) == NOTOK
? OK
: NOTOK
;
1515 #define PICK_SWITCHES \
1516 X("and", 0, PIANSW) \
1517 X("or", 0, PIORSW) \
1518 X("not", 0, PINTSW) \
1519 X("lbrace", 0, PILBSW) \
1520 X("rbrace", 0, PIRBSW) \
1521 X("cc pattern", 0, PICCSW) \
1522 X("date pattern", 0, PIDASW) \
1523 X("from pattern", 0, PIFRSW) \
1524 X("search pattern", 0, PISESW) \
1525 X("subject pattern", 0, PISUSW) \
1526 X("to pattern", 0, PITOSW) \
1527 X("-othercomponent pattern", 15, PIOTSW) \
1528 X("after date", 0, PIAFSW) \
1529 X("before date", 0, PIBFSW) \
1530 X("datefield field", 5, PIDFSW) \
1531 X("sequence name", 0, PISQSW) \
1532 X("public", 0, PIPUSW) \
1533 X("nopublic", 0, PINPUSW) \
1534 X("zero", 0, PIZRSW) \
1535 X("nozero", 0, PINZRSW) \
1536 X("list", 0, PILISW) \
1537 X("nolist", 0, PINLISW) \
1538 X("help", 0, PIHELP) \
1540 #define X(sw, minchars, id) id,
1541 DEFINE_SWITCH_ENUM(PICK
);
1544 #define X(sw, minchars, id) { sw, minchars, id },
1545 DEFINE_SWITCH_ARRAY(PICK
, pickswit
);
1550 pickcmd (char **args
)
1552 int zerosw
= 1, msgp
= 0;
1554 int vecp
= 0, hi
, lo
, msgnum
;
1555 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1556 char *seqs
[NUMATTRS
], *vec
[MAXARGS
];
1559 while ((cp
= *args
++)) {
1565 switch (smatch (cp
, pickswit
)) {
1567 ambigsw (cp
, pickswit
);
1570 fprintf (stderr
, "-%s unknown\n", cp
);
1573 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1574 print_help (buf
, pickswit
, 1);
1588 if (!(cp
= *args
++)) {/* allow -xyz arguments */
1589 advise (NULL
, "missing argument to %s", args
[-2]);
1595 advise (NULL
, "internal error!");
1606 if (!(cp
= *args
++) || *cp
== '-') {
1607 advise (NULL
, "missing argument to %s", args
[-2]);
1610 if (seqp
< NUMATTRS
)
1613 advise (NULL
, "only %d sequences allowed!", NUMATTRS
);
1624 case PIPUSW
: /* not implemented */
1631 if (*cp
== '+' || *cp
== '@') {
1632 advise (NULL
, "sorry, no folders allowed!");
1641 msgs
[msgp
++] = "all";
1642 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1643 if (!m_convert (mp
, msgs
[msgnum
]))
1648 if (!pcompile (vec
, NULL
))
1654 for (msgnum
= mp
->lowsel
;
1655 msgnum
<= mp
->hghsel
&& !interrupted
;
1657 if (is_selected (mp
, msgnum
)) {
1658 zp
= msh_ready (msgnum
, 1);
1659 if (pmatches (zp
, msgnum
, fmsh
? 0L : Msgs
[msgnum
].m_start
,
1660 fmsh
? 0L : Msgs
[msgnum
].m_stop
)) {
1667 unset_selected (mp
, msgnum
);
1678 if (mp
->numsel
<= 0) {
1679 advise (NULL
, "no messages match specification");
1684 for (seqp
= 0; seqs
[seqp
]; seqp
++)
1685 if (!seq_addsel (mp
, seqs
[seqp
], 0, zerosw
))
1688 printf ("%d hit%s\n", mp
->numsel
, mp
->numsel
== 1 ? "" : "s");
1692 #define REPL_SWITCHES \
1693 X("annotate", 0, REANSW) \
1694 X("noannotate", 0, RENANSW) \
1695 X("cc type", 0, RECCSW) \
1696 X("nocc type", 0, RENCCSW) \
1697 X("draftfolder +folder", 0, REDFSW) \
1698 X("draftmessage msg", 0, REDMSW) \
1699 X("nodraftfolder", 0, RENDFSW) \
1700 X("editor editor", 0, REEDTSW) \
1701 X("noedit", 0, RENEDSW) \
1702 X("fcc +folder", 0, REFCCSW) \
1703 X("filter filterfile", 0, REFLTSW) \
1704 X("form formfile", 0, REFRMSW) \
1705 X("inplace", 0, REINSW) \
1706 X("noinplace", 0, RENINSW) \
1707 X("query", 0, REQUSW) \
1708 X("noquery", 0, RENQUSW) \
1709 X("whatnowproc program", 0, REWHTSW) \
1710 X("nowhatnow", 0, RENWTSW) \
1711 X("width columns", 0, REWIDSW) \
1712 X("help", 0, REHELP) \
1714 #define X(sw, minchars, id) id,
1715 DEFINE_SWITCH_ENUM(REPL
);
1718 #define X(sw, minchars, id) { sw, minchars, id },
1719 DEFINE_SWITCH_ARRAY(REPL
, replswit
);
1724 replcmd (char **args
)
1727 char *cp
, *msg
= NULL
;
1728 char buf
[BUFSIZ
], *vec
[MAXARGS
];
1731 forkcmd (args
, cmd_name
);
1735 while ((cp
= *args
++)) {
1737 switch (smatch (++cp
, replswit
)) {
1739 ambigsw (cp
, replswit
);
1742 fprintf (stderr
, "-%s unknown\n", cp
);
1745 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1746 print_help (buf
, replswit
, 1);
1749 case REANSW
: /* not implemented */
1774 if (!(cp
= *args
++) || *cp
== '-') {
1775 advise (NULL
, "missing argument to %s", args
[-2]);
1781 if (*cp
== '+' || *cp
== '@') {
1782 advise (NULL
, "sorry, no folders allowed!");
1787 advise (NULL
, "only one message at a time!");
1795 vec
[vecp
++] = "-file";
1799 if (!m_convert (mp
, msg
))
1803 if (mp
->numsel
> 1) {
1804 advise (NULL
, "only one message at a time!");
1807 process (mp
->hghsel
, cmd_name
, vecp
, vec
);
1808 seq_setcur (mp
, mp
->hghsel
);
1812 #define RMM_SWITCHES \
1813 X("help", 0, RMHELP) \
1815 #define X(sw, minchars, id) id,
1816 DEFINE_SWITCH_ENUM(RMM
);
1819 #define X(sw, minchars, id) { sw, minchars, id },
1820 DEFINE_SWITCH_ARRAY(RMM
, rmmswit
);
1825 rmmcmd (char **args
)
1827 int msgp
= 0, msgnum
;
1828 char *cp
, buf
[BUFSIZ
], *msgs
[MAXARGS
];
1830 while ((cp
= *args
++)) {
1832 switch (smatch (++cp
, rmmswit
)) {
1834 ambigsw (cp
, rmmswit
);
1837 fprintf (stderr
, "-%s unknown\n", cp
);
1840 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1841 print_help (buf
, rmmswit
, 1);
1844 if (*cp
== '+' || *cp
== '@') {
1845 advise (NULL
, "sorry, no folders allowed!");
1853 msgs
[msgp
++] = "cur";
1854 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
1855 if (!m_convert (mp
, msgs
[msgnum
]))
1866 register int msgnum
, vecp
;
1868 char buffer
[BUFSIZ
], *vec
[MAXARGS
];
1872 if (mp
->numsel
> MAXARGS
- 1) {
1873 advise (NULL
, "more than %d messages for %s exec",
1874 MAXARGS
- 1, rmmproc
);
1878 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1879 if (is_selected (mp
, msgnum
))
1880 vec
[vecp
++] = getcpy (m_name (msgnum
));
1882 forkcmd (vec
, rmmproc
);
1883 for (vecp
= 0; vec
[vecp
]; vecp
++)
1887 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1888 if (is_selected (mp
, msgnum
)) {
1889 strncpy (buffer
, m_backup (cp
= m_name (msgnum
)), sizeof(buffer
));
1890 if (rename (cp
, buffer
) == NOTOK
)
1891 admonish (buffer
, "unable to rename %s to", cp
);
1895 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
1896 if (is_selected (mp
, msgnum
)) {
1897 set_deleted (mp
, msgnum
);
1898 unset_exists (mp
, msgnum
);
1901 if ((mp
->nummsg
-= mp
->numsel
) <= 0) {
1903 admonish (NULL
, "no messages remaining in +%s", fmsh
);
1905 admonish (NULL
, "no messages remaining in %s", mp
->foldpath
);
1906 mp
->lowmsg
= mp
->hghmsg
= mp
->nummsg
= 0;
1908 if (mp
->lowsel
== mp
->lowmsg
) {
1909 for (msgnum
= mp
->lowmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1910 if (does_exist (mp
, msgnum
))
1912 mp
->lowmsg
= msgnum
;
1914 if (mp
->hghsel
== mp
->hghmsg
) {
1915 for (msgnum
= mp
->hghmsg
- 1; msgnum
>= mp
->lowmsg
; msgnum
--)
1916 if (does_exist (mp
, msgnum
))
1918 mp
->hghmsg
= msgnum
;
1921 mp
->msgflags
|= MODIFIED
;
1926 #define SCAN_SWITCHES \
1927 X("clear", 0, SCCLR) \
1928 X("noclear", 0, SCNCLR) \
1929 X("form formatfile", 0, SCFORM) \
1930 X("format string", 5, SCFMT) \
1931 X("header", 0, SCHEAD) \
1932 X("noheader", 0, SCNHEAD) \
1933 X("width columns", 0, SCWID) \
1934 X("help", 0, SCHELP) \
1936 #define X(sw, minchars, id) id,
1937 DEFINE_SWITCH_ENUM(SCAN
);
1940 #define X(sw, minchars, id) { sw, minchars, id },
1941 DEFINE_SWITCH_ARRAY(SCAN
, scanswit
);
1946 scancmd (char **args
)
1948 #define equiv(a,b) (a ? b && !strcmp (a, b) : !b)
1950 int clearsw
= 0, headersw
= 0, width
= 0, msgp
= 0;
1951 int msgnum
, optim
, state
;
1952 char *cp
, *form
= NULL
, *format
= NULL
;
1953 char buf
[BUFSIZ
], *nfs
, *msgs
[MAXARGS
];
1955 static int s_optim
= 0;
1956 static char *s_form
= NULL
, *s_format
= NULL
;
1958 while ((cp
= *args
++)) {
1960 switch (smatch (++cp
, scanswit
)) {
1962 ambigsw (cp
, scanswit
);
1965 fprintf (stderr
, "-%s unknown\n", cp
);
1968 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
1969 print_help (buf
, scanswit
, 1);
1985 if (!(form
= *args
++) || *form
== '-') {
1986 advise (NULL
, "missing argument to %s", args
[-2]);
1992 if (!(format
= *args
++) || *format
== '-') {
1993 advise (NULL
, "missing argument to %s", args
[-2]);
1999 if (!(cp
= *args
++) || *cp
== '-') {
2000 advise (NULL
, "missing argument to %s", args
[-2]);
2006 if (*cp
== '+' || *cp
== '@') {
2007 advise (NULL
, "sorry, no folders allowed!");
2015 msgs
[msgp
++] = "all";
2016 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2017 if (!m_convert (mp
, msgs
[msgnum
]))
2021 /* Get new format string */
2022 nfs
= new_fs (form
, format
, FORMAT
);
2024 /* force scansbr to (re)compile format */
2031 s_optim
= optim
= 1;
2032 s_form
= form
? getcpy (form
) : NULL
;
2033 s_format
= format
? getcpy (format
) : NULL
;
2037 optim
= equiv (s_form
, form
) && equiv (s_format
, format
);
2040 for (msgnum
= mp
->lowsel
;
2041 msgnum
<= mp
->hghsel
&& !interrupted
;
2043 if (is_selected (mp
, msgnum
)) {
2044 if (optim
&& Msgs
[msgnum
].m_scanl
)
2045 printf ("%s", Msgs
[msgnum
].m_scanl
);
2048 zp
= msh_ready (msgnum
, 0);
2049 switch (state
= scan (zp
, msgnum
, 0, nfs
, width
,
2050 msgnum
== mp
->curmsg
,
2051 is_unseen (mp
, msgnum
),
2052 headersw
? (fmsh
? fmsh
: mp
->foldpath
) : NULL
,
2053 fmsh
? 0L : (long) (Msgs
[msgnum
].m_stop
- Msgs
[msgnum
].m_start
),
2059 Msgs
[msgnum
].m_scanl
= getcpy (scanl
);
2063 advise (NULL
, "scan() botch (%d)", state
);
2067 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
2079 #define SHOW_SWITCHES \
2080 X("draft", 5, SHDRAFT) \
2081 X("form formfile", 4, SHFORM) \
2082 X("moreproc program", 4, SHPROG) \
2083 X("nomoreproc", 3, SHNPROG) \
2084 X("length lines", 4, SHLEN) \
2085 X("width columns", 4, SHWID) \
2086 X("showproc program", 4, SHSHOW) \
2087 X("noshowproc", 3, SHNSHOW) \
2088 X("header", 4, SHHEAD) \
2089 X("noheader", 3, SHNHEAD) \
2090 X("help", 0, SHHELP) \
2092 #define X(sw, minchars, id) id,
2093 DEFINE_SWITCH_ENUM(SHOW
);
2096 #define X(sw, minchars, id) { sw, minchars, id },
2097 DEFINE_SWITCH_ARRAY(SHOW
, showswit
);
2102 showcmd (char **args
)
2104 int headersw
= 1, nshow
= 0, msgp
= 0, vecp
= 1;
2105 int mhl
= 0, seqnum
= -1, mode
= 0, i
, msgnum
;
2106 char *cp
, *proc
= showproc
, buf
[BUFSIZ
];
2107 char *msgs
[MAXARGS
], *vec
[MAXARGS
];
2109 if (!strcasecmp (cmd_name
, "next"))
2112 if (!strcasecmp (cmd_name
, "prev"))
2114 while ((cp
= *args
++)) {
2116 switch (i
= smatch (++cp
, showswit
)) {
2118 ambigsw (cp
, showswit
);
2125 snprintf (buf
, sizeof(buf
), "%s %s[switches] [switches for showproc]",
2126 cmd_name
, mode
? NULL
: "[msgs] ");
2127 print_help (buf
, showswit
, 1);
2135 if (!(cp
= *args
++) || *cp
== '-') {
2136 advise (NULL
, "missing argument to %s", args
[-2]);
2148 if (!(proc
= *args
++) || *proc
== '-') {
2149 advise (NULL
, "missing argument to %s", args
[-2]);
2159 advise (NULL
, "sorry, -%s not allowed!", showswit
[i
].sw
);
2162 if (*cp
== '+' || *cp
== '@') {
2163 advise (NULL
, "sorry, no folders allowed!");
2169 "usage: %s [switches] [switches for showproc]\n",
2179 msgs
[msgp
++] = mode
> 0 ? "next" : mode
< 0 ? "prev" : "cur";
2180 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2181 if (!m_convert (mp
, msgs
[msgnum
]))
2186 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2187 if (is_selected (mp
, msgnum
) && is_nontext (msgnum
)) {
2188 proc
= showmimeproc
;
2189 vec
[vecp
++] = "-file";
2197 if (strcmp (showproc
, "mhl") == 0) {
2203 seqnum
= seq_getnum (mp
, "unseen");
2204 vec
[0] = r1bindex (proc
, '/');
2207 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
2208 if (is_selected (mp
, msgnum
)) {
2209 vec
[vecp
++] = getcpy (m_name (msgnum
));
2211 seq_delmsg (mp
, "unseen", msgnum
);
2214 if (mp
->numsel
== 1 && headersw
)
2216 mhlsbr (vecp
, vec
, mhl_action
);
2217 scan_eom_action ((int (*)()) 0);
2222 for (msgnum
= mp
->lowsel
;
2223 msgnum
<= mp
->hghsel
&& !interrupted
;
2225 if (is_selected (mp
, msgnum
)) {
2226 switch (ask (msgnum
)) {
2227 case NOTOK
: /* QUIT */
2234 if (mp
->numsel
== 1 && headersw
)
2237 copy_message (msgnum
, stdout
);
2239 process (msgnum
, proc
, vecp
, vec
);
2242 seq_delmsg (mp
, "unseen", msgnum
);
2249 seq_setcur (mp
, mp
->hghsel
);
2256 if (Msgs
[msgnum
].m_bboard_id
== 0)
2259 printf ("(Message %d", msgnum
);
2260 if (Msgs
[msgnum
].m_bboard_id
> 0)
2261 printf (", %s: %d", BBoard_ID
, Msgs
[msgnum
].m_bboard_id
);
2272 return (ftell (mhlfp
) >= Msgs
[mhlnum
].m_stop
);
2277 mhl_action (char *name
)
2281 if ((msgnum
= m_atoi (name
)) < mp
->lowmsg
2282 || msgnum
> mp
->hghmsg
2283 || !does_exist (mp
, msgnum
))
2287 mhlfp
= msh_ready (msgnum
, 1);
2289 scan_eom_action (eom_action
);
2301 if (mp
->numsel
== 1 || !interactive
|| redirected
)
2304 if (SOprintf ("Press <return> to list \"%d\"...", msgnum
)) {
2305 if (mp
->lowsel
!= msgnum
)
2307 printf ("Press <return> to list \"%d\"...", msgnum
);
2312 read (fileno (stdout
), buf
, sizeof buf
);
2314 if (strchr(buf
, '\n') == NULL
)
2318 told_to_quit
= interrupted
= 0;
2333 is_nontext (int msgnum
)
2337 char buf
[BUFSIZ
], name
[NAMESZ
];
2340 if (Msgs
[msgnum
].m_flags
& MHNCHK
)
2341 return (Msgs
[msgnum
].m_flags
& MHNYES
);
2342 Msgs
[msgnum
].m_flags
|= MHNCHK
;
2344 fp
= msh_ready (msgnum
, 1);
2347 int bufsz
= sizeof buf
;
2348 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
2352 * Check Content-Type field
2354 if (!strcasecmp (name
, TYPE_FIELD
)) {
2358 cp
= add (buf
, NULL
);
2359 while (state
== FLDPLUS
) {
2361 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2368 for (; isspace ((unsigned char) *bp
); bp
++)
2373 for (bp
++, i
= 0;;) {
2403 for (dp
= bp
; istoken (*dp
); dp
++)
2410 if ((result
= (strcasecmp (bp
, "plain") != 0)))
2413 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
2416 if ((result
= !uprf (dp
, "charset")))
2418 dp
+= sizeof "charset" - 1;
2419 while (isspace ((unsigned char) *dp
))
2423 while (isspace ((unsigned char) *dp
))
2426 if ((bp
= strchr(++dp
, '"')))
2429 for (bp
= dp
; *bp
; bp
++)
2430 if (isspace ((unsigned char) *bp
)) {
2436 /* Default character set */
2439 /* Check the character set */
2440 result
= !check_charset (dp
, strlen (dp
));
2442 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
2452 Msgs
[msgnum
].m_flags
|= MHNYES
;
2459 * Check Content-Transfer-Encoding field
2461 if (!strcasecmp (name
, ENCODING_FIELD
)) {
2462 cp
= add (buf
, NULL
);
2463 while (state
== FLDPLUS
) {
2465 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2468 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
2470 for (dp
= bp
; istoken (*dp
); dp
++)
2473 result
= (strcasecmp (bp
, "7bit")
2474 && strcasecmp (bp
, "8bit")
2475 && strcasecmp (bp
, "binary"));
2479 Msgs
[msgnum
].m_flags
|= MHNYES
;
2486 * Just skip the rest of this header
2487 * field and go to next one.
2489 while (state
== FLDPLUS
) {
2491 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
2496 * We've passed the message header,
2497 * so message is just text.
2506 #define SORT_SWITCHES \
2507 X("datefield field", 0, SODATE) \
2508 X("textfield field", 0, SOSUBJ) \
2509 X("notextfield", 0, SONSUBJ) \
2510 X("limit days", 0, SOLIMT) \
2511 X("nolimit", 0, SONLIMT) \
2512 X("verbose", 0, SOVERB) \
2513 X("noverbose", 0, SONVERB) \
2514 X("help", 0, SOHELP) \
2516 #define X(sw, minchars, id) id,
2517 DEFINE_SWITCH_ENUM(SORT
);
2520 #define X(sw, minchars, id) { sw, minchars, id },
2521 DEFINE_SWITCH_ARRAY(SORT
, sortswit
);
2526 sortcmd (char **args
)
2528 int msgp
= 0, msgnum
;
2529 char *cp
, *datesw
= NULL
, *subjsw
= NULL
;
2530 char buf
[BUFSIZ
], *msgs
[MAXARGS
];
2534 forkcmd (args
, cmd_name
);
2538 while ((cp
= *args
++)) {
2540 switch (smatch (++cp
, sortswit
)) {
2542 ambigsw (cp
, sortswit
);
2545 fprintf (stderr
, "-%s unknown\n", cp
);
2548 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches]", cmd_name
);
2549 print_help (buf
, sortswit
, 1);
2554 advise (NULL
, "only one date field at a time!");
2557 if (!(datesw
= *args
++) || *datesw
== '-') {
2558 advise (NULL
, "missing argument to %s", args
[-2]);
2565 advise (NULL
, "only one text field at a time!");
2568 if (!(subjsw
= *args
++) || *subjsw
== '-') {
2569 advise (NULL
, "missing argument to %s", args
[-2]);
2577 case SOLIMT
: /* too hard */
2578 if (!(cp
= *args
++) || *cp
== '-') {
2579 advise (NULL
, "missing argument to %s", args
[-2]);
2583 case SOVERB
: /* not implemented */
2587 if (*cp
== '+' || *cp
== '@') {
2588 advise (NULL
, "sorry, no folders allowed!");
2596 msgs
[msgp
++] = "all";
2599 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
2600 if (!m_convert (mp
, msgs
[msgnum
]))
2604 twscopy (&tb
, dlocaltimenow ());
2606 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2607 if (Msgs
[msgnum
].m_scanl
) {
2608 free (Msgs
[msgnum
].m_scanl
);
2609 Msgs
[msgnum
].m_scanl
= NULL
;
2611 if (is_selected (mp
, msgnum
)) {
2612 if (get_fields (datesw
, subjsw
, msgnum
, &Msgs
[msgnum
]))
2613 twscopy (&Msgs
[msgnum
].m_tb
,
2614 msgnum
!= mp
->lowsel
? &Msgs
[msgnum
- 1].m_tb
: &tb
);
2616 else /* m_scaln is already NULL */
2617 twscopy (&Msgs
[msgnum
].m_tb
, &tb
);
2618 Msgs
[msgnum
].m_stats
= bvector_create (0);
2619 bvector_copy (Msgs
[msgnum
].m_stats
, msgstat (mp
, msgnum
));
2620 if (mp
->curmsg
== msgnum
)
2621 bvector_set (Msgs
[msgnum
].m_stats
, CUR
);
2624 qsort ((char *) &Msgs
[mp
->lowsel
], mp
->hghsel
- mp
->lowsel
+ 1,
2625 sizeof(struct Msg
), (qsort_comp
) (subjsw
? subsort
: msgsort
));
2627 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
2628 if (subjsw
&& Msgs
[msgnum
].m_scanl
) {
2629 free (Msgs
[msgnum
].m_scanl
); /* from subjsort */
2630 Msgs
[msgnum
].m_scanl
= NULL
;
2632 bvector_clear (Msgs
[msgnum
].m_stats
, CUR
);
2633 bvector_copy (msgstat (mp
, msgnum
), Msgs
[msgnum
].m_stats
);
2634 if (bvector_at (Msgs
[msgnum
].m_stats
, CUR
))
2635 seq_setcur (mp
, msgnum
);
2638 mp
->msgflags
|= MODIFIED
;
2644 * get_fields - parse message, and get date and subject if needed.
2645 * We'll use the msgp->m_tb tws struct for the date, and overload
2646 * the msgp->m_scanl field with our subject string.
2649 get_fields (char *datesw
, char *subjsw
, int msgnum
, struct Msg
*msgp
)
2651 int state
, gotdate
= 0;
2652 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
2653 struct tws
*tw
= (struct tws
*) 0;
2656 zp
= msh_ready (msgnum
, 0);
2659 int bufsz
= sizeof buf
;
2660 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
2663 if (!strcasecmp (name
, datesw
)) {
2665 while (state
== FLDPLUS
) {
2667 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2670 if ((tw
= dparsetime (bp
)) == NULL
)
2672 "unable to parse %s field in message %d",
2675 twscopy (&(msgp
->m_tb
), tw
);
2677 if (!subjsw
) /* not using this, or already done */
2678 break; /* all done! */
2681 else if (subjsw
&& !strcasecmp(name
, subjsw
)) {
2683 while (state
== FLDPLUS
) {
2685 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2688 msgp
->m_scanl
= sosmash(subjsw
, bp
);
2690 break; /* date done so we're done */
2692 subjsw
= (char *)0;/* subject done, need date */
2694 while (state
== FLDPLUS
) { /* flush this one */
2696 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
2707 admonish (NULL
, "format error in message %d", msgnum
);
2708 if (msgp
->m_scanl
) { /* this might need free'd */
2709 free (msgp
->m_scanl
); /* probably can't use subj anyway */
2710 msgp
->m_scanl
= NULL
;
2715 adios (NULL
, "internal error -- you lose");
2720 return OK
; /* not an error if subj not found */
2722 admonish (NULL
, "no %s field in message %d", datesw
, msgnum
);
2723 return NOTOK
; /* NOTOK means use some other date */
2732 msgsort (struct Msg
*a
, struct Msg
*b
)
2734 return twsort (&a
->m_tb
, &b
->m_tb
);
2739 subsort (struct Msg
*a
, struct Msg
*b
)
2743 if (a
->m_scanl
&& b
->m_scanl
)
2744 if ((i
= strcmp (a
->m_scanl
, b
->m_scanl
)))
2747 return twsort (&a
->m_tb
, &b
->m_tb
);
2752 * try to make the subject "canonical": delete leading "re:", everything
2753 * but letters & smash letters to lower case.
2756 sosmash (char *subj
, char *s
)
2758 register char *cp
, *dp
;
2759 register unsigned char c
;
2763 dp
= s
; /* dst pointer */
2764 if (!strcasecmp (subj
, "subject"))
2771 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2777 while ((c
= *cp
++)) {
2779 *dp
++ = isupper(c
) ? tolower(c
) : c
;
2789 process (int msgnum
, char *proc
, int vecp
, char **vec
)
2791 int child_id
, status
;
2792 char tmpfil
[BUFSIZ
];
2797 strncpy (tmpfil
, m_name (msgnum
), sizeof(tmpfil
));
2798 context_del (pfolder
);
2799 context_replace (pfolder
, fmsh
);/* update current folder */
2801 context_save (); /* save the context file */
2805 cp
= m_mktemp(invo_name
, NULL
, &out
);
2807 /* Try again, but try to create under /tmp */
2809 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
2812 advise (NULL
, "unable to create temporary file");
2816 copy_message (msgnum
, out
);
2818 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
2822 switch (child_id
= fork ()) {
2824 advise ("fork", "unable to");
2830 SIGNAL (SIGINT
, istat
);
2831 SIGNAL (SIGQUIT
, qstat
);
2833 vec
[vecp
++] = tmpfil
;
2837 fprintf (stderr
, "unable to exec ");
2842 status
= pidXwait (child_id
, NULL
);
2853 copy_message (int msgnum
, FILE *out
)
2856 static char buffer
[BUFSIZ
];
2859 zp
= msh_ready (msgnum
, 1);
2861 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
) {
2862 fputs (buffer
, out
);
2863 if (interrupted
&& out
== stdout
)
2869 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2870 && pos
< Msgs
[msgnum
].m_stop
) {
2871 fputs (buffer
, out
);
2872 pos
+= (long) strlen (buffer
);
2873 if (interrupted
&& out
== stdout
)
2881 copy_digest (int msgnum
, FILE *out
)
2885 static char buffer
[BUFSIZ
];
2889 zp
= msh_ready (msgnum
, 1);
2892 while (fgets (buffer
, sizeof buffer
, zp
) != NULL
2893 && !fmsh
&& pos
< Msgs
[msgnum
].m_stop
) {
2894 if (c
== '\n' && *buffer
== '-')
2896 fputs (buffer
, out
);
2897 c
= buffer
[strlen (buffer
) - 1];
2899 pos
+= (long) strlen (buffer
);
2900 if (interrupted
&& out
== stdout
)