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