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