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