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