]>
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 files This option attaches the named files to
32 * alist [-ln] This option lists the attachments on the
33 * draft. -l gets long listings, -n gets
36 * detach files This option removes attachments from the
37 * detach -n numbers draft. This can be done by file name or
38 * 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", 0, ATTACHCMDSW) \
85 X("detach [-n]", 2, 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
);
116 static int copyf (char *, char *);
121 WhatNow (int argc
, char **argv
)
123 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
124 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
125 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
126 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
127 char **argp
, **arguments
;
129 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
130 char cwd
[PATH_MAX
+ 1]; /* current working directory */
131 char file
[PATH_MAX
+ 1]; /* file name buffer */
132 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
133 FILE *f
; /* read pointer for bgnd proc */
134 char *l
; /* set on -l to alist command */
135 int n
; /* set on -n to alist command */
137 invo_name
= r1bindex (argv
[0], '/');
139 /* read user profile/context */
142 arguments
= getarguments (invo_name
, argc
, argv
, 1);
146 * Get the initial current working directory.
149 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
150 adios("getcwd", "could not get working directory");
153 while ((cp
= *argp
++)) {
155 switch (smatch (++cp
, whatnowswitches
)) {
157 ambigsw (cp
, whatnowswitches
);
160 adios (NULL
, "-%s unknown", cp
);
163 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
164 print_help (buf
, whatnowswitches
, 1);
167 print_version(invo_name
);
172 adios (NULL
, "only one draft folder at a time!");
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
175 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
176 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
180 adios (NULL
, "only one draft message at a time!");
181 if (!(dmsg
= *argp
++) || *dmsg
== '-')
182 adios (NULL
, "missing argument to %s", argp
[-2]);
190 if (!(ed
= *argp
++) || *ed
== '-')
191 adios (NULL
, "missing argument to %s", argp
[-2]);
199 if (!(myprompt
= *argp
++) || *myprompt
== '-')
200 adios (NULL
, "missing argument to %s", argp
[-2]);
204 if (!(attach
= *argp
++) || *attach
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
214 adios (NULL
, "only one draft at a time!");
219 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
220 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
222 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
224 if ((cp
= getenv ("mhatfile")) && *cp
)
227 if ((cp
= getenv ("mhuse")) && *cp
)
230 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
235 /* start editing the draft, unless -noedit was given */
236 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
237 NULL
, 1, atfile
) < 0)
240 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
242 #ifdef READLINE_SUPPORT
243 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
244 #else /* ! READLINE_SUPPORT */
245 if (!(argp
= getans (prompt
, aleqs
))) {
246 #endif /* READLINE_SUPPORT */
250 switch (smatch (*argp
, aleqs
)) {
252 /* display the message being replied to, or distributed */
254 showfile (++argp
, msgnam
);
256 advise (NULL
, "no alternate message to display");
260 /* Translate MIME composition file */
261 buildfile (++argp
, drft
);
265 /* Call an editor on the draft file */
268 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
269 NULL
, 1, atfile
) == NOTOK
)
274 /* display the draft file */
275 showfile (++argp
, drft
);
279 /* Check to whom the draft would be sent */
280 whomfile (++argp
, drft
);
284 /* Quit, and possibly delete the draft */
285 if (*++argp
&& (*argp
[0] == 'd' ||
286 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
289 if (stat (drft
, &st
) != NOTOK
)
290 advise (NULL
, "draft left on %s", drft
);
295 /* Delete draft and exit */
300 /* Send draft in background */
301 if (sendfile (++argp
, drft
, 1))
307 sendfile (++argp
, drft
, 0);
311 /* Refile the draft */
312 if (refile (++argp
, drft
) == 0)
317 /* Change the working directory for attachments
319 * Run the directory through the user's shell so that
320 * we can take advantage of any syntax that the user
321 * is accustomed to. Read back the absolute path.
324 if (*(argp
+1) == (char *)0) {
325 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
328 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
330 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
331 fgets(cwd
, sizeof (cwd
), f
);
333 if (strchr(cwd
, '\n') != (char *)0)
334 *strchr(cwd
, '\n') = '\0';
339 advise("popen", "could not get directory");
345 /* Print the working directory for attachments */
350 /* List files in the current attachment working directory
352 * Use the user's shell so that we can take advantage of any
353 * syntax that the user is accustomed to.
355 writelscmd(buf
, sizeof(buf
), "", argp
);
356 (void)system_in_dir(cwd
, buf
);
361 * List attachments on current draft. Options are:
363 * -l long listing (full path names)
367 if (attach
== (char *)0) {
368 advise((char *)0, "can't list because no header field name was given.");
375 while (*++argp
!= (char *)0) {
376 if (strcmp(*argp
, "-l") == 0)
379 else if (strcmp(*argp
, "-n") == 0)
382 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
394 advise((char *)0, "usage is alist [-ln].");
397 annolist(drft
, attach
, l
, n
);
403 * Attach files to current draft.
406 if (attach
== (char *)0) {
407 advise((char *)0, "can't attach because no header field name was given.");
411 if (*(argp
+1) == (char *)0) {
412 advise((char *)0, "attach command requires file argument(s).");
417 * Build a command line that causes the user's shell to list the file name
418 * arguments. This handles and wildcard expansion, tilde expansion, etc.
420 writelscmd(buf
, sizeof(buf
), "-d", argp
);
423 * Read back the response from the shell, which contains a number of lines
424 * with one file name per line. Remove off the newline. Determine whether
425 * we have an absolute or relative path name. Prepend the current working
426 * directory to relative path names. Add the attachment annotation to the
430 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
431 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
432 *(strchr(shell
, '\n')) = '\0';
435 (void)annotate(drft
, attach
, shell
, 1, 0, -2, 1);
437 (void)sprintf(file
, "%s/%s", cwd
, shell
);
438 (void)annotate(drft
, attach
, file
, 1, 0, -2, 1);
445 advise("popen", "could not get file from shell");
452 * Detach files from current draft.
455 if (attach
== (char *)0) {
456 advise((char *)0, "can't detach because no header field name was given.");
461 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
462 * so this catches a -n anywhere in the argument list.
465 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
466 if (strcmp(*arguments
, "-n") == 0) {
473 * A -n was found so interpret the arguments as attachment numbers.
474 * Decrement any remaining argument number that is greater than the one
475 * just processed after processing each one so that the numbering stays
480 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
481 if (strcmp(*arguments
, "-n") == 0)
484 if (**arguments
!= '\0') {
485 n
= atoi(*arguments
);
486 (void)annotate(drft
, attach
, (char *)0, 1, 0, n
, 1);
488 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
489 if (atoi(*argp
) > n
) {
490 if (atoi(*argp
) == 1)
493 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
501 * The arguments are interpreted as file names. Run them through the
502 * user's shell for wildcard expansion and other goodies. Do this from
503 * the current working directory if the argument is not an absolute path
504 * name (does not begin with a /).
506 * We feed all the file names to the shell at once, otherwise you can't
507 * provide a file name with a space in it.
509 writelscmd(buf
, sizeof(buf
), "-d", argp
);
510 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
511 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
512 *(strchr(shell
, '\n')) = '\0';
513 (void)annotate(drft
, attach
, shell
, 1, 0, 0, 1);
517 advise("popen", "could not get file from shell");
523 /* Unknown command */
524 advise (NULL
, "say what?");
533 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
535 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
538 /* Note that we do not quote -- the argp from the user
539 * is assumed to be quoted as they desire. (We can't treat
540 * it as pure literal as that would prevent them using ~,
541 * wildcards, etc.) The buffer produced by this function
542 * should be given to popen_in_dir() or system_in_dir() so
543 * that the current working directory is set correctly.
545 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
546 /* NB that some snprintf() return -1 on overflow rather than the
547 * new C99 mandated 'number of chars that would have been written'
549 /* length checks here and inside the loop allow for the
550 * trailing "&&", trailcmd, '"' and NUL
552 int trailln
= strlen(trailcmd
) + 4;
553 if (ln
< 0 || ln
+ trailln
> bufsz
)
554 adios((char *)0, "arguments too long");
558 while (*argp
&& *++argp
) {
560 /* +1 for leading space */
561 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
562 adios((char *)0, "arguments too long");
564 memcpy(cp
, *argp
, ln
+1);
568 *cp
++ = '&'; *cp
++ = '&';
569 strcpy(cp
, trailcmd
);
577 * Build a command line that causes the user's shell to list the file name
578 * arguments. This handles and wildcard expansion, tilde expansion, etc.
581 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
583 char *lscmd
= concat ("ls ", lsoptions
, " --", NULL
);
584 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
588 /* Like system(), but run the command in directory dir.
589 * This assumes the program is single-threaded!
592 system_in_dir(const char *dir
, const char *cmd
)
597 /* ensure that $SHELL exists, as the cmd was written relying on
598 a non-blank $SHELL... */
599 setenv("SHELL","/bin/sh",0); /* don't overwrite */
601 if (getcwd(olddir
, sizeof(olddir
)) == 0)
602 adios("getcwd", "could not get working directory");
604 adios("chdir", "could not change working directory");
606 if (chdir(olddir
) != 0)
607 adios("chdir", "could not change working directory");
611 /* ditto for popen() */
613 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
618 /* ensure that $SHELL exists, as the cmd was written relying on
619 a non-blank $SHELL... */
620 setenv("SHELL","/bin/sh",0); /* don't overwrite */
622 if (getcwd(olddir
, sizeof(olddir
)) == 0)
623 adios("getcwd", "could not get working directory");
625 adios("chdir", "could not change working directory");
626 f
= popen(cmd
, type
);
627 if (chdir(olddir
) != 0)
628 adios("chdir", "could not change working directory");
637 static int reedit
= 0; /* have we been here before? */
638 static char *edsave
= NULL
; /* the editor we used previously */
642 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
643 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
645 int pid
, status
, vecp
;
646 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
647 char *cp
, *prog
, **vec
;
652 #endif /* HAVE_LSTAT */
654 /* Was there a previous edit session? */
656 if (!*ed
) { /* no explicit editor */
657 *ed
= edsave
; /* so use the previous one */
658 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
661 /* unless we've specified it via "editor-next" */
662 cp
= concat (cp
, "-next", NULL
);
663 if ((cp
= context_find (cp
)) != NULL
)
667 /* set initial editor */
669 *ed
= get_default_editor();
673 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
674 strncpy (altpath
, altmsg
, sizeof(altpath
));
676 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
678 strncpy (linkpath
, LINK
, sizeof(linkpath
));
680 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
685 if (link (altpath
, linkpath
) == NOTOK
) {
686 symlink (altpath
, linkpath
);
691 #else /* not HAVE_LSTAT */
692 link (altpath
, linkpath
);
693 #endif /* not HAVE_LSTAT */
697 context_save (); /* save the context file */
700 switch (pid
= fork()) {
702 advise ("fork", "unable to");
711 m_putenv ("mhfolder", mp
->foldpath
);
712 m_putenv ("editalt", altpath
);
715 vec
= argsplit(*ed
, &prog
, &vecp
);
719 vec
[vecp
++] = *arg
++;
724 fprintf (stderr
, "unable to exec ");
729 if ((status
= pidwait (pid
, NOTOK
))) {
730 if (((status
& 0xff00) != 0xff00)
731 && (!reedit
|| (status
& 0x00ff))) {
732 if (!use
&& (status
& 0xff00) &&
733 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
734 advise (NULL
, "problems with edit--draft left in %s", cp
);
736 advise (NULL
, "problems with edit--%s preserved", file
);
739 status
= -2; /* maybe "reedit ? -2 : -1"? */
749 ? lstat (linkpath
, &st
) != NOTOK
750 && S_ISREG(st
.st_mode
)
751 && copyf (linkpath
, altpath
) == NOTOK
752 : stat (linkpath
, &st
) != NOTOK
754 && (unlink (altpath
) == NOTOK
755 || link (linkpath
, altpath
) == NOTOK
)))
756 advise (linkpath
, "unable to update %s from", altmsg
);
757 #else /* HAVE_LSTAT */
761 && stat (linkpath
, &st
) != NOTOK
763 && (unlink (altpath
) == NOTOK
764 || link (linkpath
, altpath
) == NOTOK
))
765 advise (linkpath
, "unable to update %s from", altmsg
);
766 #endif /* HAVE_LSTAT */
769 /* normally, we remember which editor we used */
771 edsave
= getcpy (*ed
);
774 if (altmsg
&& atfile
)
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");
807 #endif /* HAVE_LSTAT */
815 sendfile (char **arg
, char *file
, int pushsw
)
819 char *cp
, *sp
, **vec
, *program
;
821 /* Translate MIME composition file, if necessary */
822 if ((cp
= context_find ("automimeproc"))
823 && (!strcmp (cp
, "1"))
824 && check_draft (file
)
825 && (buildfile (NULL
, file
) == NOTOK
))
828 /* For backwards compatibility */
829 if ((cp
= context_find ("automhnproc"))
830 && check_draft (file
)
831 && (i
= editfile (&cp
, NULL
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0)))
835 * If the sendproc is the nmh command `send', then we call
836 * those routines directly rather than exec'ing the command.
838 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
840 sendit (invo_name
= sp
, arg
, file
, pushsw
);
845 context_save (); /* save the context file */
848 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
852 advise (NULL
, "unable to fork, so sending directly...");
854 vec
= argsplit(sendproc
, &program
, &vecp
);
856 vec
[vecp
++] = "-push";
859 vec
[vecp
++] = *arg
++;
863 execvp (program
, vec
);
864 fprintf (stderr
, "unable to exec ");
869 if (pidwait(child_id
, OK
) == 0)
877 * Translate MIME composition file (call buildmimeproc)
881 buildfile (char **argp
, char *file
)
888 /* allocate space for arguments */
894 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
897 * For backward compatibility, we need to add -build
898 * if we are using mhn as buildmimeproc
901 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
902 args
[i
++] = "-build";
904 /* copy any other arguments */
905 while (argp
&& *argp
)
909 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
912 return (i
? NOTOK
: OK
);
917 * Check if draft is a mhbuild composition file
921 check_draft (char *msgnam
)
924 char buf
[BUFSIZ
], name
[NAMESZ
];
926 m_getfld_state_t gstate
= 0;
928 if ((fp
= fopen (msgnam
, "r")) == NULL
)
931 int bufsz
= sizeof buf
;
932 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
936 * If draft already contains any of the
937 * Content-XXX fields, then assume it already
940 if (uprf (name
, XXX_FIELD_PRF
)) {
942 m_getfld_state_destroy (&gstate
);
945 while (state
== FLDPLUS
) {
947 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
955 for (bp
= buf
; *bp
; bp
++)
956 if (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n') {
958 m_getfld_state_destroy (&gstate
);
963 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
964 } while (state
== BODY
);
969 m_getfld_state_destroy (&gstate
);
977 # define SASLminc(a) (a)
978 #else /* CYRUS_SASL */
979 # define SASLminc(a) 0
980 #endif /* CYRUS_SASL */
983 # define TLSminc(a) (a)
984 #else /* TLS_SUPPORT */
985 # define TLSminc(a) 0
986 #endif /* TLS_SUPPORT */
988 #define SEND_SWITCHES \
989 X("alias aliasfile", 0, ALIASW) \
990 X("debug", -5, DEBUGSW) \
991 X("filter filterfile", 0, FILTSW) \
992 X("nofilter", 0, NFILTSW) \
993 X("format", 0, FRMTSW) \
994 X("noformat", 0, NFRMTSW) \
995 X("forward", 0, FORWSW) \
996 X("noforward", 0, NFORWSW) \
997 X("mime", 0, MIMESW) \
998 X("nomime", 0, NMIMESW) \
999 X("msgid", 0, MSGDSW) \
1000 X("nomsgid", 0, NMSGDSW) \
1001 X("push", 0, SPSHSW) \
1002 X("nopush", 0, NSPSHSW) \
1003 X("split seconds", 0, SPLITSW) \
1004 X("unique", -6, UNIQSW) \
1005 X("nounique", -8, NUNIQSW) \
1006 X("verbose", 0, VERBSW) \
1007 X("noverbose", 0, NVERBSW) \
1008 X("watch", 0, WATCSW) \
1009 X("nowatch", 0, NWATCSW) \
1010 X("width columns", 0, WIDTHSW) \
1011 X("version", 0, SVERSIONSW) \
1012 X("help", 0, SHELPSW) \
1013 X("dashstuffing", -12, BITSTUFFSW) \
1014 X("nodashstuffing", -14, NBITSTUFFSW) \
1015 X("client host", -6, CLIESW) \
1016 X("server host", 6, SERVSW) \
1017 X("snoop", -5, SNOOPSW) \
1018 X("draftfolder +folder", -6, SDRFSW) \
1019 X("draftmessage msg", -6, SDRMSW) \
1020 X("nodraftfolder", -3, SNDRFSW) \
1021 X("sasl", SASLminc(-4), SASLSW) \
1022 X("nosasl", SASLminc(-6), NOSASLSW) \
1023 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
1024 X("saslmech", SASLminc(-5), SASLMECHSW) \
1025 X("user", SASLminc(-4), USERSW) \
1026 X("attach file", 6, SNDATTACHSW) \
1027 X("noattach", 0, SNDNOATTACHSW) \
1028 X("attachformat", 7, SNDATTACHFORMAT) \
1029 X("port server-port-name/number", 4, PORTSW) \
1030 X("tls", TLSminc(-3), TLSSW) \
1031 X("notls", TLSminc(-5), NTLSSW) \
1032 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
1033 X("messageid localname|random", 2, MESSAGEIDSW) \
1035 #define X(sw, minchars, id) id,
1036 DEFINE_SWITCH_ENUM(SEND
);
1039 #define X(sw, minchars, id) { sw, minchars, id },
1040 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
1044 extern int debugsw
; /* from sendsbr.c */
1052 extern char *altmsg
; /* .. */
1053 extern char *annotext
;
1054 extern char *distfile
;
1058 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1061 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1062 char **arguments
, *savearg
[MAXARGS
], **vec
;
1064 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
1065 int attachformat
= 1; /* mhbuild format specifier for
1073 * Make sure these are defined. In particular, we need
1074 * savearg[1] to be NULL, in case "arg" is NULL below. It
1075 * doesn't matter what is the value of savearg[0], but we
1076 * set it to NULL, to help catch "off-by-one" errors.
1082 * Temporarily copy arg to savearg, since the brkstring() call in
1083 * getarguments() will wipe it out before it is merged in.
1084 * Also, we skip the first element of savearg, since getarguments()
1085 * skips it. Then we count the number of arguments
1086 * copied. The value of "n" will be one greater than
1087 * this in order to simulate the standard argc/argv.
1092 copyip (arg
, savearg
+1, MAXARGS
-1);
1099 * Merge any arguments from command line (now in savearg)
1100 * and arguments from profile.
1102 arguments
= getarguments (sp
, n
, savearg
, 1);
1115 * Get our initial arguments for postproc now
1118 vec
= argsplit(postproc
, &program
, &vecp
);
1120 vec
[vecp
++] = "-library";
1121 vec
[vecp
++] = getcpy (m_maildir (""));
1123 if ((cp
= context_find ("fileproc"))) {
1124 vec
[vecp
++] = "-fileproc";
1128 if ((cp
= context_find ("mhlproc"))) {
1129 vec
[vecp
++] = "-mhlproc";
1133 while ((cp
= *argp
++)) {
1135 switch (smatch (++cp
, sendswitches
)) {
1137 ambigsw (cp
, sendswitches
);
1140 advise (NULL
, "-%s unknown\n", cp
);
1144 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1145 print_help (buf
, sendswitches
, 1);
1148 print_version (invo_name
);
1159 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1160 advise (NULL
, "missing argument to %s", argp
[-2]);
1188 debugsw
++; /* fall */
1220 if (!(cp
= *argp
++) || *cp
== '-') {
1221 advise (NULL
, "missing argument to %s", argp
[-2]);
1229 if (!(cp
= *argp
++) || *cp
== '-') {
1230 advise (NULL
, "missing argument to %s", argp
[-2]);
1237 if (!(attach
= *argp
++) || *attach
== '-') {
1238 advise (NULL
, "missing argument to %s", argp
[-2]);
1246 case SNDATTACHFORMAT
:
1247 if (! *argp
|| **argp
== '-')
1248 adios (NULL
, "missing argument to %s", argp
[-1]);
1250 attachformat
= atoi (*argp
);
1251 if (attachformat
< 0 ||
1252 attachformat
> ATTACHFORMATS
- 1) {
1253 advise (NULL
, "unsupported attachformat %d",
1262 advise (NULL
, "usage: %s [switches]", sp
);
1266 /* allow Aliasfile: profile entry */
1267 if ((cp
= context_find ("Aliasfile"))) {
1271 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1272 vec
[vecp
++] = "-alias";
1277 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1278 if ((cp
= context_find ("signature")) && *cp
)
1279 m_putenv ("SIGNATURE", cp
);
1281 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1283 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1285 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1286 inplace
= atoi (cp
);
1288 if ((cp
= getenv ("mhdist"))
1291 && (distsw
= atoi (cp
))
1292 #endif /* not lint */
1294 vec
[vecp
++] = "-dist";
1295 distfile
= getcpy (m_mktemp2(altmsg
, invo_name
, NULL
, NULL
));
1297 if (link (altmsg
, distfile
) == NOTOK
)
1298 adios (distfile
, "unable to link %s to", altmsg
);
1303 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1308 if ((pushsw
= pushed
))
1313 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, attach
, attachformat
) == OK
)
1322 whomfile (char **arg
, char *file
)
1326 char **vec
, *program
;
1328 context_save (); /* save the context file */
1331 switch (pid
= fork()) {
1333 advise ("fork", "unable to");
1337 vec
= argsplit(whomproc
, &program
, &vecp
);
1341 vec
[vecp
++] = *arg
++;
1344 execvp (program
, vec
);
1345 fprintf (stderr
, "unable to exec ");
1347 _exit (-1); /* NOTREACHED */
1350 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1356 * Remove the draft file
1360 removefile (char *drft
)
1362 if (unlink (drft
) == NOTOK
)
1363 adios (drft
, "unable to unlink");