]>
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 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 cwd
[PATH_MAX
+ 1]; /* current working directory */
126 char file
[PATH_MAX
+ 1]; /* file name buffer */
127 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
128 FILE *f
; /* read pointer for bgnd proc */
129 char *l
; /* set on -l to alist command */
130 int n
; /* set on -n to alist command */
132 /* Need this if called from what_now(). */
133 invo_name
= r1bindex (argv
[0], '/');
135 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 * Get the initial current working directory.
142 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
143 adios("getcwd", "could not get working directory");
146 while ((cp
= *argp
++)) {
148 switch (smatch (++cp
, whatnowswitches
)) {
150 ambigsw (cp
, whatnowswitches
);
153 adios (NULL
, "-%s unknown", cp
);
156 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
157 print_help (buf
, whatnowswitches
, 1);
160 print_version(invo_name
);
165 adios (NULL
, "only one draft folder at a time!");
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
168 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
169 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
173 adios (NULL
, "only one draft message at a time!");
174 if (!(dmsg
= *argp
++) || *dmsg
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
183 if (!(ed
= *argp
++) || *ed
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
192 if (!(myprompt
= *argp
++) || *myprompt
== '-')
193 adios (NULL
, "missing argument to %s", argp
[-2]);
197 advise(NULL
, "The -attach switch is deprecated");
201 advise(NULL
, "The -noattach switch is deprecated");
206 adios (NULL
, "only one draft at a time!");
211 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
212 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
214 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
216 if ((cp
= getenv ("mhatfile")) && *cp
)
219 if ((cp
= getenv ("mhuse")) && *cp
)
222 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
227 /* start editing the draft, unless -noedit was given */
228 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
229 NULL
, 1, atfile
) < 0)
232 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
234 #ifdef READLINE_SUPPORT
235 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
236 #else /* ! READLINE_SUPPORT */
237 if (!(argp
= getans (prompt
, aleqs
))) {
238 #endif /* READLINE_SUPPORT */
239 (void) m_unlink (LINK
);
242 switch (smatch (*argp
, aleqs
)) {
244 /* display the message being replied to, or distributed */
246 showfile (++argp
, msgnam
);
248 advise (NULL
, "no alternate message to display");
252 /* Translate MIME composition file */
253 buildfile (++argp
, drft
);
257 /* Call an editor on the draft file */
260 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
261 NULL
, 1, atfile
) == NOTOK
)
266 /* display the draft file */
267 showfile (++argp
, drft
);
271 /* Check to whom the draft would be sent */
272 whomfile (++argp
, drft
);
276 /* Quit, and possibly delete the draft */
277 if (*++argp
&& (*argp
[0] == 'd' ||
278 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
281 if (stat (drft
, &st
) != NOTOK
)
282 advise (NULL
, "draft left on %s", drft
);
287 /* Delete draft and exit */
292 /* Send draft in background */
293 if (sendfile (++argp
, drft
, 1))
299 sendfile (++argp
, drft
, 0);
303 /* Refile the draft */
304 if (refile (++argp
, drft
) == 0)
309 /* Change the working directory for attachments
311 * Run the directory through the user's shell so that
312 * we can take advantage of any syntax that the user
313 * is accustomed to. Read back the absolute path.
316 if (*(argp
+1) == (char *)0) {
317 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
320 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
322 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
323 fgets(cwd
, sizeof (cwd
), f
);
325 if (strchr(cwd
, '\n') != (char *)0)
326 *strchr(cwd
, '\n') = '\0';
331 advise("popen", "could not get directory");
337 /* Print the working directory for attachments */
342 /* List files in the current attachment working directory
344 * Use the user's shell so that we can take advantage of any
345 * syntax that the user is accustomed to.
347 writelscmd(buf
, sizeof(buf
), "", argp
);
348 (void)system_in_dir(cwd
, buf
);
353 * List attachments on current draft. Options are:
355 * -l long listing (full path names)
362 while (*++argp
!= (char *)0) {
363 if (strcmp(*argp
, "-l") == 0)
366 else if (strcmp(*argp
, "-n") == 0)
369 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
381 advise((char *)0, "usage is alist [-ln].");
384 annolist(drft
, ATTACH_FIELD
, l
, n
);
390 * Attach files to current draft.
396 for (ap
= argp
+1; *ap
; ++ap
) {
397 if (strcmp(*ap
, "-v") == 0) {
400 } else if (*ap
[0] != '-') {
405 if (*(argp
+1) == (char *)0) {
406 advise(NULL
, "attach command requires file argument(s).");
411 * Build a command line that causes the user's shell to list the file name
412 * arguments. This handles and wildcard expansion, tilde expansion, etc.
414 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
417 * Read back the response from the shell, which contains a number of lines
418 * with one file name per line. Remove off the newline. Determine whether
419 * we have an absolute or relative path name. Prepend the current working
420 * directory to relative path names. Add the attachment annotation to the
424 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
425 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
428 *(strchr(shell
, '\n')) = '\0';
431 strncpy(file
, shell
, sizeof(file
));
432 file
[sizeof(file
) - 1] = '\0';
434 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
437 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
439 ctype
= mime_type(file
);
443 printf ("Attaching %s as a %s\n", file
, ctype
);
451 advise("popen", "could not get file from shell");
458 * Detach files from current draft.
462 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
463 * so this catches a -n anywhere in the argument list.
466 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
467 if (strcmp(*arguments
, "-n") == 0) {
474 * A -n was found so interpret the arguments as attachment numbers.
475 * Decrement any remaining argument number that is greater than the one
476 * just processed after processing each one so that the numbering stays
481 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
482 if (strcmp(*arguments
, "-n") == 0)
485 if (**arguments
!= '\0') {
486 n
= atoi(*arguments
);
487 annotate(drft
, ATTACH_FIELD
, (char *)0, 1, 0, n
, 1);
489 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
490 if (atoi(*argp
) > n
) {
491 if (atoi(*argp
) == 1)
494 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
502 * The arguments are interpreted as file names. Run them through the
503 * user's shell for wildcard expansion and other goodies. Do this from
504 * the current working directory if the argument is not an absolute path
505 * name (does not begin with a /).
507 * We feed all the file names to the shell at once, otherwise you can't
508 * provide a file name with a space in it.
510 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
511 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
512 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
513 *(strchr(shell
, '\n')) = '\0';
514 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
518 advise("popen", "could not get file from shell");
524 /* Unknown command */
525 advise (NULL
, "say what?");
534 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
536 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
539 /* Note that we do not quote -- the argp from the user
540 * is assumed to be quoted as they desire. (We can't treat
541 * it as pure literal as that would prevent them using ~,
542 * wildcards, etc.) The buffer produced by this function
543 * should be given to popen_in_dir() or system_in_dir() so
544 * that the current working directory is set correctly.
546 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
547 /* NB that some snprintf() return -1 on overflow rather than the
548 * new C99 mandated 'number of chars that would have been written'
550 /* length checks here and inside the loop allow for the
551 * trailing "&&", trailcmd, '"' and NUL
553 int trailln
= strlen(trailcmd
) + 4;
554 if (ln
< 0 || ln
+ trailln
> bufsz
)
555 adios((char *)0, "arguments too long");
559 while (*argp
&& *++argp
) {
561 /* +1 for leading space */
562 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
563 adios((char *)0, "arguments too long");
565 memcpy(cp
, *argp
, ln
+1);
569 *cp
++ = '&'; *cp
++ = '&';
570 strcpy(cp
, trailcmd
);
578 * Build a command line that causes the user's shell to list the file name
579 * arguments. This handles and wildcard expansion, tilde expansion, etc.
582 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
584 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
585 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
589 /* Like system(), but run the command in directory dir.
590 * This assumes the program is single-threaded!
593 system_in_dir(const char *dir
, const char *cmd
)
598 /* ensure that $SHELL exists, as the cmd was written relying on
599 a non-blank $SHELL... */
600 setenv("SHELL","/bin/sh",0); /* don't overwrite */
602 if (getcwd(olddir
, sizeof(olddir
)) == 0)
603 adios("getcwd", "could not get working directory");
605 adios("chdir", "could not change working directory");
607 if (chdir(olddir
) != 0)
608 adios("chdir", "could not change working directory");
612 /* ditto for popen() */
614 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
619 /* ensure that $SHELL exists, as the cmd was written relying on
620 a non-blank $SHELL... */
621 setenv("SHELL","/bin/sh",0); /* don't overwrite */
623 if (getcwd(olddir
, sizeof(olddir
)) == 0)
624 adios("getcwd", "could not get working directory");
626 adios("chdir", "could not change working directory");
627 f
= popen(cmd
, type
);
628 if (chdir(olddir
) != 0)
629 adios("chdir", "could not change working directory");
638 static int reedit
= 0; /* have we been here before? */
639 static char *edsave
= NULL
; /* the editor we used previously */
643 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
644 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
646 int pid
, status
, vecp
;
647 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
648 char *cp
, *prog
, **vec
;
653 /* Was there a previous edit session? */
655 if (!*ed
) { /* no explicit editor */
656 *ed
= edsave
; /* so use the previous one */
657 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
660 /* unless we've specified it via "editor-next" */
661 cp
= concat (cp
, "-next", NULL
);
662 if ((cp
= context_find (cp
)) != NULL
)
666 /* set initial editor */
668 *ed
= get_default_editor();
672 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
673 strncpy (altpath
, altmsg
, sizeof(altpath
));
675 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
677 strncpy (linkpath
, LINK
, sizeof(linkpath
));
679 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
682 (void) m_unlink (linkpath
);
683 if (link (altpath
, linkpath
) == NOTOK
) {
684 symlink (altpath
, linkpath
);
692 context_save (); /* save the context file */
695 switch (pid
= fork()) {
697 advise ("fork", "unable to");
706 m_putenv ("mhfolder", mp
->foldpath
);
707 m_putenv ("editalt", altpath
);
710 vec
= argsplit(*ed
, &prog
, &vecp
);
714 vec
[vecp
++] = *arg
++;
719 fprintf (stderr
, "unable to exec ");
724 if ((status
= pidwait (pid
, NOTOK
))) {
725 if (((status
& 0xff00) != 0xff00)
726 && (!reedit
|| (status
& 0x00ff))) {
727 if (!use
&& (status
& 0xff00) &&
728 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
729 advise (NULL
, "problems with edit--draft left in %s", cp
);
731 advise (NULL
, "problems with edit--%s preserved", file
);
734 status
= -2; /* maybe "reedit ? -2 : -1"? */
743 ? lstat (linkpath
, &st
) != NOTOK
744 && S_ISREG(st
.st_mode
)
745 && copyf (linkpath
, altpath
) == NOTOK
746 : stat (linkpath
, &st
) != NOTOK
748 && (m_unlink (altpath
) == NOTOK
749 || link (linkpath
, altpath
) == NOTOK
)))
750 advise (linkpath
, "unable to update %s from", altmsg
);
753 /* normally, we remember which editor we used */
755 edsave
= getcpy (*ed
);
758 if (altmsg
&& atfile
)
759 (void) m_unlink (linkpath
);
766 copyf (char *ifile
, char *ofile
)
771 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
773 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
774 admonish (ofile
, "unable to open and truncate");
779 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
780 if (write (out
, buffer
, i
) != i
) {
781 advise (ofile
, "may have damaged");
797 sendfile (char **arg
, char *file
, int pushsw
)
801 char *cp
, *sp
, **vec
, *program
;
804 * If the sendproc is the nmh command `send', then we call
805 * those routines directly rather than exec'ing the command.
807 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
809 sendit (invo_name
= sp
, arg
, file
, pushsw
);
814 context_save (); /* save the context file */
817 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
821 advise (NULL
, "unable to fork, so sending directly...");
823 vec
= argsplit(sendproc
, &program
, &vecp
);
825 vec
[vecp
++] = "-push";
828 vec
[vecp
++] = *arg
++;
832 execvp (program
, vec
);
833 fprintf (stderr
, "unable to exec ");
838 if (pidwait(child_id
, OK
) == 0)
846 * Translate MIME composition file (call buildmimeproc)
850 buildfile (char **argp
, char *file
)
857 /* allocate space for arguments */
863 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
866 * For backward compatibility, we need to add -build
867 * if we are using mhn as buildmimeproc
870 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
871 args
[i
++] = "-build";
873 /* copy any other arguments */
874 while (argp
&& *argp
)
878 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
881 return (i
? NOTOK
: OK
);
886 # define SASLminc(a) (a)
887 #else /* CYRUS_SASL */
888 # define SASLminc(a) 0
889 #endif /* CYRUS_SASL */
892 # define TLSminc(a) (a)
893 #else /* TLS_SUPPORT */
894 # define TLSminc(a) 0
895 #endif /* TLS_SUPPORT */
897 #define SEND_SWITCHES \
898 X("alias aliasfile", 0, ALIASW) \
899 X("debug", -5, DEBUGSW) \
900 X("filter filterfile", 0, FILTSW) \
901 X("nofilter", 0, NFILTSW) \
902 X("format", 0, FRMTSW) \
903 X("noformat", 0, NFRMTSW) \
904 X("forward", 0, FORWSW) \
905 X("noforward", 0, NFORWSW) \
906 X("mime", 0, MIMESW) \
907 X("nomime", 0, NMIMESW) \
908 X("msgid", 0, MSGDSW) \
909 X("nomsgid", 0, NMSGDSW) \
910 X("push", 0, SPSHSW) \
911 X("nopush", 0, NSPSHSW) \
912 X("split seconds", 0, SPLITSW) \
913 X("unique", -6, UNIQSW) \
914 X("nounique", -8, NUNIQSW) \
915 X("verbose", 0, VERBSW) \
916 X("noverbose", 0, NVERBSW) \
917 X("watch", 0, WATCSW) \
918 X("nowatch", 0, NWATCSW) \
919 X("width columns", 0, WIDTHSW) \
920 X("version", 0, SVERSIONSW) \
921 X("help", 0, SHELPSW) \
922 X("dashstuffing", -12, BITSTUFFSW) \
923 X("nodashstuffing", -14, NBITSTUFFSW) \
924 X("client host", -6, CLIESW) \
925 X("server host", 6, SERVSW) \
926 X("snoop", -5, SNOOPSW) \
927 X("draftfolder +folder", -6, SDRFSW) \
928 X("draftmessage msg", -6, SDRMSW) \
929 X("nodraftfolder", -3, SNDRFSW) \
930 X("sasl", SASLminc(-4), SASLSW) \
931 X("nosasl", SASLminc(-6), NOSASLSW) \
932 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
933 X("saslmech", SASLminc(-5), SASLMECHSW) \
934 X("user", SASLminc(-4), USERSW) \
935 X("attach fieldname", 6, SNDATTACHSW) \
936 X("noattach", 0, SNDNOATTACHSW) \
937 X("attachformat", 7, SNDATTACHFORMAT) \
938 X("port server-port-name/number", 4, PORTSW) \
939 X("tls", TLSminc(-3), TLSSW) \
940 X("initialtls", TLSminc(-10), INITTLSSW) \
941 X("notls", TLSminc(-5), NTLSSW) \
942 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
943 X("messageid localname|random", 2, MESSAGEIDSW) \
945 #define X(sw, minchars, id) id,
946 DEFINE_SWITCH_ENUM(SEND
);
949 #define X(sw, minchars, id) { sw, minchars, id },
950 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
954 extern int debugsw
; /* from sendsbr.c */
962 extern char *altmsg
; /* .. */
963 extern char *annotext
;
964 extern char *distfile
;
968 sendit (char *sp
, char **arg
, char *file
, int pushed
)
971 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
972 char **arguments
, *savearg
[MAXARGS
], **vec
;
980 * Make sure these are defined. In particular, we need
981 * savearg[1] to be NULL, in case "arg" is NULL below. It
982 * doesn't matter what is the value of savearg[0], but we
983 * set it to NULL, to help catch "off-by-one" errors.
989 * Temporarily copy arg to savearg, since the brkstring() call in
990 * getarguments() will wipe it out before it is merged in.
991 * Also, we skip the first element of savearg, since getarguments()
992 * skips it. Then we count the number of arguments
993 * copied. The value of "n" will be one greater than
994 * this in order to simulate the standard argc/argv.
999 copyip (arg
, savearg
+1, MAXARGS
-1);
1006 * Merge any arguments from command line (now in savearg)
1007 * and arguments from profile.
1009 arguments
= getarguments (sp
, n
, savearg
, 1);
1022 * Get our initial arguments for postproc now
1025 vec
= argsplit(postproc
, &program
, &vecp
);
1027 vec
[vecp
++] = "-library";
1028 vec
[vecp
++] = getcpy (m_maildir (""));
1030 if ((cp
= context_find ("fileproc"))) {
1031 vec
[vecp
++] = "-fileproc";
1035 if ((cp
= context_find ("mhlproc"))) {
1036 vec
[vecp
++] = "-mhlproc";
1040 if ((cp
= context_find ("credentials"))) {
1041 /* post doesn't read context so need to pass credentials. */
1042 vec
[vecp
++] = "-credentials";
1046 while ((cp
= *argp
++)) {
1048 switch (smatch (++cp
, sendswitches
)) {
1050 ambigsw (cp
, sendswitches
);
1053 advise (NULL
, "-%s unknown\n", cp
);
1057 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1058 print_help (buf
, sendswitches
, 1);
1061 print_version (invo_name
);
1072 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1073 advise (NULL
, "missing argument to %s", argp
[-2]);
1101 debugsw
++; /* fall */
1134 if (!(cp
= *argp
++) || *cp
== '-') {
1135 advise (NULL
, "missing argument to %s", argp
[-2]);
1143 if (!(cp
= *argp
++) || *cp
== '-') {
1144 advise (NULL
, "missing argument to %s", argp
[-2]);
1151 advise(NULL
, "The -attach switch is deprecated");
1154 advise(NULL
, "The -noattach switch is deprecated");
1157 case SNDATTACHFORMAT
:
1158 advise(NULL
, "The -attachformat switch is deprecated");
1162 advise (NULL
, "usage: %s [switches]", sp
);
1166 /* allow Aliasfile: profile entry */
1167 if ((cp
= context_find ("Aliasfile"))) {
1171 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1172 vec
[vecp
++] = "-alias";
1177 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1178 if ((cp
= context_find ("signature")) && *cp
)
1179 m_putenv ("SIGNATURE", cp
);
1181 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1183 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1185 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1186 inplace
= atoi (cp
);
1188 if ((cp
= getenv ("mhdist"))
1191 && (distsw
= atoi (cp
))
1192 #endif /* not lint */
1194 vec
[vecp
++] = "-dist";
1195 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1196 adios(NULL
, "unable to create temporary file in %s",
1199 distfile
= getcpy (cp
);
1200 (void) m_unlink(distfile
);
1201 if (link (altmsg
, distfile
) == NOTOK
)
1202 adios (distfile
, "unable to link %s to", altmsg
);
1207 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1212 if ((pushsw
= pushed
))
1217 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1) == OK
)
1226 whomfile (char **arg
, char *file
)
1230 char **vec
, *program
;
1232 context_save (); /* save the context file */
1235 switch (pid
= fork()) {
1237 advise ("fork", "unable to");
1241 vec
= argsplit(whomproc
, &program
, &vecp
);
1245 vec
[vecp
++] = *arg
++;
1248 execvp (program
, vec
);
1249 fprintf (stderr
, "unable to exec ");
1251 _exit (-1); /* NOTREACHED */
1254 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1260 * Remove the draft file
1264 removefile (char *drft
)
1266 if (m_unlink (drft
) == NOTOK
)
1267 adios (drft
, "unable to unlink");