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