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