]> diplodocus.org Git - nmh/blob - uip/whatnowproc.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / uip / whatnowproc.c
1
2 /*
3 * whatnowproc.c -- exec the "whatnowproc"
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.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 /* from whatnowsbr.c */
25 int WhatNow (int, char **);
26
27
28 int
29 what_now (char *ed, int nedit, int use, char *file, char *altmsg, int dist,
30 struct msgs *mp, char *text, int inplace, char *cwd)
31 {
32 int found, k, msgnum, vecp;
33 int len, buflen;
34 register char *bp;
35 char buffer[BUFSIZ], *vec[MAXARGS];
36
37 vecp = 0;
38 vec[vecp++] = r1bindex (whatnowproc, '/');
39 vec[vecp] = NULL;
40
41 m_putenv ("mhdraft", file);
42 if (mp)
43 m_putenv ("mhfolder", mp->foldpath);
44 else
45 unputenv ("mhfolder");
46 if (altmsg) {
47 if (mp == NULL || *altmsg == '/' || cwd == NULL)
48 m_putenv ("mhaltmsg", altmsg);
49 else {
50 snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath, altmsg);
51 m_putenv ("mhaltmsg", buffer);
52 }
53 } else {
54 unputenv ("mhaltmsg");
55 }
56 if ((bp = getenv ("mhaltmsg")))/* XXX */
57 m_putenv ("editalt", bp);
58 snprintf (buffer, sizeof(buffer), "%d", dist);
59 m_putenv ("mhdist", buffer);
60 if (nedit) {
61 unputenv ("mheditor");
62 } else {
63 m_putenv ("mheditor", ed ? ed : (ed = context_find ("editor"))
64 ? ed : defaulteditor);
65 }
66 snprintf (buffer, sizeof(buffer), "%d", use);
67 m_putenv ("mhuse", buffer);
68
69 unputenv ("mhmessages");
70 unputenv ("mhannotate");
71 unputenv ("mhinplace");
72
73 if (text && mp && !is_readonly(mp)) {
74 found = 0;
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++;
93 }
94 }
95 if (found) {
96 m_putenv ("mhmessages", buffer);
97 m_putenv ("mhannotate", text);
98 snprintf (buffer, sizeof(buffer), "%d", inplace);
99 m_putenv ("mhinplace", buffer);
100 }
101 }
102
103 context_save (); /* save the context file */
104 fflush (stdout);
105
106 if (cwd)
107 chdir (cwd);
108
109 /*
110 * If the "whatnowproc" is the nmh command "whatnow",
111 * we run it internally, rather than exec'ing it.
112 */
113 if (strcmp (vec[0], "whatnow") == 0) {
114 WhatNow (vecp, vec);
115 done (0);
116 }
117
118 execvp (whatnowproc, vec);
119 fprintf (stderr, "unable to exec ");
120 perror (whatnowproc);
121
122 return 0;
123 }