]> diplodocus.org Git - nmh/blob - uip/whatnowproc.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / whatnowproc.c
1 /* whatnowproc.c -- exec the "whatnowproc"
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include "h/done.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
22 int
23 what_now (char *ed, int nedit, int use, char *file, char *altmsg, int dist,
24 struct msgs *mp, char *text, int inplace, char *cwd, int atfile)
25 {
26 bool found;
27 int k, msgnum, vecp;
28 int len, buflen;
29 char *bp;
30 char buffer[BUFSIZ], *vec[MAXARGS];
31
32 vecp = 0;
33 vec[vecp++] = r1bindex (whatnowproc, '/');
34 vec[vecp] = NULL;
35
36 setenv("mhdraft", file, 1);
37 if (mp)
38 setenv("mhfolder", mp->foldpath, 1);
39 else
40 unsetenv("mhfolder");
41 if (altmsg) {
42 if (mp == NULL || *altmsg == '/' || cwd == NULL)
43 setenv("mhaltmsg", altmsg, 1);
44 else {
45 snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath, altmsg);
46 setenv("mhaltmsg", buffer, 1);
47 }
48 } else {
49 unsetenv("mhaltmsg");
50 }
51 if ((bp = getenv ("mhaltmsg")))/* XXX */
52 setenv("editalt", bp, 1);
53 snprintf (buffer, sizeof(buffer), "%d", dist);
54 setenv("mhdist", buffer, 1);
55 if (nedit) {
56 unsetenv("mheditor");
57 } else {
58 if (!ed)
59 ed = get_default_editor();
60 setenv("mheditor", ed, 1);
61 }
62 snprintf (buffer, sizeof(buffer), "%d", use);
63 setenv("mhuse", buffer, 1);
64 snprintf (buffer, sizeof(buffer), "%d", atfile);
65 setenv("mhatfile", buffer, 1);
66
67 unsetenv("mhmessages");
68 unsetenv("mhannotate");
69 unsetenv("mhinplace");
70
71 if (text && mp && !is_readonly(mp)) {
72 found = false;
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 ? " " : "", m_name (msgnum));
78 len = strlen (bp);
79 bp += len;
80 buflen -= len;
81 for (k = msgnum + 1; k <= mp->hghmsg && is_selected(mp, k); k++)
82 continue;
83 if (--k > msgnum) {
84 snprintf (bp, buflen, "-%s", m_name (k));
85 len = strlen (bp);
86 bp += len;
87 buflen -= len;
88 }
89 msgnum = k + 1;
90 found = true;
91 }
92 }
93 if (found) {
94 setenv("mhmessages", buffer, 1);
95 setenv("mhannotate", text, 1);
96 snprintf (buffer, sizeof(buffer), "%d", inplace);
97 setenv("mhinplace", buffer, 1);
98 }
99 }
100
101 context_save (); /* save the context file */
102 fflush (stdout);
103
104 if (cwd) {
105 if (chdir (cwd) < 0) {
106 advise (cwd, "chdir");
107 }
108 }
109
110 /*
111 * If the "whatnowproc" is the nmh command "whatnow",
112 * we run it internally, rather than exec'ing it.
113 */
114 if (strcmp (vec[0], "whatnow") == 0) {
115 WhatNow (vecp, vec);
116 done (0);
117 }
118
119 execvp (whatnowproc, vec);
120 fprintf (stderr, "unable to exec ");
121 perror (whatnowproc);
122
123 return 0;
124 }