]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
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", -6, ATTACHSW) \
57 X("noattach", -8, NOATTACHSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(WHATNOW
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
69 * Options at the "whatnow" prompt
71 #define PROMPT_SWITCHES \
72 X("edit [<editor> <switches>]", 0, EDITSW) \
73 X("refile [<switches>] +folder", 0, REFILEOPT) \
74 X("mime [<switches>]", 0, BUILDMIMESW) \
75 X("display [<switches>]", 0, DISPSW) \
76 X("list [<switches>]", 0, LISTSW) \
77 X("send [<switches>]", 0, SENDSW) \
78 X("push [<switches>]", 0, PUSHSW) \
79 X("whom [<switches>]", 0, WHOMSW) \
80 X("quit [-delete]", 0, QUITSW) \
81 X("delete", 0, DELETESW) \
82 X("cd [directory]", 0, CDCMDSW) \
83 X("pwd", 0, PWDCMDSW) \
85 X("attach [-v]", 0, ATTACHCMDSW) \
86 X("detach [-n]", 0, DETACHCMDSW) \
87 X("alist [-ln] ", 2, ALISTCMDSW) \
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(PROMPT
);
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
97 static char *myprompt
= "\nWhat now? ";
102 static int editfile (char **, char **, char *, int, struct msgs
*,
103 char *, char *, int, int);
104 static int sendfile (char **, char *, int);
105 static void sendit (char *, char **, char *, int);
106 static int buildfile (char **, char *);
107 static int check_draft (char *);
108 static int whomfile (char **, char *);
109 static int removefile (char *);
110 static void writelscmd(char *, int, char *, char **);
111 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
112 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
113 static int system_in_dir(const char *dir
, const char *cmd
);
114 static int copyf (char *, char *);
118 WhatNow (int argc
, char **argv
)
120 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
121 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
122 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
123 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
124 char **argp
, **arguments
;
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 advise(NULL
, "The -attach switch is deprecated");
204 advise(NULL
, "The -noattach switch is deprecated");
209 adios (NULL
, "only one draft at a time!");
214 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
215 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
217 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
219 if ((cp
= getenv ("mhatfile")) && *cp
)
222 if ((cp
= getenv ("mhuse")) && *cp
)
225 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
230 /* start editing the draft, unless -noedit was given */
231 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
232 NULL
, 1, atfile
) < 0)
235 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
237 #ifdef READLINE_SUPPORT
238 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
239 #else /* ! READLINE_SUPPORT */
240 if (!(argp
= getans (prompt
, aleqs
))) {
241 #endif /* READLINE_SUPPORT */
245 switch (smatch (*argp
, aleqs
)) {
247 /* display the message being replied to, or distributed */
249 showfile (++argp
, msgnam
);
251 advise (NULL
, "no alternate message to display");
255 /* Translate MIME composition file */
256 buildfile (++argp
, drft
);
260 /* Call an editor on the draft file */
263 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
264 NULL
, 1, atfile
) == NOTOK
)
269 /* display the draft file */
270 showfile (++argp
, drft
);
274 /* Check to whom the draft would be sent */
275 whomfile (++argp
, drft
);
279 /* Quit, and possibly delete the draft */
280 if (*++argp
&& (*argp
[0] == 'd' ||
281 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
284 if (stat (drft
, &st
) != NOTOK
)
285 advise (NULL
, "draft left on %s", drft
);
290 /* Delete draft and exit */
295 /* Send draft in background */
296 if (sendfile (++argp
, drft
, 1))
302 sendfile (++argp
, drft
, 0);
306 /* Refile the draft */
307 if (refile (++argp
, drft
) == 0)
312 /* Change the working directory for attachments
314 * Run the directory through the user's shell so that
315 * we can take advantage of any syntax that the user
316 * is accustomed to. Read back the absolute path.
319 if (*(argp
+1) == (char *)0) {
320 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
323 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
325 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
326 fgets(cwd
, sizeof (cwd
), f
);
328 if (strchr(cwd
, '\n') != (char *)0)
329 *strchr(cwd
, '\n') = '\0';
334 advise("popen", "could not get directory");
340 /* Print the working directory for attachments */
345 /* List files in the current attachment working directory
347 * Use the user's shell so that we can take advantage of any
348 * syntax that the user is accustomed to.
350 writelscmd(buf
, sizeof(buf
), "", argp
);
351 (void)system_in_dir(cwd
, buf
);
356 * List attachments on current draft. Options are:
358 * -l long listing (full path names)
365 while (*++argp
!= (char *)0) {
366 if (strcmp(*argp
, "-l") == 0)
369 else if (strcmp(*argp
, "-n") == 0)
372 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
384 advise((char *)0, "usage is alist [-ln].");
387 annolist(drft
, ATTACH_FIELD
, l
, n
);
393 * Attach files to current draft.
399 for (ap
= argp
+1; *ap
; ++ap
) {
400 if (strcmp(*ap
, "-v") == 0) {
403 } else if (*ap
[0] != '-') {
408 if (*(argp
+1) == (char *)0) {
409 advise(NULL
, "attach command requires file argument(s).");
414 * Build a command line that causes the user's shell to list the file name
415 * arguments. This handles and wildcard expansion, tilde expansion, etc.
417 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
420 * Read back the response from the shell, which contains a number of lines
421 * with one file name per line. Remove off the newline. Determine whether
422 * we have an absolute or relative path name. Prepend the current working
423 * directory to relative path names. Add the attachment annotation to the
427 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
428 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
431 *(strchr(shell
, '\n')) = '\0';
434 strncpy(file
, shell
, sizeof(file
));
435 file
[sizeof(file
) - 1] = '\0';
437 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
440 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
442 ctype
= mime_type(file
);
446 printf ("Attaching %s as a %s", file
, ctype
);
454 advise("popen", "could not get file from shell");
461 * Detach files from current draft.
465 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
466 * so this catches a -n anywhere in the argument list.
469 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
470 if (strcmp(*arguments
, "-n") == 0) {
477 * A -n was found so interpret the arguments as attachment numbers.
478 * Decrement any remaining argument number that is greater than the one
479 * just processed after processing each one so that the numbering stays
484 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
485 if (strcmp(*arguments
, "-n") == 0)
488 if (**arguments
!= '\0') {
489 n
= atoi(*arguments
);
490 annotate(drft
, ATTACH_FIELD
, (char *)0, 1, 0, n
, 1);
492 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
493 if (atoi(*argp
) > n
) {
494 if (atoi(*argp
) == 1)
497 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
505 * The arguments are interpreted as file names. Run them through the
506 * user's shell for wildcard expansion and other goodies. Do this from
507 * the current working directory if the argument is not an absolute path
508 * name (does not begin with a /).
510 * We feed all the file names to the shell at once, otherwise you can't
511 * provide a file name with a space in it.
513 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
514 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
515 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
516 *(strchr(shell
, '\n')) = '\0';
517 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
521 advise("popen", "could not get file from shell");
527 /* Unknown command */
528 advise (NULL
, "say what?");
537 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
539 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
542 /* Note that we do not quote -- the argp from the user
543 * is assumed to be quoted as they desire. (We can't treat
544 * it as pure literal as that would prevent them using ~,
545 * wildcards, etc.) The buffer produced by this function
546 * should be given to popen_in_dir() or system_in_dir() so
547 * that the current working directory is set correctly.
549 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
550 /* NB that some snprintf() return -1 on overflow rather than the
551 * new C99 mandated 'number of chars that would have been written'
553 /* length checks here and inside the loop allow for the
554 * trailing "&&", trailcmd, '"' and NUL
556 int trailln
= strlen(trailcmd
) + 4;
557 if (ln
< 0 || ln
+ trailln
> bufsz
)
558 adios((char *)0, "arguments too long");
562 while (*argp
&& *++argp
) {
564 /* +1 for leading space */
565 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
566 adios((char *)0, "arguments too long");
568 memcpy(cp
, *argp
, ln
+1);
572 *cp
++ = '&'; *cp
++ = '&';
573 strcpy(cp
, trailcmd
);
581 * Build a command line that causes the user's shell to list the file name
582 * arguments. This handles and wildcard expansion, tilde expansion, etc.
585 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
587 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
588 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
592 /* Like system(), but run the command in directory dir.
593 * This assumes the program is single-threaded!
596 system_in_dir(const char *dir
, const char *cmd
)
601 /* ensure that $SHELL exists, as the cmd was written relying on
602 a non-blank $SHELL... */
603 setenv("SHELL","/bin/sh",0); /* don't overwrite */
605 if (getcwd(olddir
, sizeof(olddir
)) == 0)
606 adios("getcwd", "could not get working directory");
608 adios("chdir", "could not change working directory");
610 if (chdir(olddir
) != 0)
611 adios("chdir", "could not change working directory");
615 /* ditto for popen() */
617 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
622 /* ensure that $SHELL exists, as the cmd was written relying on
623 a non-blank $SHELL... */
624 setenv("SHELL","/bin/sh",0); /* don't overwrite */
626 if (getcwd(olddir
, sizeof(olddir
)) == 0)
627 adios("getcwd", "could not get working directory");
629 adios("chdir", "could not change working directory");
630 f
= popen(cmd
, type
);
631 if (chdir(olddir
) != 0)
632 adios("chdir", "could not change working directory");
641 static int reedit
= 0; /* have we been here before? */
642 static char *edsave
= NULL
; /* the editor we used previously */
646 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
647 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
649 int pid
, status
, vecp
;
650 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
651 char *cp
, *prog
, **vec
;
656 /* Was there a previous edit session? */
658 if (!*ed
) { /* no explicit editor */
659 *ed
= edsave
; /* so use the previous one */
660 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
663 /* unless we've specified it via "editor-next" */
664 cp
= concat (cp
, "-next", NULL
);
665 if ((cp
= context_find (cp
)) != NULL
)
669 /* set initial editor */
671 *ed
= get_default_editor();
675 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
676 strncpy (altpath
, altmsg
, sizeof(altpath
));
678 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
680 strncpy (linkpath
, LINK
, sizeof(linkpath
));
682 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
686 if (link (altpath
, linkpath
) == NOTOK
) {
687 symlink (altpath
, linkpath
);
695 context_save (); /* save the context file */
698 switch (pid
= fork()) {
700 advise ("fork", "unable to");
709 m_putenv ("mhfolder", mp
->foldpath
);
710 m_putenv ("editalt", altpath
);
713 vec
= argsplit(*ed
, &prog
, &vecp
);
717 vec
[vecp
++] = *arg
++;
722 fprintf (stderr
, "unable to exec ");
727 if ((status
= pidwait (pid
, NOTOK
))) {
728 if (((status
& 0xff00) != 0xff00)
729 && (!reedit
|| (status
& 0x00ff))) {
730 if (!use
&& (status
& 0xff00) &&
731 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
732 advise (NULL
, "problems with edit--draft left in %s", cp
);
734 advise (NULL
, "problems with edit--%s preserved", file
);
737 status
= -2; /* maybe "reedit ? -2 : -1"? */
746 ? lstat (linkpath
, &st
) != NOTOK
747 && S_ISREG(st
.st_mode
)
748 && copyf (linkpath
, altpath
) == NOTOK
749 : stat (linkpath
, &st
) != NOTOK
751 && (unlink (altpath
) == NOTOK
752 || link (linkpath
, altpath
) == NOTOK
)))
753 advise (linkpath
, "unable to update %s from", altmsg
);
756 /* normally, we remember which editor we used */
758 edsave
= getcpy (*ed
);
761 if (altmsg
&& atfile
)
769 copyf (char *ifile
, char *ofile
)
774 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
776 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
777 admonish (ofile
, "unable to open and truncate");
782 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
783 if (write (out
, buffer
, i
) != i
) {
784 advise (ofile
, "may have damaged");
800 sendfile (char **arg
, char *file
, int pushsw
)
804 char *cp
, *sp
, **vec
, *program
;
807 * If the sendproc is the nmh command `send', then we call
808 * those routines directly rather than exec'ing the command.
810 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
812 sendit (invo_name
= sp
, arg
, file
, pushsw
);
817 context_save (); /* save the context file */
820 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
824 advise (NULL
, "unable to fork, so sending directly...");
826 vec
= argsplit(sendproc
, &program
, &vecp
);
828 vec
[vecp
++] = "-push";
831 vec
[vecp
++] = *arg
++;
835 execvp (program
, vec
);
836 fprintf (stderr
, "unable to exec ");
841 if (pidwait(child_id
, OK
) == 0)
849 * Translate MIME composition file (call buildmimeproc)
853 buildfile (char **argp
, char *file
)
860 /* allocate space for arguments */
866 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
869 * For backward compatibility, we need to add -build
870 * if we are using mhn as buildmimeproc
873 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
874 args
[i
++] = "-build";
876 /* copy any other arguments */
877 while (argp
&& *argp
)
881 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
884 return (i
? NOTOK
: OK
);
889 * Check if draft is a mhbuild composition file
893 check_draft (char *msgnam
)
896 char buf
[BUFSIZ
], name
[NAMESZ
];
898 m_getfld_state_t gstate
= 0;
900 if ((fp
= fopen (msgnam
, "r")) == NULL
)
903 int bufsz
= sizeof buf
;
904 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
908 * If draft already contains any of the
909 * Content-XXX fields, then assume it already
912 if (uprf (name
, XXX_FIELD_PRF
)) {
914 m_getfld_state_destroy (&gstate
);
917 while (state
== FLDPLUS
) {
919 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
927 for (bp
= buf
; *bp
; bp
++)
928 if (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n') {
930 m_getfld_state_destroy (&gstate
);
935 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
936 } while (state
== BODY
);
941 m_getfld_state_destroy (&gstate
);
949 # define SASLminc(a) (a)
950 #else /* CYRUS_SASL */
951 # define SASLminc(a) 0
952 #endif /* CYRUS_SASL */
955 # define TLSminc(a) (a)
956 #else /* TLS_SUPPORT */
957 # define TLSminc(a) 0
958 #endif /* TLS_SUPPORT */
960 #define SEND_SWITCHES \
961 X("alias aliasfile", 0, ALIASW) \
962 X("debug", -5, DEBUGSW) \
963 X("filter filterfile", 0, FILTSW) \
964 X("nofilter", 0, NFILTSW) \
965 X("format", 0, FRMTSW) \
966 X("noformat", 0, NFRMTSW) \
967 X("forward", 0, FORWSW) \
968 X("noforward", 0, NFORWSW) \
969 X("mime", 0, MIMESW) \
970 X("nomime", 0, NMIMESW) \
971 X("msgid", 0, MSGDSW) \
972 X("nomsgid", 0, NMSGDSW) \
973 X("push", 0, SPSHSW) \
974 X("nopush", 0, NSPSHSW) \
975 X("split seconds", 0, SPLITSW) \
976 X("unique", -6, UNIQSW) \
977 X("nounique", -8, NUNIQSW) \
978 X("verbose", 0, VERBSW) \
979 X("noverbose", 0, NVERBSW) \
980 X("watch", 0, WATCSW) \
981 X("nowatch", 0, NWATCSW) \
982 X("width columns", 0, WIDTHSW) \
983 X("version", 0, SVERSIONSW) \
984 X("help", 0, SHELPSW) \
985 X("dashstuffing", -12, BITSTUFFSW) \
986 X("nodashstuffing", -14, NBITSTUFFSW) \
987 X("client host", -6, CLIESW) \
988 X("server host", 6, SERVSW) \
989 X("snoop", -5, SNOOPSW) \
990 X("draftfolder +folder", -6, SDRFSW) \
991 X("draftmessage msg", -6, SDRMSW) \
992 X("nodraftfolder", -3, SNDRFSW) \
993 X("sasl", SASLminc(-4), SASLSW) \
994 X("nosasl", SASLminc(-6), NOSASLSW) \
995 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
996 X("saslmech", SASLminc(-5), SASLMECHSW) \
997 X("user", SASLminc(-4), USERSW) \
998 X("attach fieldname", 6, SNDATTACHSW) \
999 X("noattach", 0, SNDNOATTACHSW) \
1000 X("attachformat", 7, SNDATTACHFORMAT) \
1001 X("port server-port-name/number", 4, PORTSW) \
1002 X("tls", TLSminc(-3), TLSSW) \
1003 X("initialtls", TLSminc(-10), INITTLSSW) \
1004 X("notls", TLSminc(-5), NTLSSW) \
1005 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
1006 X("messageid localname|random", 2, MESSAGEIDSW) \
1008 #define X(sw, minchars, id) id,
1009 DEFINE_SWITCH_ENUM(SEND
);
1012 #define X(sw, minchars, id) { sw, minchars, id },
1013 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
1017 extern int debugsw
; /* from sendsbr.c */
1025 extern char *altmsg
; /* .. */
1026 extern char *annotext
;
1027 extern char *distfile
;
1031 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1034 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1035 char **arguments
, *savearg
[MAXARGS
], **vec
;
1043 * Make sure these are defined. In particular, we need
1044 * savearg[1] to be NULL, in case "arg" is NULL below. It
1045 * doesn't matter what is the value of savearg[0], but we
1046 * set it to NULL, to help catch "off-by-one" errors.
1052 * Temporarily copy arg to savearg, since the brkstring() call in
1053 * getarguments() will wipe it out before it is merged in.
1054 * Also, we skip the first element of savearg, since getarguments()
1055 * skips it. Then we count the number of arguments
1056 * copied. The value of "n" will be one greater than
1057 * this in order to simulate the standard argc/argv.
1062 copyip (arg
, savearg
+1, MAXARGS
-1);
1069 * Merge any arguments from command line (now in savearg)
1070 * and arguments from profile.
1072 arguments
= getarguments (sp
, n
, savearg
, 1);
1085 * Get our initial arguments for postproc now
1088 vec
= argsplit(postproc
, &program
, &vecp
);
1090 vec
[vecp
++] = "-library";
1091 vec
[vecp
++] = getcpy (m_maildir (""));
1093 if ((cp
= context_find ("fileproc"))) {
1094 vec
[vecp
++] = "-fileproc";
1098 if ((cp
= context_find ("mhlproc"))) {
1099 vec
[vecp
++] = "-mhlproc";
1103 if ((cp
= context_find ("credentials"))) {
1104 /* post doesn't read context so need to pass credentials. */
1105 vec
[vecp
++] = "-credentials";
1109 while ((cp
= *argp
++)) {
1111 switch (smatch (++cp
, sendswitches
)) {
1113 ambigsw (cp
, sendswitches
);
1116 advise (NULL
, "-%s unknown\n", cp
);
1120 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1121 print_help (buf
, sendswitches
, 1);
1124 print_version (invo_name
);
1135 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1136 advise (NULL
, "missing argument to %s", argp
[-2]);
1164 debugsw
++; /* fall */
1197 if (!(cp
= *argp
++) || *cp
== '-') {
1198 advise (NULL
, "missing argument to %s", argp
[-2]);
1206 if (!(cp
= *argp
++) || *cp
== '-') {
1207 advise (NULL
, "missing argument to %s", argp
[-2]);
1214 advise(NULL
, "The -attach switch is deprecated");
1217 advise(NULL
, "The -noattach switch is deprecated");
1220 case SNDATTACHFORMAT
:
1221 advise(NULL
, "The -attachformat switch is deprecated");
1225 advise (NULL
, "usage: %s [switches]", sp
);
1229 /* allow Aliasfile: profile entry */
1230 if ((cp
= context_find ("Aliasfile"))) {
1234 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1235 vec
[vecp
++] = "-alias";
1240 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1241 if ((cp
= context_find ("signature")) && *cp
)
1242 m_putenv ("SIGNATURE", cp
);
1244 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1246 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1248 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1249 inplace
= atoi (cp
);
1251 if ((cp
= getenv ("mhdist"))
1254 && (distsw
= atoi (cp
))
1255 #endif /* not lint */
1257 vec
[vecp
++] = "-dist";
1258 distfile
= getcpy (m_mktemp2(altmsg
, invo_name
, NULL
, NULL
));
1260 if (link (altmsg
, distfile
) == NOTOK
)
1261 adios (distfile
, "unable to link %s to", altmsg
);
1266 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1271 if ((pushsw
= pushed
))
1276 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1) == OK
)
1285 whomfile (char **arg
, char *file
)
1289 char **vec
, *program
;
1291 context_save (); /* save the context file */
1294 switch (pid
= fork()) {
1296 advise ("fork", "unable to");
1300 vec
= argsplit(whomproc
, &program
, &vecp
);
1304 vec
[vecp
++] = *arg
++;
1307 execvp (program
, vec
);
1308 fprintf (stderr
, "unable to exec ");
1310 _exit (-1); /* NOTREACHED */
1313 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1319 * Remove the draft file
1323 removefile (char *drft
)
1325 if (unlink (drft
) == NOTOK
)
1326 adios (drft
, "unable to unlink");