Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / whatnowproc.c
1 /* whatnowproc.c - exec whatnowproc */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: whatnowproc.c,v 1.4 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8
9 what_now (ed, nedit, use, file, altmsg, dist, mp, text, inplace, cwd)
10 struct msgs *mp;
11 char  *ed,
12        *file,
13        *altmsg,
14        *text,
15        *cwd;
16 int     nedit,
17         use,
18         dist,
19         inplace;
20 {
21     int     found,
22             k,
23             msgnum,
24             vecp;
25     register char  *bp;
26     char    buffer[BUFSIZ],
27            *vec[MAXARGS];
28
29     vecp = 0;
30     vec[vecp++] = r1bindex (whatnowproc, '/');
31     vec[vecp] = NULL;
32
33     (void) m_putenv ("mhdraft", file);
34     if (mp)
35         (void) m_putenv ("mhfolder", mp -> foldpath);
36     else
37         (void) unputenv ("mhfolder");
38     if (altmsg)
39         if (mp == NULL || *altmsg == '/' || cwd == NULL)
40             (void) m_putenv ("mhaltmsg", altmsg);
41         else {
42             (void) sprintf (buffer, "%s/%s", mp -> foldpath, altmsg);
43             (void) m_putenv ("mhaltmsg", buffer);
44         }
45     else
46         (void) unputenv ("mhaltmsg");
47     if (bp = getenv ("mhaltmsg"))/* XXX */
48         (void) m_putenv ("editalt", bp);
49     (void) sprintf (buffer, "%d", dist);
50     (void) m_putenv ("mhdist", buffer);
51     if (nedit)
52         (void) unputenv ("mheditor");
53     else
54         (void) m_putenv ("mheditor", ed ? ed : (ed = m_find ("editor")) ? ed : sysed);
55     (void) sprintf (buffer, "%d", use);
56     (void) m_putenv ("mhuse", buffer);
57
58     (void) unputenv ("mhmessages");
59     (void) unputenv ("mhannotate");
60     (void) unputenv ("mhinplace");
61     if (text && mp && !(mp -> msgflags & READONLY)) {
62         found = 0;
63         bp = buffer;
64         for (msgnum = mp -> lowmsg; msgnum <= mp -> hghmsg; msgnum++)
65             if (mp -> msgstats[msgnum] & SELECTED) {
66                 (void) sprintf (bp, "%s%s", found ? " " : "", m_name (msgnum));
67                 bp += strlen (bp);
68                 for (k = msgnum + 1;
69                         k <= mp -> hghmsg && mp -> msgstats[k] & SELECTED;
70                         k++)
71                     continue;
72                 if (--k > msgnum) {
73                     (void) sprintf (bp, "-%s", m_name (k));
74                     bp += strlen (bp);
75                 }
76                 msgnum = k + 1;
77                 found++;
78             }
79         if (found) {
80             (void) m_putenv ("mhmessages", buffer);
81             (void) m_putenv ("mhannotate", text);
82             (void) sprintf (buffer, "%d", inplace);
83             (void) m_putenv ("mhinplace", buffer);
84         }
85     }
86
87     m_update ();
88     (void) fflush (stdout);
89
90     if (cwd)
91         (void) chdir (cwd);
92
93     if (strcmp (vec[0], "whatnow") == 0) {
94         WhatNow (vecp, vec);
95         done (0);
96     }
97
98     execvp (whatnowproc, vec);
99     fprintf (stderr, "unable to exec ");
100     perror (whatnowproc);
101
102     return 0;
103 }