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