caeeec2c79017cdc54254967c23f461c246299c1
[mmh] / uip / whatnowproc.c
1
2 /*
3  * whatnowproc.c -- exec the "whatnowproc"
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10
11 /*
12  * This routine is used by comp, repl, forw, and dist to exec
13  * the "whatnowproc".  It first sets up all the environment
14  * variables that the "whatnowproc" will need to check, and
15  * then execs the command.  As an optimization, if the
16  * "whatnowproc" is the nmh command "whatnow" (typical case),
17  * it will call this routine directly without exec'ing it.
18  */
19
20 int
21 what_now (char *ed, int nedit, int use, char *file, char *altmsg, int dist,
22           struct msgs *mp, char *text, int inplace, char *cwd)
23 {
24     int found, k, msgnum, vecp;
25     int len, buflen;
26     register char *bp;
27     char buffer[BUFSIZ], *vec[MAXARGS];
28
29     vecp = 0;
30     vec[vecp++] = r1bindex (whatnowproc, '/');
31     vec[vecp] = NULL;
32
33     m_putenv ("mhdraft", file);
34     if (mp)
35         m_putenv ("mhfolder", mp->foldpath);
36     else
37         unputenv ("mhfolder");
38     if (altmsg) {
39         if (mp == NULL || *altmsg == '/' || cwd == NULL)
40             m_putenv ("mhaltmsg", altmsg);
41         else {
42             snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath, altmsg);
43             m_putenv ("mhaltmsg", buffer);
44         }
45     } else {
46         unputenv ("mhaltmsg");
47     }
48     if ((bp = getenv ("mhaltmsg")))/* XXX */
49         m_putenv ("editalt", bp);
50     snprintf (buffer, sizeof(buffer), "%d", dist);
51     m_putenv ("mhdist", buffer);
52     if (nedit) {
53         unputenv ("mheditor");
54     } else {
55         m_putenv ("mheditor", ed ? ed : (ed = context_find ("editor"))
56             ? ed : defaulteditor);
57     }
58     snprintf (buffer, sizeof(buffer), "%d", use);
59     m_putenv ("mhuse", buffer);
60
61     unputenv ("mhmessages");
62     unputenv ("mhannotate");
63     unputenv ("mhinplace");
64
65     if (text && mp && !is_readonly(mp)) {
66         found = 0;
67         bp = buffer;
68         buflen = sizeof(buffer);
69         for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
70             if (is_selected(mp, msgnum)) {
71                 snprintf (bp, buflen, "%s%s", found ? " " : "", 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             snprintf (buffer, sizeof(buffer), "%d", inplace);
91             m_putenv ("mhinplace", buffer);
92         }
93     }
94
95     context_save ();    /* save the context file */
96     fflush (stdout);
97
98     if (cwd)
99         chdir (cwd);
100
101     /*
102      * If the "whatnowproc" is the nmh command "whatnow",
103      * we run it internally, rather than exec'ing it.
104      */
105     if (strcmp (vec[0], "whatnow") == 0) {
106         WhatNow (vecp, vec);
107         done (0);
108     }
109
110     execvp (whatnowproc, vec);
111     fprintf (stderr, "unable to exec ");
112     perror (whatnowproc);
113
114     return 0;
115 }