]> diplodocus.org Git - nmh/blob - uip/whatnowproc.c
Alter mh-chart(7)'s NAME to be lowercase.
[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
23 int
24 what_now (char *ed, int nedit, int use, char *file, char *altmsg, int dist,
25 struct msgs *mp, char *text, int inplace, char *cwd, int atfile)
26 {
27 int found, k, msgnum, vecp;
28 int len, buflen;
29 char *bp;
30 char buffer[BUFSIZ], *vec[MAXARGS];
31
32 vecp = 0;
33 vec[vecp++] = r1bindex (whatnowproc, '/');
34 vec[vecp] = NULL;
35
36 m_putenv ("mhdraft", file);
37 if (mp)
38 m_putenv ("mhfolder", mp->foldpath);
39 else
40 unputenv ("mhfolder");
41 if (altmsg) {
42 if (mp == NULL || *altmsg == '/' || cwd == NULL)
43 m_putenv ("mhaltmsg", altmsg);
44 else {
45 snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath, altmsg);
46 m_putenv ("mhaltmsg", buffer);
47 }
48 } else {
49 unputenv ("mhaltmsg");
50 }
51 if ((bp = getenv ("mhaltmsg")))/* XXX */
52 m_putenv ("editalt", bp);
53 snprintf (buffer, sizeof(buffer), "%d", dist);
54 m_putenv ("mhdist", buffer);
55 if (nedit) {
56 unputenv ("mheditor");
57 } else {
58 m_putenv ("mheditor", ed ? ed : (ed = get_default_editor()));
59 }
60 snprintf (buffer, sizeof(buffer), "%d", use);
61 m_putenv ("mhuse", buffer);
62 snprintf (buffer, sizeof(buffer), "%d", atfile);
63 m_putenv ("mhatfile", buffer);
64
65 unputenv ("mhmessages");
66 unputenv ("mhannotate");
67 unputenv ("mhinplace");
68
69 if (text && mp && !is_readonly(mp)) {
70 found = 0;
71 bp = buffer;
72 buflen = sizeof(buffer);
73 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
74 if (is_selected(mp, msgnum)) {
75 snprintf (bp, buflen, "%s%s", found ? " " : "", m_name (msgnum));
76 len = strlen (bp);
77 bp += len;
78 buflen -= len;
79 for (k = msgnum + 1; k <= mp->hghmsg && is_selected(mp, k); k++)
80 continue;
81 if (--k > msgnum) {
82 snprintf (bp, buflen, "-%s", m_name (k));
83 len = strlen (bp);
84 bp += len;
85 buflen -= len;
86 }
87 msgnum = k + 1;
88 found++;
89 }
90 }
91 if (found) {
92 m_putenv ("mhmessages", buffer);
93 m_putenv ("mhannotate", text);
94 snprintf (buffer, sizeof(buffer), "%d", inplace);
95 m_putenv ("mhinplace", buffer);
96 }
97 }
98
99 context_save (); /* save the context file */
100 fflush (stdout);
101
102 if (cwd) {
103 if (chdir (cwd) < 0) {
104 advise (cwd, "chdir");
105 }
106 }
107
108 /*
109 * If the "whatnowproc" is the nmh command "whatnow",
110 * we run it internally, rather than exec'ing it.
111 */
112 if (strcmp (vec[0], "whatnow") == 0) {
113 WhatNow (vecp, vec);
114 done (0);
115 }
116
117 execvp (whatnowproc, vec);
118 fprintf (stderr, "unable to exec ");
119 perror (whatnowproc);
120
121 return 0;
122 }