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