Rework of Editor/defaulteditor, similar to Pager/defaultpager; new env vars.
[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 nedit, 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 (nedit) {
52                 unputenv("mheditor");
53         } else {
54                 m_putenv("mheditor", ed ? ed : defaulteditor);
55         }
56         snprintf(buffer, sizeof(buffer), "%d", use);
57         m_putenv("mhuse", buffer);
58
59         unputenv("mhmessages");
60         unputenv("mhannotate");
61
62         if (text && mp && !is_readonly(mp)) {
63                 found = 0;
64                 bp = buffer;
65                 buflen = sizeof(buffer);
66                 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
67                         if (is_selected(mp, msgnum)) {
68                                 snprintf(bp, buflen, "%s%s", found ? " " : "",
69                                                 m_name(msgnum));
70                                 len = strlen(bp);
71                                 bp += len;
72                                 buflen -= len;
73                                 for (k = msgnum + 1; k <= mp->hghmsg && is_selected(mp, k); k++)
74                                         continue;
75                                 if (--k > msgnum) {
76                                         snprintf(bp, buflen, "-%s", m_name(k));
77                                         len = strlen(bp);
78                                         bp += len;
79                                         buflen -= len;
80                                 }
81                                 msgnum = k + 1;
82                                 found++;
83                         }
84                 }
85                 if (found) {
86                         m_putenv("mhmessages", buffer);
87                         m_putenv("mhannotate", text);
88                 }
89         }
90
91         context_save();  /* save the context file */
92         fflush(stdout);
93
94         if (cwd)
95                 chdir(cwd);
96
97         execvp(whatnowproc, vec);
98         fprintf(stderr, "unable to exec ");
99         perror(whatnowproc);
100
101         return 0;
102 }