]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
d41413898e3cc23fcab13e9f4bcfbd2878b5734b
3 * whatnowsbr.c -- the WhatNow shell
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.
9 * Several options have been added to ease the inclusion of attachments
10 * using the header field name mechanism added to anno and send. The
11 * -attach option is used to specify the header field name for attachments.
13 * Several commands have been added at the whatnow prompt:
15 * cd [ directory ] This option works just like the shell's
16 * cd command and lets the user change the
17 * directory from which attachments are
18 * taken so that long path names are not
19 * needed with every file.
21 * ls [ ls-options ] This option works just like the normal
22 * ls command and exists to allow the user
23 * to verify file names in the directory.
25 * pwd This option works just like the normal
26 * pwd command and exists to allow the user
27 * to verify the directory.
29 * attach [-v] files This option attaches the named files to
30 * the draft. -v displays the mhbuild
31 * directive that send(1) will use.
33 * alist [-ln] This option lists the attachments on the
34 * draft. -l gets long listings, -n gets
37 * detach files This option removes attachments from the
38 * detach -n numbers draft. This can be done by file name or
39 * by attachment number.
47 #define WHATNOW_SWITCHES \
48 X("draftfolder +folder", 0, DFOLDSW) \
49 X("draftmessage msg", 0, DMSGSW) \
50 X("nodraftfolder", 0, NDFLDSW) \
51 X("editor editor", 0, EDITRSW) \
52 X("noedit", 0, NEDITSW) \
53 X("prompt string", 4, PRMPTSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("attach header-field-name", 0, ATTACHSW) \
57 X("noattach", 0, NOATTACHSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(WHATNOW
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
68 * Options at the "whatnow" prompt
70 #define PROMPT_SWITCHES \
71 X("edit [<editor> <switches>]", 0, EDITSW) \
72 X("refile [<switches>] +folder", 0, REFILEOPT) \
73 X("mime [<switches>]", 0, BUILDMIMESW) \
74 X("display [<switches>]", 0, DISPSW) \
75 X("list [<switches>]", 0, LISTSW) \
76 X("send [<switches>]", 0, SENDSW) \
77 X("push [<switches>]", 0, PUSHSW) \
78 X("whom [<switches>]", 0, WHOMSW) \
79 X("quit [-delete]", 0, QUITSW) \
80 X("delete", 0, DELETESW) \
81 X("cd [directory]", 0, CDCMDSW) \
82 X("pwd", 0, PWDCMDSW) \
84 X("attach [-v]", 0, ATTACHCMDSW) \
85 X("detach [-n]", 0, DETACHCMDSW) \
86 X("alist [-ln] ", 2, ALISTCMDSW) \
88 #define X(sw, minchars, id) id,
89 DEFINE_SWITCH_ENUM(PROMPT
);
92 #define X(sw, minchars, id) { sw, minchars, id },
93 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
96 static char *myprompt
= "\nWhat now? ";
101 static int editfile (char **, char **, char *, int, struct msgs
*,
102 char *, char *, int, int);
103 static int sendfile (char **, char *, int);
104 static void sendit (char *, char **, char *, int);
105 static int buildfile (char **, char *);
106 static int check_draft (char *);
107 static int whomfile (char **, char *);
108 static int removefile (char *);
109 static void writelscmd(char *, int, char *, char **);
110 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
111 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
112 static int system_in_dir(const char *dir
, const char *cmd
);
113 static int copyf (char *, char *);
117 WhatNow (int argc
, char **argv
)
119 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
120 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
121 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
122 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
123 char **argp
, **arguments
;
125 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
126 char cwd
[PATH_MAX
+ 1]; /* current working directory */
127 char file
[PATH_MAX
+ 1]; /* file name buffer */
128 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
129 FILE *f
; /* read pointer for bgnd proc */
130 char *l
; /* set on -l to alist command */
131 int n
; /* set on -n to alist command */
133 invo_name
= r1bindex (argv
[0], '/');
135 /* read user profile/context */
138 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 * Get the initial current working directory.
145 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
146 adios("getcwd", "could not get working directory");
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, whatnowswitches
)) {
153 ambigsw (cp
, whatnowswitches
);
156 adios (NULL
, "-%s unknown", cp
);
159 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
160 print_help (buf
, whatnowswitches
, 1);
163 print_version(invo_name
);
168 adios (NULL
, "only one draft folder at a time!");
169 if (!(cp
= *argp
++) || *cp
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
171 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
172 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
176 adios (NULL
, "only one draft message at a time!");
177 if (!(dmsg
= *argp
++) || *dmsg
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
186 if (!(ed
= *argp
++) || *ed
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if (!(myprompt
= *argp
++) || *myprompt
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
200 if (!(attach
= *argp
++) || *attach
== '-')
201 adios (NULL
, "missing argument to %s", argp
[-2]);
210 adios (NULL
, "only one draft at a time!");
215 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
216 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
218 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
220 if ((cp
= getenv ("mhatfile")) && *cp
)
223 if ((cp
= getenv ("mhuse")) && *cp
)
226 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
231 /* start editing the draft, unless -noedit was given */
232 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
233 NULL
, 1, atfile
) < 0)
236 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
238 #ifdef READLINE_SUPPORT
239 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
240 #else /* ! READLINE_SUPPORT */
241 if (!(argp
= getans (prompt
, aleqs
))) {
242 #endif /* READLINE_SUPPORT */
246 switch (smatch (*argp
, aleqs
)) {
248 /* display the message being replied to, or distributed */
250 showfile (++argp
, msgnam
);
252 advise (NULL
, "no alternate message to display");
256 /* Translate MIME composition file */
257 buildfile (++argp
, drft
);
261 /* Call an editor on the draft file */
264 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
265 NULL
, 1, atfile
) == NOTOK
)
270 /* display the draft file */
271 showfile (++argp
, drft
);
275 /* Check to whom the draft would be sent */
276 whomfile (++argp
, drft
);
280 /* Quit, and possibly delete the draft */
281 if (*++argp
&& (*argp
[0] == 'd' ||
282 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
285 if (stat (drft
, &st
) != NOTOK
)
286 advise (NULL
, "draft left on %s", drft
);
291 /* Delete draft and exit */
296 /* Send draft in background */
297 if (sendfile (++argp
, drft
, 1))
303 sendfile (++argp
, drft
, 0);
307 /* Refile the draft */
308 if (refile (++argp
, drft
) == 0)
313 /* Change the working directory for attachments
315 * Run the directory through the user's shell so that
316 * we can take advantage of any syntax that the user
317 * is accustomed to. Read back the absolute path.
320 if (*(argp
+1) == (char *)0) {
321 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
324 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
326 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
327 fgets(cwd
, sizeof (cwd
), f
);
329 if (strchr(cwd
, '\n') != (char *)0)
330 *strchr(cwd
, '\n') = '\0';
335 advise("popen", "could not get directory");
341 /* Print the working directory for attachments */
346 /* List files in the current attachment working directory
348 * Use the user's shell so that we can take advantage of any
349 * syntax that the user is accustomed to.
351 writelscmd(buf
, sizeof(buf
), "", argp
);
352 (void)system_in_dir(cwd
, buf
);
357 * List attachments on current draft. Options are:
359 * -l long listing (full path names)
363 if (attach
== (char *)0) {
364 advise((char *)0, "can't list because no header field name was given.");
371 while (*++argp
!= (char *)0) {
372 if (strcmp(*argp
, "-l") == 0)
375 else if (strcmp(*argp
, "-n") == 0)
378 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
390 advise((char *)0, "usage is alist [-ln].");
393 annolist(drft
, attach
, l
, n
);
399 * Attach files to current draft.
405 if (attach
== (char *)0) {
406 advise((char *)0, "can't attach because no header field name was given.");
410 for (ap
= argp
+1; *ap
; ++ap
) {
411 if (strcmp(*ap
, "-v") == 0) {
414 } else if (*ap
[0] != '-') {
419 if (*(argp
+1) == (char *)0) {
420 advise(NULL
, "attach command requires file argument(s).");
425 * Build a command line that causes the user's shell to list the file name
426 * arguments. This handles and wildcard expansion, tilde expansion, etc.
428 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
431 * Read back the response from the shell, which contains a number of lines
432 * with one file name per line. Remove off the newline. Determine whether
433 * we have an absolute or relative path name. Prepend the current working
434 * directory to relative path names. Add the attachment annotation to the
438 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
439 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
440 char *build_directive
;
442 *(strchr(shell
, '\n')) = '\0';
445 (void)annotate(drft
, attach
, shell
, 1, 0, -2, 1);
448 construct_build_directive (shell
, NULL
, 1);
451 (void)sprintf(file
, "%s/%s", cwd
, shell
);
452 (void)annotate(drft
, attach
, file
, 1, 0, -2, 1);
455 construct_build_directive (file
, NULL
, 1);
460 printf ("%s", build_directive
);
461 free (build_directive
);
468 advise("popen", "could not get file from shell");
475 * Detach files from current draft.
478 if (attach
== (char *)0) {
479 advise((char *)0, "can't detach because no header field name was given.");
484 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
485 * so this catches a -n anywhere in the argument list.
488 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
489 if (strcmp(*arguments
, "-n") == 0) {
496 * A -n was found so interpret the arguments as attachment numbers.
497 * Decrement any remaining argument number that is greater than the one
498 * just processed after processing each one so that the numbering stays
503 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
504 if (strcmp(*arguments
, "-n") == 0)
507 if (**arguments
!= '\0') {
508 n
= atoi(*arguments
);
509 (void)annotate(drft
, attach
, (char *)0, 1, 0, n
, 1);
511 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
512 if (atoi(*argp
) > n
) {
513 if (atoi(*argp
) == 1)
516 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
524 * The arguments are interpreted as file names. Run them through the
525 * user's shell for wildcard expansion and other goodies. Do this from
526 * the current working directory if the argument is not an absolute path
527 * name (does not begin with a /).
529 * We feed all the file names to the shell at once, otherwise you can't
530 * provide a file name with a space in it.
532 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
533 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
534 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
535 *(strchr(shell
, '\n')) = '\0';
536 (void)annotate(drft
, attach
, shell
, 1, 0, 0, 1);
540 advise("popen", "could not get file from shell");
546 /* Unknown command */
547 advise (NULL
, "say what?");
556 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
558 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
561 /* Note that we do not quote -- the argp from the user
562 * is assumed to be quoted as they desire. (We can't treat
563 * it as pure literal as that would prevent them using ~,
564 * wildcards, etc.) The buffer produced by this function
565 * should be given to popen_in_dir() or system_in_dir() so
566 * that the current working directory is set correctly.
568 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
569 /* NB that some snprintf() return -1 on overflow rather than the
570 * new C99 mandated 'number of chars that would have been written'
572 /* length checks here and inside the loop allow for the
573 * trailing "&&", trailcmd, '"' and NUL
575 int trailln
= strlen(trailcmd
) + 4;
576 if (ln
< 0 || ln
+ trailln
> bufsz
)
577 adios((char *)0, "arguments too long");
581 while (*argp
&& *++argp
) {
583 /* +1 for leading space */
584 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
585 adios((char *)0, "arguments too long");
587 memcpy(cp
, *argp
, ln
+1);
591 *cp
++ = '&'; *cp
++ = '&';
592 strcpy(cp
, trailcmd
);
600 * Build a command line that causes the user's shell to list the file name
601 * arguments. This handles and wildcard expansion, tilde expansion, etc.
604 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
606 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
607 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
611 /* Like system(), but run the command in directory dir.
612 * This assumes the program is single-threaded!
615 system_in_dir(const char *dir
, const char *cmd
)
620 /* ensure that $SHELL exists, as the cmd was written relying on
621 a non-blank $SHELL... */
622 setenv("SHELL","/bin/sh",0); /* don't overwrite */
624 if (getcwd(olddir
, sizeof(olddir
)) == 0)
625 adios("getcwd", "could not get working directory");
627 adios("chdir", "could not change working directory");
629 if (chdir(olddir
) != 0)
630 adios("chdir", "could not change working directory");
634 /* ditto for popen() */
636 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
641 /* ensure that $SHELL exists, as the cmd was written relying on
642 a non-blank $SHELL... */
643 setenv("SHELL","/bin/sh",0); /* don't overwrite */
645 if (getcwd(olddir
, sizeof(olddir
)) == 0)
646 adios("getcwd", "could not get working directory");
648 adios("chdir", "could not change working directory");
649 f
= popen(cmd
, type
);
650 if (chdir(olddir
) != 0)
651 adios("chdir", "could not change working directory");
660 static int reedit
= 0; /* have we been here before? */
661 static char *edsave
= NULL
; /* the editor we used previously */
665 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
666 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
668 int pid
, status
, vecp
;
669 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
670 char *cp
, *prog
, **vec
;
675 /* Was there a previous edit session? */
677 if (!*ed
) { /* no explicit editor */
678 *ed
= edsave
; /* so use the previous one */
679 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
682 /* unless we've specified it via "editor-next" */
683 cp
= concat (cp
, "-next", NULL
);
684 if ((cp
= context_find (cp
)) != NULL
)
688 /* set initial editor */
690 *ed
= get_default_editor();
694 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
695 strncpy (altpath
, altmsg
, sizeof(altpath
));
697 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
699 strncpy (linkpath
, LINK
, sizeof(linkpath
));
701 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
705 if (link (altpath
, linkpath
) == NOTOK
) {
706 symlink (altpath
, linkpath
);
714 context_save (); /* save the context file */
717 switch (pid
= fork()) {
719 advise ("fork", "unable to");
728 m_putenv ("mhfolder", mp
->foldpath
);
729 m_putenv ("editalt", altpath
);
732 vec
= argsplit(*ed
, &prog
, &vecp
);
736 vec
[vecp
++] = *arg
++;
741 fprintf (stderr
, "unable to exec ");
746 if ((status
= pidwait (pid
, NOTOK
))) {
747 if (((status
& 0xff00) != 0xff00)
748 && (!reedit
|| (status
& 0x00ff))) {
749 if (!use
&& (status
& 0xff00) &&
750 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
751 advise (NULL
, "problems with edit--draft left in %s", cp
);
753 advise (NULL
, "problems with edit--%s preserved", file
);
756 status
= -2; /* maybe "reedit ? -2 : -1"? */
765 ? lstat (linkpath
, &st
) != NOTOK
766 && S_ISREG(st
.st_mode
)
767 && copyf (linkpath
, altpath
) == NOTOK
768 : stat (linkpath
, &st
) != NOTOK
770 && (unlink (altpath
) == NOTOK
771 || link (linkpath
, altpath
) == NOTOK
)))
772 advise (linkpath
, "unable to update %s from", altmsg
);
775 /* normally, we remember which editor we used */
777 edsave
= getcpy (*ed
);
780 if (altmsg
&& atfile
)
788 copyf (char *ifile
, char *ofile
)
793 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
795 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
796 admonish (ofile
, "unable to open and truncate");
801 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
802 if (write (out
, buffer
, i
) != i
) {
803 advise (ofile
, "may have damaged");
819 sendfile (char **arg
, char *file
, int pushsw
)
823 char *cp
, *sp
, **vec
, *program
;
825 /* Translate MIME composition file, if necessary */
826 if ((cp
= context_find ("automimeproc"))
827 && (!strcmp (cp
, "1"))
828 && check_draft (file
)
829 && (buildfile (NULL
, file
) == NOTOK
))
832 /* For backwards compatibility */
833 if ((cp
= context_find ("automhnproc"))
834 && check_draft (file
)
835 && (i
= editfile (&cp
, NULL
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0)))
839 * If the sendproc is the nmh command `send', then we call
840 * those routines directly rather than exec'ing the command.
842 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
844 sendit (invo_name
= sp
, arg
, file
, pushsw
);
849 context_save (); /* save the context file */
852 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
856 advise (NULL
, "unable to fork, so sending directly...");
858 vec
= argsplit(sendproc
, &program
, &vecp
);
860 vec
[vecp
++] = "-push";
863 vec
[vecp
++] = *arg
++;
867 execvp (program
, vec
);
868 fprintf (stderr
, "unable to exec ");
873 if (pidwait(child_id
, OK
) == 0)
881 * Translate MIME composition file (call buildmimeproc)
885 buildfile (char **argp
, char *file
)
892 /* allocate space for arguments */
898 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
901 * For backward compatibility, we need to add -build
902 * if we are using mhn as buildmimeproc
905 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
906 args
[i
++] = "-build";
908 /* copy any other arguments */
909 while (argp
&& *argp
)
913 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
916 return (i
? NOTOK
: OK
);
921 * Check if draft is a mhbuild composition file
925 check_draft (char *msgnam
)
928 char buf
[BUFSIZ
], name
[NAMESZ
];
930 m_getfld_state_t gstate
= 0;
932 if ((fp
= fopen (msgnam
, "r")) == NULL
)
935 int bufsz
= sizeof buf
;
936 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
940 * If draft already contains any of the
941 * Content-XXX fields, then assume it already
944 if (uprf (name
, XXX_FIELD_PRF
)) {
946 m_getfld_state_destroy (&gstate
);
949 while (state
== FLDPLUS
) {
951 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
959 for (bp
= buf
; *bp
; bp
++)
960 if (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n') {
962 m_getfld_state_destroy (&gstate
);
967 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
968 } while (state
== BODY
);
973 m_getfld_state_destroy (&gstate
);
981 # define SASLminc(a) (a)
982 #else /* CYRUS_SASL */
983 # define SASLminc(a) 0
984 #endif /* CYRUS_SASL */
987 # define TLSminc(a) (a)
988 #else /* TLS_SUPPORT */
989 # define TLSminc(a) 0
990 #endif /* TLS_SUPPORT */
992 #define SEND_SWITCHES \
993 X("alias aliasfile", 0, ALIASW) \
994 X("debug", -5, DEBUGSW) \
995 X("filter filterfile", 0, FILTSW) \
996 X("nofilter", 0, NFILTSW) \
997 X("format", 0, FRMTSW) \
998 X("noformat", 0, NFRMTSW) \
999 X("forward", 0, FORWSW) \
1000 X("noforward", 0, NFORWSW) \
1001 X("mime", 0, MIMESW) \
1002 X("nomime", 0, NMIMESW) \
1003 X("msgid", 0, MSGDSW) \
1004 X("nomsgid", 0, NMSGDSW) \
1005 X("push", 0, SPSHSW) \
1006 X("nopush", 0, NSPSHSW) \
1007 X("split seconds", 0, SPLITSW) \
1008 X("unique", -6, UNIQSW) \
1009 X("nounique", -8, NUNIQSW) \
1010 X("verbose", 0, VERBSW) \
1011 X("noverbose", 0, NVERBSW) \
1012 X("watch", 0, WATCSW) \
1013 X("nowatch", 0, NWATCSW) \
1014 X("width columns", 0, WIDTHSW) \
1015 X("version", 0, SVERSIONSW) \
1016 X("help", 0, SHELPSW) \
1017 X("dashstuffing", -12, BITSTUFFSW) \
1018 X("nodashstuffing", -14, NBITSTUFFSW) \
1019 X("client host", -6, CLIESW) \
1020 X("server host", 6, SERVSW) \
1021 X("snoop", -5, SNOOPSW) \
1022 X("draftfolder +folder", -6, SDRFSW) \
1023 X("draftmessage msg", -6, SDRMSW) \
1024 X("nodraftfolder", -3, SNDRFSW) \
1025 X("sasl", SASLminc(-4), SASLSW) \
1026 X("nosasl", SASLminc(-6), NOSASLSW) \
1027 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
1028 X("saslmech", SASLminc(-5), SASLMECHSW) \
1029 X("user", SASLminc(-4), USERSW) \
1030 X("attach fieldname", 6, SNDATTACHSW) \
1031 X("noattach", 0, SNDNOATTACHSW) \
1032 X("attachformat", 7, SNDATTACHFORMAT) \
1033 X("port server-port-name/number", 4, PORTSW) \
1034 X("tls", TLSminc(-3), TLSSW) \
1035 X("initialtls", TLSminc(-10), INITTLSSW) \
1036 X("notls", TLSminc(-5), NTLSSW) \
1037 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
1038 X("messageid localname|random", 2, MESSAGEIDSW) \
1040 #define X(sw, minchars, id) id,
1041 DEFINE_SWITCH_ENUM(SEND
);
1044 #define X(sw, minchars, id) { sw, minchars, id },
1045 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
1049 extern int debugsw
; /* from sendsbr.c */
1057 extern char *altmsg
; /* .. */
1058 extern char *annotext
;
1059 extern char *distfile
;
1063 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1066 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1067 char **arguments
, *savearg
[MAXARGS
], **vec
;
1069 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
1070 int attachformat
= 1; /* mhbuild format specifier for
1078 * Make sure these are defined. In particular, we need
1079 * savearg[1] to be NULL, in case "arg" is NULL below. It
1080 * doesn't matter what is the value of savearg[0], but we
1081 * set it to NULL, to help catch "off-by-one" errors.
1087 * Temporarily copy arg to savearg, since the brkstring() call in
1088 * getarguments() will wipe it out before it is merged in.
1089 * Also, we skip the first element of savearg, since getarguments()
1090 * skips it. Then we count the number of arguments
1091 * copied. The value of "n" will be one greater than
1092 * this in order to simulate the standard argc/argv.
1097 copyip (arg
, savearg
+1, MAXARGS
-1);
1104 * Merge any arguments from command line (now in savearg)
1105 * and arguments from profile.
1107 arguments
= getarguments (sp
, n
, savearg
, 1);
1120 * Get our initial arguments for postproc now
1123 vec
= argsplit(postproc
, &program
, &vecp
);
1125 vec
[vecp
++] = "-library";
1126 vec
[vecp
++] = getcpy (m_maildir (""));
1128 if ((cp
= context_find ("fileproc"))) {
1129 vec
[vecp
++] = "-fileproc";
1133 if ((cp
= context_find ("mhlproc"))) {
1134 vec
[vecp
++] = "-mhlproc";
1138 if ((cp
= context_find ("credentials"))) {
1139 /* post doesn't read context so need to pass credentials. */
1140 vec
[vecp
++] = "-credentials";
1144 while ((cp
= *argp
++)) {
1146 switch (smatch (++cp
, sendswitches
)) {
1148 ambigsw (cp
, sendswitches
);
1151 advise (NULL
, "-%s unknown\n", cp
);
1155 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1156 print_help (buf
, sendswitches
, 1);
1159 print_version (invo_name
);
1170 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1171 advise (NULL
, "missing argument to %s", argp
[-2]);
1199 debugsw
++; /* fall */
1232 if (!(cp
= *argp
++) || *cp
== '-') {
1233 advise (NULL
, "missing argument to %s", argp
[-2]);
1241 if (!(cp
= *argp
++) || *cp
== '-') {
1242 advise (NULL
, "missing argument to %s", argp
[-2]);
1249 if (!(attach
= *argp
++) || *attach
== '-') {
1250 advise (NULL
, "missing argument to %s", argp
[-2]);
1258 case SNDATTACHFORMAT
:
1259 if (! *argp
|| **argp
== '-')
1260 adios (NULL
, "missing argument to %s", argp
[-1]);
1262 attachformat
= atoi (*argp
);
1263 if (attachformat
< 0 ||
1264 attachformat
> ATTACHFORMATS
- 1) {
1265 advise (NULL
, "unsupported attachformat %d",
1274 advise (NULL
, "usage: %s [switches]", sp
);
1278 /* allow Aliasfile: profile entry */
1279 if ((cp
= context_find ("Aliasfile"))) {
1283 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1284 vec
[vecp
++] = "-alias";
1289 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1290 if ((cp
= context_find ("signature")) && *cp
)
1291 m_putenv ("SIGNATURE", cp
);
1293 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1295 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1297 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1298 inplace
= atoi (cp
);
1300 if ((cp
= getenv ("mhdist"))
1303 && (distsw
= atoi (cp
))
1304 #endif /* not lint */
1306 vec
[vecp
++] = "-dist";
1307 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1308 adios(NULL
, "unable to create temporary file in %s",
1311 distfile
= getcpy (cp
);
1313 if (link (altmsg
, distfile
) == NOTOK
)
1314 adios (distfile
, "unable to link %s to", altmsg
);
1319 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1324 if ((pushsw
= pushed
))
1329 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, attach
, attachformat
) == OK
)
1338 whomfile (char **arg
, char *file
)
1342 char **vec
, *program
;
1344 context_save (); /* save the context file */
1347 switch (pid
= fork()) {
1349 advise ("fork", "unable to");
1353 vec
= argsplit(whomproc
, &program
, &vecp
);
1357 vec
[vecp
++] = *arg
++;
1360 execvp (program
, vec
);
1361 fprintf (stderr
, "unable to exec ");
1363 _exit (-1); /* NOTREACHED */
1366 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1372 * Remove the draft file
1376 removefile (char *drft
)
1378 if (unlink (drft
) == NOTOK
)
1379 adios (drft
, "unable to unlink");