]>
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 int checkmimeheader (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 /* Need this if called from what_now(). */
134 invo_name
= r1bindex (argv
[0], '/');
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 * Get the initial current working directory.
143 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
144 adios("getcwd", "could not get working directory");
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, whatnowswitches
)) {
151 ambigsw (cp
, whatnowswitches
);
154 adios (NULL
, "-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
158 print_help (buf
, whatnowswitches
, 1);
161 print_version(invo_name
);
166 adios (NULL
, "only one draft folder at a time!");
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
169 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
170 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
174 adios (NULL
, "only one draft message at a time!");
175 if (!(dmsg
= *argp
++) || *dmsg
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (!(ed
= *argp
++) || *ed
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
193 if (!(myprompt
= *argp
++) || *myprompt
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
198 advise(NULL
, "The -attach switch is deprecated");
202 advise(NULL
, "The -noattach switch is deprecated");
207 adios (NULL
, "only one draft at a time!");
212 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
213 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
215 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
217 if ((cp
= getenv ("mhatfile")) && *cp
)
220 if ((cp
= getenv ("mhuse")) && *cp
)
223 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
228 /* start editing the draft, unless -noedit was given */
229 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
230 NULL
, 1, atfile
) < 0)
233 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
235 #ifdef READLINE_SUPPORT
236 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
237 #else /* ! READLINE_SUPPORT */
238 if (!(argp
= getans (prompt
, aleqs
))) {
239 #endif /* READLINE_SUPPORT */
240 (void) m_unlink (LINK
);
243 switch (smatch (*argp
, aleqs
)) {
245 /* display the message being replied to, or distributed */
247 showfile (++argp
, msgnam
);
249 advise (NULL
, "no alternate message to display");
253 /* Translate MIME composition file */
254 buildfile (++argp
, drft
);
258 /* Call an editor on the draft file */
261 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
262 NULL
, 1, atfile
) == NOTOK
)
267 /* display the draft file */
268 showfile (++argp
, drft
);
272 /* Check to whom the draft would be sent */
273 whomfile (++argp
, drft
);
277 /* Quit, and possibly delete the draft */
278 if (*++argp
&& (*argp
[0] == 'd' ||
279 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
282 if (stat (drft
, &st
) != NOTOK
)
283 advise (NULL
, "draft left on %s", drft
);
288 /* Delete draft and exit */
293 /* Send draft in background */
294 if (sendfile (++argp
, drft
, 1))
300 sendfile (++argp
, drft
, 0);
304 /* Refile the draft */
305 if (refile (++argp
, drft
) == 0)
310 /* Change the working directory for attachments
312 * Run the directory through the user's shell so that
313 * we can take advantage of any syntax that the user
314 * is accustomed to. Read back the absolute path.
317 if (*(argp
+1) == (char *)0) {
318 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
321 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
323 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
324 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
325 advise (buf
, "fgets");
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)
362 if (checkmimeheader(drft
))
368 while (*++argp
!= (char *)0) {
369 if (strcmp(*argp
, "-l") == 0)
372 else if (strcmp(*argp
, "-n") == 0)
375 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
387 advise((char *)0, "usage is alist [-ln].");
390 annolist(drft
, ATTACH_FIELD
, l
, n
);
396 * Attach files to current draft.
402 if (checkmimeheader(drft
))
405 for (ap
= argp
+1; *ap
; ++ap
) {
406 if (strcmp(*ap
, "-v") == 0) {
409 } else if (*ap
[0] != '-') {
414 if (*(argp
+1) == (char *)0) {
415 advise(NULL
, "attach command requires file argument(s).");
420 * Build a command line that causes the user's shell to list the file name
421 * arguments. This handles and wildcard expansion, tilde expansion, etc.
423 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
426 * Read back the response from the shell, which contains a number of lines
427 * with one file name per line. Remove off the newline. Determine whether
428 * we have an absolute or relative path name. Prepend the current working
429 * directory to relative path names. Add the attachment annotation to the
433 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
434 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
437 *(strchr(shell
, '\n')) = '\0';
440 strncpy(file
, shell
, sizeof(file
));
441 file
[sizeof(file
) - 1] = '\0';
443 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
446 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
448 ctype
= mime_type(file
);
452 printf ("Attaching %s as a %s\n", file
, ctype
);
460 advise("popen", "could not get file from shell");
467 * Detach files from current draft.
471 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
472 * so this catches a -n anywhere in the argument list.
475 if (checkmimeheader(drft
))
478 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
479 if (strcmp(*arguments
, "-n") == 0) {
486 * A -n was found so interpret the arguments as attachment numbers.
487 * Decrement any remaining argument number that is greater than the one
488 * just processed after processing each one so that the numbering stays
493 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
494 if (strcmp(*arguments
, "-n") == 0)
497 if (**arguments
!= '\0') {
498 n
= atoi(*arguments
);
499 annotate(drft
, ATTACH_FIELD
, (char *)0, 1, 0, n
, 1);
501 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
502 if (atoi(*argp
) > n
) {
503 if (atoi(*argp
) == 1)
506 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
514 * The arguments are interpreted as file names. Run them through the
515 * user's shell for wildcard expansion and other goodies. Do this from
516 * the current working directory if the argument is not an absolute path
517 * name (does not begin with a /).
519 * We feed all the file names to the shell at once, otherwise you can't
520 * provide a file name with a space in it.
522 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
523 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
524 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
525 *(strchr(shell
, '\n')) = '\0';
526 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
530 advise("popen", "could not get file from shell");
536 /* Unknown command */
537 advise (NULL
, "say what?");
546 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
548 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
551 /* Note that we do not quote -- the argp from the user
552 * is assumed to be quoted as they desire. (We can't treat
553 * it as pure literal as that would prevent them using ~,
554 * wildcards, etc.) The buffer produced by this function
555 * should be given to popen_in_dir() or system_in_dir() so
556 * that the current working directory is set correctly.
558 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
559 /* NB that some snprintf() return -1 on overflow rather than the
560 * new C99 mandated 'number of chars that would have been written'
562 /* length checks here and inside the loop allow for the
563 * trailing "&&", trailcmd, '"' and NUL
565 int trailln
= strlen(trailcmd
) + 4;
566 if (ln
< 0 || ln
+ trailln
> bufsz
)
567 adios((char *)0, "arguments too long");
571 while (*argp
&& *++argp
) {
573 /* +1 for leading space */
574 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
575 adios((char *)0, "arguments too long");
577 memcpy(cp
, *argp
, ln
+1);
581 *cp
++ = '&'; *cp
++ = '&';
582 strcpy(cp
, trailcmd
);
590 * Build a command line that causes the user's shell to list the file name
591 * arguments. This handles and wildcard expansion, tilde expansion, etc.
594 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
596 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
597 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
601 /* Like system(), but run the command in directory dir.
602 * This assumes the program is single-threaded!
605 system_in_dir(const char *dir
, const char *cmd
)
610 /* ensure that $SHELL exists, as the cmd was written relying on
611 a non-blank $SHELL... */
612 setenv("SHELL","/bin/sh",0); /* don't overwrite */
614 if (getcwd(olddir
, sizeof(olddir
)) == 0)
615 adios("getcwd", "could not get working directory");
617 adios("chdir", "could not change working directory");
619 if (chdir(olddir
) != 0)
620 adios("chdir", "could not change working directory");
624 /* ditto for popen() */
626 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
631 /* ensure that $SHELL exists, as the cmd was written relying on
632 a non-blank $SHELL... */
633 setenv("SHELL","/bin/sh",0); /* don't overwrite */
635 if (getcwd(olddir
, sizeof(olddir
)) == 0)
636 adios("getcwd", "could not get working directory");
638 adios("chdir", "could not change working directory");
639 f
= popen(cmd
, type
);
640 if (chdir(olddir
) != 0)
641 adios("chdir", "could not change working directory");
650 static int reedit
= 0; /* have we been here before? */
651 static char *edsave
= NULL
; /* the editor we used previously */
655 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
656 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
658 int pid
, status
, vecp
;
659 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
660 char *cp
, *prog
, **vec
;
665 /* Was there a previous edit session? */
666 if (reedit
&& (*ed
|| edsave
)) {
667 if (!*ed
) { /* no explicit editor */
668 *ed
= edsave
; /* so use the previous one */
669 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
672 /* unless we've specified it via "editor-next" */
673 cp
= concat (cp
, "-next", NULL
);
674 if ((cp
= context_find (cp
)) != NULL
)
678 /* set initial editor */
680 *ed
= get_default_editor();
684 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
685 strncpy (altpath
, altmsg
, sizeof(altpath
));
687 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
689 strncpy (linkpath
, LINK
, sizeof(linkpath
));
691 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
694 (void) m_unlink (linkpath
);
695 if (link (altpath
, linkpath
) == NOTOK
) {
696 if (symlink (altpath
, linkpath
) < 0) {
697 adios (linkpath
, "symlink");
706 context_save (); /* save the context file */
709 switch (pid
= fork()) {
711 advise ("fork", "unable to");
717 if (chdir (cwd
) < 0) {
718 advise (cwd
, "chdir");
723 m_putenv ("mhfolder", mp
->foldpath
);
724 m_putenv ("editalt", altpath
);
727 vec
= argsplit(*ed
, &prog
, &vecp
);
731 vec
[vecp
++] = *arg
++;
736 fprintf (stderr
, "unable to exec ");
741 if ((status
= pidwait (pid
, NOTOK
))) {
742 if (((status
& 0xff00) != 0xff00)
743 && (!reedit
|| (status
& 0x00ff))) {
744 if (!use
&& (status
& 0xff00) &&
745 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
746 advise (NULL
, "problems with edit--draft left in %s", cp
);
748 advise (NULL
, "problems with edit--%s preserved", file
);
751 status
= -2; /* maybe "reedit ? -2 : -1"? */
760 ? lstat (linkpath
, &st
) != NOTOK
761 && S_ISREG(st
.st_mode
)
762 && copyf (linkpath
, altpath
) == NOTOK
763 : stat (linkpath
, &st
) != NOTOK
765 && (m_unlink (altpath
) == NOTOK
766 || link (linkpath
, altpath
) == NOTOK
)))
767 advise (linkpath
, "unable to update %s from", altmsg
);
770 /* normally, we remember which editor we used */
772 edsave
= getcpy (*ed
);
775 if (altmsg
&& atfile
)
776 (void) m_unlink (linkpath
);
783 copyf (char *ifile
, char *ofile
)
788 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
790 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
791 admonish (ofile
, "unable to open and truncate");
796 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
797 if (write (out
, buffer
, i
) != i
) {
798 advise (ofile
, "may have damaged");
814 sendfile (char **arg
, char *file
, int pushsw
)
818 char *cp
, *sp
, **vec
, *program
;
821 * If the sendproc is the nmh command `send', then we call
822 * those routines directly rather than exec'ing the command.
824 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
826 sendit (invo_name
= sp
, arg
, file
, pushsw
);
831 context_save (); /* save the context file */
834 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
838 advise (NULL
, "unable to fork, so sending directly...");
840 vec
= argsplit(sendproc
, &program
, &vecp
);
842 vec
[vecp
++] = "-push";
845 vec
[vecp
++] = *arg
++;
849 execvp (program
, vec
);
850 fprintf (stderr
, "unable to exec ");
855 if (pidwait(child_id
, OK
) == 0)
863 * Translate MIME composition file (call buildmimeproc)
867 buildfile (char **argp
, char *file
)
874 /* allocate space for arguments */
880 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
883 * For backward compatibility, we need to add -build
884 * if we are using mhn as buildmimeproc
887 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
888 args
[i
++] = "-build";
890 /* copy any other arguments */
891 while (argp
&& *argp
)
895 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
898 return (i
? NOTOK
: OK
);
903 # define SASLminc(a) (a)
904 #else /* CYRUS_SASL */
905 # define SASLminc(a) 0
906 #endif /* CYRUS_SASL */
909 # define TLSminc(a) (a)
910 #else /* TLS_SUPPORT */
911 # define TLSminc(a) 0
912 #endif /* TLS_SUPPORT */
914 #define SEND_SWITCHES \
915 X("alias aliasfile", 0, ALIASW) \
916 X("debug", -5, DEBUGSW) \
917 X("filter filterfile", 0, FILTSW) \
918 X("nofilter", 0, NFILTSW) \
919 X("format", 0, FRMTSW) \
920 X("noformat", 0, NFRMTSW) \
921 X("forward", 0, FORWSW) \
922 X("noforward", 0, NFORWSW) \
923 X("mime", 0, MIMESW) \
924 X("nomime", 0, NMIMESW) \
925 X("msgid", 0, MSGDSW) \
926 X("nomsgid", 0, NMSGDSW) \
927 X("push", 0, SPSHSW) \
928 X("nopush", 0, NSPSHSW) \
929 X("split seconds", 0, SPLITSW) \
930 X("unique", -6, UNIQSW) \
931 X("nounique", -8, NUNIQSW) \
932 X("verbose", 0, VERBSW) \
933 X("noverbose", 0, NVERBSW) \
934 X("watch", 0, WATCSW) \
935 X("nowatch", 0, NWATCSW) \
936 X("width columns", 0, WIDTHSW) \
937 X("version", 0, SVERSIONSW) \
938 X("help", 0, SHELPSW) \
939 X("dashstuffing", -12, BITSTUFFSW) \
940 X("nodashstuffing", -14, NBITSTUFFSW) \
941 X("client host", -6, CLIESW) \
942 X("server host", 6, SERVSW) \
943 X("snoop", -5, SNOOPSW) \
944 X("draftfolder +folder", -6, SDRFSW) \
945 X("draftmessage msg", -6, SDRMSW) \
946 X("nodraftfolder", -3, SNDRFSW) \
947 X("sasl", SASLminc(-4), SASLSW) \
948 X("nosasl", SASLminc(-6), NOSASLSW) \
949 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
950 X("saslmech", SASLminc(-5), SASLMECHSW) \
951 X("user", SASLminc(-4), USERSW) \
952 X("attach fieldname", 6, SNDATTACHSW) \
953 X("noattach", 0, SNDNOATTACHSW) \
954 X("attachformat", 7, SNDATTACHFORMAT) \
955 X("port server-port-name/number", 4, PORTSW) \
956 X("tls", TLSminc(-3), TLSSW) \
957 X("initialtls", TLSminc(-10), INITTLSSW) \
958 X("notls", TLSminc(-5), NTLSSW) \
959 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
960 X("messageid localname|random", 2, MESSAGEIDSW) \
962 #define X(sw, minchars, id) id,
963 DEFINE_SWITCH_ENUM(SEND
);
966 #define X(sw, minchars, id) { sw, minchars, id },
967 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
971 extern int debugsw
; /* from sendsbr.c */
979 extern char *altmsg
; /* .. */
980 extern char *annotext
;
981 extern char *distfile
;
985 sendit (char *sp
, char **arg
, char *file
, int pushed
)
988 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
989 char **arguments
, *savearg
[MAXARGS
], **vec
;
997 * Make sure these are defined. In particular, we need
998 * savearg[1] to be NULL, in case "arg" is NULL below. It
999 * doesn't matter what is the value of savearg[0], but we
1000 * set it to NULL, to help catch "off-by-one" errors.
1006 * Temporarily copy arg to savearg, since the brkstring() call in
1007 * getarguments() will wipe it out before it is merged in.
1008 * Also, we skip the first element of savearg, since getarguments()
1009 * skips it. Then we count the number of arguments
1010 * copied. The value of "n" will be one greater than
1011 * this in order to simulate the standard argc/argv.
1016 copyip (arg
, savearg
+1, MAXARGS
-1);
1023 * Merge any arguments from command line (now in savearg)
1024 * and arguments from profile.
1026 arguments
= getarguments (sp
, n
, savearg
, 1);
1039 * Get our initial arguments for postproc now
1042 vec
= argsplit(postproc
, &program
, &vecp
);
1044 vec
[vecp
++] = "-library";
1045 vec
[vecp
++] = getcpy (m_maildir (""));
1047 if ((cp
= context_find ("fileproc"))) {
1048 vec
[vecp
++] = "-fileproc";
1052 if ((cp
= context_find ("mhlproc"))) {
1053 vec
[vecp
++] = "-mhlproc";
1057 if ((cp
= context_find ("credentials"))) {
1058 /* post doesn't read context so need to pass credentials. */
1059 vec
[vecp
++] = "-credentials";
1063 while ((cp
= *argp
++)) {
1065 switch (smatch (++cp
, sendswitches
)) {
1067 ambigsw (cp
, sendswitches
);
1070 advise (NULL
, "-%s unknown\n", cp
);
1074 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1075 print_help (buf
, sendswitches
, 1);
1078 print_version (invo_name
);
1089 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1090 advise (NULL
, "missing argument to %s", argp
[-2]);
1118 debugsw
++; /* fall */
1151 if (!(cp
= *argp
++) || *cp
== '-') {
1152 advise (NULL
, "missing argument to %s", argp
[-2]);
1160 if (!(cp
= *argp
++) || *cp
== '-') {
1161 advise (NULL
, "missing argument to %s", argp
[-2]);
1168 advise(NULL
, "The -attach switch is deprecated");
1171 advise(NULL
, "The -noattach switch is deprecated");
1174 case SNDATTACHFORMAT
:
1175 advise(NULL
, "The -attachformat switch is deprecated");
1179 advise (NULL
, "usage: %s [switches]", sp
);
1183 /* allow Aliasfile: profile entry */
1184 if ((cp
= context_find ("Aliasfile"))) {
1188 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1189 vec
[vecp
++] = "-alias";
1194 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1195 if ((cp
= context_find ("signature")) && *cp
)
1196 m_putenv ("SIGNATURE", cp
);
1198 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1200 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1202 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1203 inplace
= atoi (cp
);
1205 if ((cp
= getenv ("mhdist"))
1208 && (distsw
= atoi (cp
))
1209 #endif /* not lint */
1211 vec
[vecp
++] = "-dist";
1212 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1213 adios(NULL
, "unable to create temporary file in %s",
1216 distfile
= getcpy (cp
);
1217 (void) m_unlink(distfile
);
1218 if (link (altmsg
, distfile
) == NOTOK
)
1219 adios (distfile
, "unable to link %s to", altmsg
);
1224 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1229 if ((pushsw
= pushed
))
1234 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1) == OK
)
1243 whomfile (char **arg
, char *file
)
1247 char **vec
, *program
;
1249 context_save (); /* save the context file */
1252 switch (pid
= fork()) {
1254 advise ("fork", "unable to");
1258 vec
= argsplit(whomproc
, &program
, &vecp
);
1262 vec
[vecp
++] = *arg
++;
1265 execvp (program
, vec
);
1266 fprintf (stderr
, "unable to exec ");
1268 _exit (-1); /* NOTREACHED */
1271 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1277 * Remove the draft file
1281 removefile (char *drft
)
1283 if (m_unlink (drft
) == NOTOK
)
1284 adios (drft
, "unable to unlink");
1291 * Return 1 if we already have a MIME-Verson header, 0 otherwise.
1295 checkmimeheader (char *drft
)
1298 m_getfld_state_t gstate
= 0;
1299 char buf
[BUFSIZ
], name
[NAMESZ
];
1300 int state
, retval
= 0;
1302 if ((f
= fopen(drft
, "r")) == NULL
) {
1303 admonish(drft
, "unable to read draft");
1308 int bufsz
= sizeof(buf
);
1309 switch (state
= m_getfld(&gstate
, name
, buf
, &bufsz
, f
)) {
1312 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1313 advise(NULL
, "Cannot use attach commands with already-"
1314 "formatted MIME message \"%s\"", drft
);
1325 m_getfld_state_destroy(&gstate
);