Renamed -version switch to -Version to remove the conflict with -verbose.
[mmh] / uip / whatnowproc.c
1 /*
2 ** whatnowproc.c -- exec the "whatnowproc"
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11
12 /*
13 ** This routine is used by comp, repl, forw, and dist to exec
14 ** the "whatnowproc".  It first sets up all the environment
15 ** variables that the "whatnowproc" will need to check, and
16 ** then execs the command.
17 */
18 int
19 what_now(char *ed, int use, char *file, char *altmsg, int dist,
20         struct msgs *mp, char *text, char *cwd)
21 {
22         int found, k, msgnum, vecp;
23         int len, buflen;
24         register char *bp;
25         char buffer[BUFSIZ], *vec[MAXARGS];
26
27         vecp = 0;
28         vec[vecp++] = mhbasename(whatnowproc);
29         vec[vecp] = NULL;
30
31         m_putenv("mhdraft", file);
32         if (mp)
33                 m_putenv("mhfolder", mp->foldpath);
34         else
35                 unputenv("mhfolder");
36         if (altmsg) {
37                 if (mp == NULL || *altmsg == '/' || cwd == NULL)
38                         m_putenv("mhaltmsg", altmsg);
39                 else {
40                         snprintf(buffer, sizeof(buffer), "%s/%s",
41                                         mp->foldpath, altmsg);
42                         m_putenv("mhaltmsg", buffer);
43                 }
44         } else {
45                 unputenv("mhaltmsg");
46         }
47         if ((bp = getenv("mhaltmsg")))/* XXX */
48                 m_putenv("editalt", bp);
49         snprintf(buffer, sizeof(buffer), "%d", dist);
50         m_putenv("mhdist", buffer);
51         if (!ed) {
52                 m_putenv("mheditor", defaulteditor);
53         } else if (*ed) {
54                 m_putenv("mheditor", ed);
55         } else {
56                 unputenv("mheditor");
57         }
58         snprintf(buffer, sizeof(buffer), "%d", use);
59         m_putenv("mhuse", buffer);
60
61         unputenv("mhmessages");
62         unputenv("mhannotate");
63
64         if (text && mp && !is_readonly(mp)) {
65                 found = 0;
66                 bp = buffer;
67                 buflen = sizeof(buffer);
68                 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
69                         if (is_selected(mp, msgnum)) {
70                                 snprintf(bp, buflen, "%s%s", found ? " " : "",
71                                                 m_name(msgnum));
72                                 len = strlen(bp);
73                                 bp += len;
74                                 buflen -= len;
75                                 for (k = msgnum + 1; k <= mp->hghmsg && is_selected(mp, k); k++)
76                                         continue;
77                                 if (--k > msgnum) {
78                                         snprintf(bp, buflen, "-%s", m_name(k));
79                                         len = strlen(bp);
80                                         bp += len;
81                                         buflen -= len;
82                                 }
83                                 msgnum = k + 1;
84                                 found++;
85                         }
86                 }
87                 if (found) {
88                         m_putenv("mhmessages", buffer);
89                         m_putenv("mhannotate", text);
90                 }
91         }
92
93         context_save();  /* save the context file */
94         fflush(stdout);
95
96         if (cwd)
97                 chdir(cwd);
98
99         execvp(whatnowproc, vec);
100         fprintf(stderr, "unable to exec ");
101         perror(whatnowproc);
102
103         return 0;
104 }