]>
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 static struct swit whatnowswitches
[] = {
49 { "draftfolder +folder", 0 },
51 { "draftmessage msg", 0 },
53 { "nodraftfolder", 0 },
55 { "editor editor", 0 },
59 { "prompt string", 4 },
65 { "attach header-field-name", 0 },
72 * Options at the "whatnow" prompt
74 static struct swit aleqs
[] = {
76 { "edit [<editor> <switches>]", 0 },
78 { "refile [<switches>] +folder", 0 },
80 { "mime [<switches>]", 0 },
82 { "display [<switches>]", 0 },
84 { "list [<switches>]", 0 },
86 { "send [<switches>]", 0 },
88 { "push [<switches>]", 0 },
90 { "whom [<switches>]", 0 },
92 { "quit [-delete]", 0 },
96 { "cd [directory]", 0 },
101 #define ATTACHCMDSW 13
103 #define DETACHCMDSW 14
104 { "detach [-n]", 2 },
105 #define ALISTCMDSW 15
106 { "alist [-ln] ", 2 },
110 static char *myprompt
= "\nWhat now? ";
115 static int editfile (char **, char **, char *, int, struct msgs
*,
116 char *, char *, int, int);
117 static int sendfile (char **, char *, int);
118 static void sendit (char *, char **, char *, int);
119 static int buildfile (char **, char *);
120 static int check_draft (char *);
121 static int whomfile (char **, char *);
122 static int removefile (char *);
123 static void writelscmd(char *, int, char *, char **);
124 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
125 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
126 static int system_in_dir(const char *dir
, const char *cmd
);
130 static int copyf (char *, char *);
135 WhatNow (int argc
, char **argv
)
137 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
138 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
139 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
140 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
141 char **argp
, **arguments
;
143 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
144 char cwd
[PATH_MAX
+ 1]; /* current working directory */
145 char file
[PATH_MAX
+ 1]; /* file name buffer */
146 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
147 FILE *f
; /* read pointer for bgnd proc */
148 char *l
; /* set on -l to alist command */
149 int n
; /* set on -n to alist command */
151 invo_name
= r1bindex (argv
[0], '/');
153 /* read user profile/context */
156 arguments
= getarguments (invo_name
, argc
, argv
, 1);
160 * Get the initial current working directory.
163 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
164 adios("getcwd", "could not get working directory");
167 while ((cp
= *argp
++)) {
169 switch (smatch (++cp
, whatnowswitches
)) {
171 ambigsw (cp
, whatnowswitches
);
174 adios (NULL
, "-%s unknown", cp
);
177 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
178 print_help (buf
, whatnowswitches
, 1);
181 print_version(invo_name
);
186 adios (NULL
, "only one draft folder at a time!");
187 if (!(cp
= *argp
++) || *cp
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
189 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
190 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
194 adios (NULL
, "only one draft message at a time!");
195 if (!(dmsg
= *argp
++) || *dmsg
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
204 if (!(ed
= *argp
++) || *ed
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
213 if (!(myprompt
= *argp
++) || *myprompt
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
218 if (!(attach
= *argp
++) || *attach
== '-')
219 adios (NULL
, "missing argument to %s", argp
[-2]);
228 adios (NULL
, "only one draft at a time!");
233 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
234 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
236 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
238 if ((cp
= getenv ("mhatfile")) && *cp
)
241 if ((cp
= getenv ("mhuse")) && *cp
)
244 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
249 /* start editing the draft, unless -noedit was given */
250 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
251 NULL
, 1, atfile
) < 0)
254 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
256 #ifdef READLINE_SUPPORT
257 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
258 #else /* ! READLINE_SUPPORT */
259 if (!(argp
= getans (prompt
, aleqs
))) {
260 #endif /* READLINE_SUPPORT */
264 switch (smatch (*argp
, aleqs
)) {
266 /* display the message being replied to, or distributed */
268 showfile (++argp
, msgnam
);
270 advise (NULL
, "no alternate message to display");
274 /* Translate MIME composition file */
275 buildfile (++argp
, drft
);
279 /* Call an editor on the draft file */
282 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
283 NULL
, 1, atfile
) == NOTOK
)
288 /* display the draft file */
289 showfile (++argp
, drft
);
293 /* Check to whom the draft would be sent */
294 whomfile (++argp
, drft
);
298 /* Quit, and possibly delete the draft */
299 if (*++argp
&& (*argp
[0] == 'd' ||
300 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
303 if (stat (drft
, &st
) != NOTOK
)
304 advise (NULL
, "draft left on %s", drft
);
309 /* Delete draft and exit */
314 /* Send draft in background */
315 if (sendfile (++argp
, drft
, 1))
321 sendfile (++argp
, drft
, 0);
325 /* Refile the draft */
326 if (refile (++argp
, drft
) == 0)
331 /* Change the working directory for attachments
333 * Run the directory through the user's shell so that
334 * we can take advantage of any syntax that the user
335 * is accustomed to. Read back the absolute path.
338 if (*(argp
+1) == (char *)0) {
339 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
342 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
344 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
345 fgets(cwd
, sizeof (cwd
), f
);
347 if (strchr(cwd
, '\n') != (char *)0)
348 *strchr(cwd
, '\n') = '\0';
353 advise("popen", "could not get directory");
359 /* Print the working directory for attachments */
364 /* List files in the current attachment working directory
366 * Use the user's shell so that we can take advantage of any
367 * syntax that the user is accustomed to.
369 writelscmd(buf
, sizeof(buf
), "", argp
);
370 (void)system_in_dir(cwd
, buf
);
375 * List attachments on current draft. Options are:
377 * -l long listing (full path names)
381 if (attach
== (char *)0) {
382 advise((char *)0, "can't list because no header field name was given.");
389 while (*++argp
!= (char *)0) {
390 if (strcmp(*argp
, "-l") == 0)
393 else if (strcmp(*argp
, "-n") == 0)
396 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
408 advise((char *)0, "usage is alist [-ln].");
411 annolist(drft
, attach
, l
, n
);
417 * Attach files to current draft.
420 if (attach
== (char *)0) {
421 advise((char *)0, "can't attach because no header field name was given.");
425 if (*(argp
+1) == (char *)0) {
426 advise((char *)0, "attach command requires file argument(s).");
431 * Build a command line that causes the user's shell to list the file name
432 * arguments. This handles and wildcard expansion, tilde expansion, etc.
434 writelscmd(buf
, sizeof(buf
), "-d", argp
);
437 * Read back the response from the shell, which contains a number of lines
438 * with one file name per line. Remove off the newline. Determine whether
439 * we have an absolute or relative path name. Prepend the current working
440 * directory to relative path names. Add the attachment annotation to the
444 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
445 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
446 *(strchr(shell
, '\n')) = '\0';
449 (void)annotate(drft
, attach
, shell
, 1, 0, -2, 1);
451 (void)sprintf(file
, "%s/%s", cwd
, shell
);
452 (void)annotate(drft
, attach
, file
, 1, 0, -2, 1);
459 advise("popen", "could not get file from shell");
466 * Detach files from current draft.
469 if (attach
== (char *)0) {
470 advise((char *)0, "can't detach because no header field name was given.");
475 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
476 * so this catches a -n anywhere in the argument list.
479 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
480 if (strcmp(*arguments
, "-n") == 0) {
487 * A -n was found so interpret the arguments as attachment numbers.
488 * Decrement any remaining argument number that is greater than the one
489 * just processed after processing each one so that the numbering stays
494 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
495 if (strcmp(*arguments
, "-n") == 0)
498 if (**arguments
!= '\0') {
499 n
= atoi(*arguments
);
500 (void)annotate(drft
, attach
, (char *)0, 1, 0, n
, 1);
502 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
503 if (atoi(*argp
) > n
) {
504 if (atoi(*argp
) == 1)
507 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
515 * The arguments are interpreted as file names. Run them through the
516 * user's shell for wildcard expansion and other goodies. Do this from
517 * the current working directory if the argument is not an absolute path
518 * name (does not begin with a /).
520 * We feed all the file names to the shell at once, otherwise you can't
521 * provide a file name with a space in it.
523 writelscmd(buf
, sizeof(buf
), "-d", argp
);
524 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
525 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
526 *(strchr(shell
, '\n')) = '\0';
527 (void)annotate(drft
, attach
, shell
, 1, 0, 0, 1);
531 advise("popen", "could not get file from shell");
537 /* Unknown command */
538 advise (NULL
, "say what?");
547 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
549 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
552 /* Note that we do not quote -- the argp from the user
553 * is assumed to be quoted as they desire. (We can't treat
554 * it as pure literal as that would prevent them using ~,
555 * wildcards, etc.) The buffer produced by this function
556 * should be given to popen_in_dir() or system_in_dir() so
557 * that the current working directory is set correctly.
559 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
560 /* NB that some snprintf() return -1 on overflow rather than the
561 * new C99 mandated 'number of chars that would have been written'
563 /* length checks here and inside the loop allow for the
564 * trailing "&&", trailcmd, '"' and NUL
566 int trailln
= strlen(trailcmd
) + 4;
567 if (ln
< 0 || ln
+ trailln
> bufsz
)
568 adios((char *)0, "arguments too long");
572 while (*++argp
!= (char *)0) {
574 /* +1 for leading space */
575 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
576 adios((char *)0, "arguments too long");
578 memcpy(cp
, *argp
, ln
+1);
582 *cp
++ = '&'; *cp
++ = '&';
583 strcpy(cp
, trailcmd
);
591 * Build a command line that causes the user's shell to list the file name
592 * arguments. This handles and wildcard expansion, tilde expansion, etc.
595 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
597 char *lscmd
= concat ("ls ", lsoptions
, " --", NULL
);
598 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
602 /* Like system(), but run the command in directory dir.
603 * This assumes the program is single-threaded!
606 system_in_dir(const char *dir
, const char *cmd
)
611 /* ensure that $SHELL exists, as the cmd was written relying on
612 a non-blank $SHELL... */
613 setenv("SHELL","/bin/sh",0); /* don't overwrite */
615 if (getcwd(olddir
, sizeof(olddir
)) == 0)
616 adios("getcwd", "could not get working directory");
618 adios("chdir", "could not change working directory");
620 if (chdir(olddir
) != 0)
621 adios("chdir", "could not change working directory");
625 /* ditto for popen() */
627 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
632 /* ensure that $SHELL exists, as the cmd was written relying on
633 a non-blank $SHELL... */
634 setenv("SHELL","/bin/sh",0); /* don't overwrite */
636 if (getcwd(olddir
, sizeof(olddir
)) == 0)
637 adios("getcwd", "could not get working directory");
639 adios("chdir", "could not change working directory");
640 f
= popen(cmd
, type
);
641 if (chdir(olddir
) != 0)
642 adios("chdir", "could not change working directory");
651 static int reedit
= 0; /* have we been here before? */
652 static char *edsave
= NULL
; /* the editor we used previously */
656 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
657 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
659 int pid
, status
, vecp
;
660 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
661 char *cp
, *vec
[MAXARGS
];
666 #endif /* HAVE_LSTAT */
668 /* Was there a previous edit session? */
670 if (!*ed
) { /* no explicit editor */
671 *ed
= edsave
; /* so use the previous one */
672 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
675 /* unless we've specified it via "editor-next" */
676 cp
= concat (cp
, "-next", NULL
);
677 if ((cp
= context_find (cp
)) != NULL
)
681 /* set initial editor */
682 if (*ed
== NULL
&& (*ed
= context_find ("editor")) == NULL
)
686 if (altmsg
&& atfile
) {
687 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
688 strncpy (altpath
, altmsg
, sizeof(altpath
));
690 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
692 strncpy (linkpath
, LINK
, sizeof(linkpath
));
694 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
698 if (link (altpath
, linkpath
) == NOTOK
) {
699 symlink (altpath
, linkpath
);
704 #else /* not HAVE_LSTAT */
705 link (altpath
, linkpath
);
706 #endif /* not HAVE_LSTAT */
709 context_save (); /* save the context file */
712 switch (pid
= vfork()) {
714 advise ("fork", "unable to");
723 m_putenv ("mhfolder", mp
->foldpath
);
724 m_putenv ("editalt", altpath
);
728 vec
[vecp
++] = r1bindex (*ed
, '/');
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"? */
761 ? lstat (linkpath
, &st
) != NOTOK
762 && S_ISREG(st
.st_mode
)
763 && copyf (linkpath
, altpath
) == NOTOK
764 : stat (linkpath
, &st
) != NOTOK
766 && (unlink (altpath
) == NOTOK
767 || link (linkpath
, altpath
) == NOTOK
)))
768 advise (linkpath
, "unable to update %s from", altmsg
);
769 #else /* HAVE_LSTAT */
773 && stat (linkpath
, &st
) != NOTOK
775 && (unlink (altpath
) == NOTOK
776 || link (linkpath
, altpath
) == NOTOK
))
777 advise (linkpath
, "unable to update %s from", altmsg
);
778 #endif /* HAVE_LSTAT */
781 /* normally, we remember which editor we used */
783 edsave
= getcpy (*ed
);
786 if (altmsg
&& atfile
)
795 copyf (char *ifile
, char *ofile
)
800 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
802 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
803 admonish (ofile
, "unable to open and truncate");
808 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
809 if (write (out
, buffer
, i
) != i
) {
810 advise (ofile
, "may have damaged");
819 #endif /* HAVE_LSTAT */
827 sendfile (char **arg
, char *file
, int pushsw
)
831 char *cp
, *sp
, *vec
[MAXARGS
];
833 /* Translate MIME composition file, if necessary */
834 if ((cp
= context_find ("automimeproc"))
835 && (!strcmp (cp
, "1"))
836 && check_draft (file
)
837 && (buildfile (NULL
, file
) == NOTOK
))
840 /* For backwards compatibility */
841 if ((cp
= context_find ("automhnproc"))
842 && check_draft (file
)
843 && (i
= editfile (&cp
, NULL
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0)))
847 * If the sendproc is the nmh command `send', then we call
848 * those routines directly rather than exec'ing the command.
850 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
852 sendit (invo_name
= sp
, arg
, file
, pushsw
);
857 context_save (); /* save the context file */
860 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
864 advise (NULL
, "unable to fork, so sending directly...");
867 vec
[vecp
++] = invo_name
;
869 vec
[vecp
++] = "-push";
872 vec
[vecp
++] = *arg
++;
876 execvp (sendproc
, vec
);
877 fprintf (stderr
, "unable to exec ");
882 if (pidwait(child_id
, OK
) == 0)
890 * Translate MIME composition file (call buildmimeproc)
894 buildfile (char **argp
, char *file
)
901 /* allocate space for arguments */
907 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
910 * For backward compatibility, we need to add -build
911 * if we are using mhn as buildmimeproc
914 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
915 args
[i
++] = "-build";
917 /* copy any other arguments */
918 while (argp
&& *argp
)
922 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
925 return (i
? NOTOK
: OK
);
930 * Check if draft is a mhbuild composition file
934 check_draft (char *msgnam
)
937 char buf
[BUFSIZ
], name
[NAMESZ
];
940 if ((fp
= fopen (msgnam
, "r")) == NULL
)
943 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
948 * If draft already contains any of the
949 * Content-XXX fields, then assume it already
952 if (uprf (name
, XXX_FIELD_PRF
)) {
956 while (state
== FLDPLUS
)
957 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
964 for (bp
= buf
; *bp
; bp
++)
965 if (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n') {
970 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
971 } while (state
== BODY
);
982 # define SASLminc(a) (a)
983 #else /* CYRUS_SASL */
984 # define SASLminc(a) 0
985 #endif /* CYRUS_SASL */
988 # define TLSminc(a) (a)
989 #else /* TLS_SUPPORT */
990 # define TLSminc(a) 0
991 #endif /* TLS_SUPPORT */
993 static struct swit sendswitches
[] = {
995 { "alias aliasfile", 0 },
999 { "filter filterfile", 0 },
1023 { "split seconds", 0 },
1037 { "width columns", 0 },
1038 #define SVERSIONSW 22
1042 #define BITSTUFFSW 24
1043 { "dashstuffing", -12 },
1044 #define NBITSTUFFSW 25
1045 { "nodashstuffing", -14 },
1055 { "client host", -6 },
1057 { "server host", 6 },
1061 { "draftfolder +folder", -6 },
1063 { "draftmessage msg", -6 },
1065 { "nodraftfolder", -3 },
1067 { "sasl", SASLminc(-4) },
1069 { "nosasl", SASLminc(-6) },
1070 #define SASLMXSSFSW 38
1071 { "saslmaxssf", SASLminc(-10) },
1072 #define SASLMECHSW 39
1073 { "saslmech", SASLminc(-5) },
1075 { "user", SASLminc(-4) },
1076 #define SNDATTACHSW 41
1077 { "attach file", 6 },
1078 #define SNDNOATTACHSW 42
1080 #define SNDATTACHFORMAT 43
1081 { "attachformat", 7 },
1083 { "port server-port-name/number", 4 },
1085 { "tls", TLSminc(-3) },
1087 { "notls", TLSminc(-5) },
1089 { "mts smtp|sendmail/smtp|sendmail/pipe", 2 },
1090 #define MESSAGEIDSW 48
1091 { "messageid localname|random", 2 },
1096 extern int debugsw
; /* from sendsbr.c */
1104 extern char *altmsg
; /* .. */
1105 extern char *annotext
;
1106 extern char *distfile
;
1110 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1113 char *cp
, buf
[BUFSIZ
], **argp
;
1114 char **arguments
, *vec
[MAXARGS
];
1116 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
1117 int attachformat
= 1; /* mhbuild format specifier for
1125 * Make sure these are defined. In particular, we need
1126 * vec[1] to be NULL, in case "arg" is NULL below. It
1127 * doesn't matter what is the value of vec[0], but we
1128 * set it to NULL, to help catch "off-by-one" errors.
1134 * Temporarily copy arg to vec, since the brkstring() call in
1135 * getarguments() will wipe it out before it is merged in.
1136 * Also, we skip the first element of vec, since getarguments()
1137 * skips it. Then we count the number of arguments
1138 * copied. The value of "n" will be one greater than
1139 * this in order to simulate the standard argc/argv.
1144 copyip (arg
, vec
+1, MAXARGS
-1);
1151 * Merge any arguments from command line (now in vec)
1152 * and arguments from profile.
1154 arguments
= getarguments (sp
, n
, vec
, 1);
1166 vecp
= 1; /* we'll get the zero'th element later */
1167 vec
[vecp
++] = "-library";
1168 vec
[vecp
++] = getcpy (m_maildir (""));
1170 if ((cp
= context_find ("fileproc"))) {
1171 vec
[vecp
++] = "-fileproc";
1175 if ((cp
= context_find ("mhlproc"))) {
1176 vec
[vecp
++] = "-mhlproc";
1180 while ((cp
= *argp
++)) {
1182 switch (smatch (++cp
, sendswitches
)) {
1184 ambigsw (cp
, sendswitches
);
1187 advise (NULL
, "-%s unknown\n", cp
);
1191 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1192 print_help (buf
, sendswitches
, 1);
1195 print_version (invo_name
);
1206 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1207 advise (NULL
, "missing argument to %s", argp
[-2]);
1235 debugsw
++; /* fall */
1271 if (!(cp
= *argp
++) || *cp
== '-') {
1272 advise (NULL
, "missing argument to %s", argp
[-2]);
1280 if (!(cp
= *argp
++) || *cp
== '-') {
1281 advise (NULL
, "missing argument to %s", argp
[-2]);
1288 if (!(attach
= *argp
++) || *attach
== '-') {
1289 advise (NULL
, "missing argument to %s", argp
[-2]);
1297 case SNDATTACHFORMAT
:
1298 if (! *argp
|| **argp
== '-')
1299 adios (NULL
, "missing argument to %s", argp
[-1]);
1301 attachformat
= atoi (*argp
);
1302 if (attachformat
< 0 ||
1303 attachformat
> ATTACHFORMATS
- 1) {
1304 advise (NULL
, "unsupported attachformat %d",
1313 advise (NULL
, "usage: %s [switches]", sp
);
1317 /* allow Aliasfile: profile entry */
1318 if ((cp
= context_find ("Aliasfile"))) {
1322 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1323 vec
[vecp
++] = "-alias";
1328 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1329 if ((cp
= context_find ("signature")) && *cp
)
1330 m_putenv ("SIGNATURE", cp
);
1332 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1334 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1336 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1337 inplace
= atoi (cp
);
1339 if ((cp
= getenv ("mhdist"))
1342 && (distsw
= atoi (cp
))
1343 #endif /* not lint */
1345 vec
[vecp
++] = "-dist";
1346 distfile
= getcpy (m_mktemp2(altmsg
, invo_name
, NULL
, NULL
));
1348 if (link (altmsg
, distfile
) == NOTOK
)
1349 adios (distfile
, "unable to link %s to", altmsg
);
1354 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1359 if ((pushsw
= pushed
))
1362 vec
[0] = r1bindex (postproc
, '/');
1365 if (sendsbr (vec
, vecp
, file
, &st
, 1, attach
, attachformat
) == OK
)
1374 whomfile (char **arg
, char *file
)
1380 context_save (); /* save the context file */
1383 switch (pid
= vfork()) {
1385 advise ("fork", "unable to");
1390 vec
[vecp
++] = r1bindex (whomproc
, '/');
1394 vec
[vecp
++] = *arg
++;
1397 execvp (whomproc
, vec
);
1398 fprintf (stderr
, "unable to exec ");
1400 _exit (-1); /* NOTREACHED */
1403 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1409 * Remove the draft file
1413 removefile (char *drft
)
1415 if (unlink (drft
) == NOTOK
)
1416 adios (drft
, "unable to unlink");