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