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