583f133c87725874593f5b931ec43e0a8d4dca87
[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.  As an optimization, if the
17 ** "whatnowproc" is the nmh command "whatnow" (typical case),
18 ** it will call this routine directly without exec'ing it.
19 */
20
21 /* from whatnowsbr.c */
22 int WhatNow(int, char **);
23
24
25 int
26 what_now(char *ed, int nedit, int use, char *file, char *altmsg, int dist,
27         struct msgs *mp, char *text, int inplace, char *cwd)
28 {
29         int found, k, msgnum, vecp;
30         int len, buflen;
31         register char *bp;
32         char buffer[BUFSIZ], *vec[MAXARGS];
33
34         vecp = 0;
35         vec[vecp++] = r1bindex(whatnowproc, '/');
36         vec[vecp] = NULL;
37
38         m_putenv("mhdraft", file);
39         if (mp)
40                 m_putenv("mhfolder", mp->foldpath);
41         else
42                 unputenv("mhfolder");
43         if (altmsg) {
44                 if (mp == NULL || *altmsg == '/' || cwd == NULL)
45                         m_putenv("mhaltmsg", altmsg);
46                 else {
47                         snprintf(buffer, sizeof(buffer), "%s/%s",
48                                         mp->foldpath, altmsg);
49                         m_putenv("mhaltmsg", buffer);
50                 }
51         } else {
52                 unputenv("mhaltmsg");
53         }
54         if ((bp = getenv("mhaltmsg")))/* XXX */
55                 m_putenv("editalt", bp);
56         snprintf(buffer, sizeof(buffer), "%d", dist);
57         m_putenv("mhdist", buffer);
58         if (nedit) {
59                 unputenv("mheditor");
60         } else {
61                 m_putenv("mheditor", ed ? ed : (ed = context_find("editor")) ?
62                                 ed : defaulteditor);
63         }
64         snprintf(buffer, sizeof(buffer), "%d", use);
65         m_putenv("mhuse", buffer);
66
67         unputenv("mhmessages");
68         unputenv("mhannotate");
69         unputenv("mhinplace");
70
71         if (text && mp && !is_readonly(mp)) {
72                 found = 0;
73                 bp = buffer;
74                 buflen = sizeof(buffer);
75                 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
76                         if (is_selected(mp, msgnum)) {
77                                 snprintf(bp, buflen, "%s%s", found ? " " : "",
78                                                 m_name(msgnum));
79                                 len = strlen(bp);
80                                 bp += len;
81                                 buflen -= len;
82                                 for (k = msgnum + 1; k <= mp->hghmsg && is_selected(mp, k); k++)
83                                         continue;
84                                 if (--k > msgnum) {
85                                         snprintf(bp, buflen, "-%s", m_name(k));
86                                         len = strlen(bp);
87                                         bp += len;
88                                         buflen -= len;
89                                 }
90                                 msgnum = k + 1;
91                                 found++;
92                         }
93                 }
94                 if (found) {
95                         m_putenv("mhmessages", buffer);
96                         m_putenv("mhannotate", text);
97                         snprintf(buffer, sizeof(buffer), "%d", inplace);
98                         m_putenv("mhinplace", buffer);
99                 }
100         }
101
102         context_save();  /* save the context file */
103         fflush(stdout);
104
105         if (cwd)
106                 chdir(cwd);
107
108         /*
109         ** If the "whatnowproc" is the nmh command "whatnow",
110         ** we run it internally, rather than exec'ing it.
111         */
112         if (strcmp(vec[0], "whatnow") == 0) {
113                 WhatNow(vecp, vec);
114                 done(0);
115         }
116
117         execvp(whatnowproc, vec);
118         fprintf(stderr, "unable to exec ");
119         perror(whatnowproc);
120
121         return 0;
122 }