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