Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / dist.c
1 /* dist.c - re-distribute a message */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: dist.c,v 1.5 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #ifdef LOCALE
11 #include        <locale.h>
12 #endif
13
14 /* \f */
15
16 static struct swit switches[] = {
17 #define ANNOSW  0
18     "annotate", 0,
19 #define NANNOSW 1
20     "noannotate", 0,
21
22 #define DFOLDSW 2
23     "draftfolder +folder", 0,
24 #define DMSGSW  3
25     "draftmessage msg", 0,
26 #define NDFLDSW 4
27     "nodraftfolder", 0,
28
29 #define EDITRSW 5
30     "editor editor", 0,
31 #define NEDITSW 6
32     "noedit", 0,
33
34 #define FORMSW  7
35     "form formfile", 0,
36
37 #define INPLSW  8
38     "inplace", 0,
39 #define NINPLSW 9
40     "noinplace", 0,
41
42 #define WHATSW  10
43     "whatnowproc program", 0,
44 #define NWHATSW 11
45     "nowhatnowproc", 0,
46
47 #define HELPSW  12
48     "help", 4,
49
50 #define FILESW  13
51     "file file", -4,            /* interface from msh */
52
53     NULL, 0
54 };
55
56 /* \f */
57
58 static struct swit aqrnl[] = {
59 #define NOSW    0
60     "quit", 0,
61 #define YESW    1
62     "replace", 0,
63 #define LISTDSW 2
64     "list", 0,
65 #define REFILSW 3
66     "refile +folder", 0,
67 #define NEWSW   4
68     "new", 0,
69
70     NULL, 0
71 };
72
73
74 static struct swit aqrl[] = {
75     "quit", 0,
76     "replace", 0,
77     "list", 0,
78     "refile +folder", 0,
79
80     NULL, 0
81 };
82
83 /* \f */
84
85 /* ARGSUSED */
86
87 main (argc, argv)
88 int     argc;
89 char   *argv[];
90 {
91     int     anot = 0,
92             inplace = 0,
93             nedit = 0,
94             nwhat = 0,
95             i,
96             in,
97             isdf = 0,
98             out;
99     char   *cp,
100            *cwd,
101            *maildir,
102            *msgnam,
103            *dfolder = NULL,
104            *dmsg = NULL,
105            *ed = NULL,
106            *file = NULL,
107            *folder = NULL,
108            *form = NULL,
109            *msg = NULL,
110             buf[100],
111             drft[BUFSIZ],
112           **ap,
113           **argp,
114            *arguments[MAXARGS];
115     struct msgs *mp = NULL;
116     struct stat st;
117
118 #ifdef LOCALE
119         setlocale(LC_ALL, "");
120 #endif
121     invo_name = r1bindex (argv[0], '/');
122     if ((cp = m_find (invo_name)) != NULL) {
123         ap = brkstring (cp = getcpy (cp), " ", "\n");
124         ap = copyip (ap, arguments);
125     }
126     else
127         ap = arguments;
128     (void) copyip (argv + 1, ap);
129     argp = arguments;
130
131 /* \f */
132
133     while (cp = *argp++) {
134         if (*cp == '-')
135             switch (smatch (++cp, switches)) {
136                 case AMBIGSW: 
137                     ambigsw (cp, switches);
138                     done (1);
139                 case UNKWNSW: 
140                     adios (NULLCP, "-%s unknown", cp);
141                 case HELPSW: 
142                     (void) sprintf (buf, "%s [+folder] [msg] [switches]",
143                             invo_name);
144                     help (buf, switches);
145                     done (1);
146
147                 case ANNOSW: 
148                     anot++;
149                     continue;
150                 case NANNOSW: 
151                     anot = 0;
152                     continue;
153
154                 case EDITRSW: 
155                     if (!(ed = *argp++) || *ed == '-')
156                         adios (NULLCP, "missing argument to %s", argp[-2]);
157                     nedit = 0;
158                     continue;
159                 case NEDITSW:
160                     nedit++;
161                     continue;
162                     
163                 case WHATSW: 
164                     if (!(whatnowproc = *argp++) || *whatnowproc == '-')
165                         adios (NULLCP, "missing argument to %s", argp[-2]);
166                     nwhat = 0;
167                     continue;
168                 case NWHATSW: 
169                     nwhat++;
170                     continue;
171
172                 case FILESW: 
173                     if (file)
174                         adios (NULLCP, "only one file at a time!");
175                     if (!(cp = *argp++) || *cp == '-')
176                         adios (NULLCP, "missing argument to %s", argp[-2]);
177                     file = path (cp, TFILE);
178                     continue;
179                 case FORMSW: 
180                     if (!(form = *argp++) || *form == '-')
181                         adios (NULLCP, "missing argument to %s", argp[-2]);
182                     continue;
183
184                 case INPLSW: 
185                     inplace++;
186                     continue;
187                 case NINPLSW: 
188                     inplace = 0;
189                     continue;
190
191                 case DFOLDSW: 
192                     if (dfolder)
193                         adios (NULLCP, "only one draft folder at a time!");
194                     if (!(cp = *argp++) || *cp == '-')
195                         adios (NULLCP, "missing argument to %s", argp[-2]);
196                     dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
197                             *cp != '@' ? TFOLDER : TSUBCWF);
198                     continue;
199                 case DMSGSW: 
200                     if (dmsg)
201                         adios (NULLCP, "only one draft message at a time!");
202                     if (!(dmsg = *argp++) || *dmsg == '-')
203                         adios (NULLCP, "missing argument to %s", argp[-2]);
204                     continue;
205                 case NDFLDSW: 
206                     dfolder = NULL;
207                     isdf = NOTOK;
208                     continue;
209             }
210         if (*cp == '+' || *cp == '@') {
211             if (folder)
212                 adios (NULLCP, "only one folder at a time!");
213             else
214                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
215         }
216         else
217             if (msg)
218                 adios (NULLCP, "only one message at a time!");
219             else
220                 msg = cp;
221     }
222
223 /* \f */
224
225     cwd = getcpy (pwd ());
226
227     if (!m_find ("path"))
228         free (path ("./", TFOLDER));
229     if (file && (msg || folder))
230         adios (NULLCP, "can't mix files and folders/msgs");
231
232     if (form) {
233         if ((in = open (libpath (form), 0)) == NOTOK)
234             adios (form, "unable to open form file");
235     }
236     else {
237         if ((in = open (libpath (distcomps), 0)) == NOTOK)
238             adios (distcomps, "unable to open default components file");
239         form = distcomps;
240     }
241
242 try_it_again: ;
243     (void) strcpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf));
244     if (stat (drft, &st) != NOTOK) {
245         printf ("Draft \"%s\" exists (%ld bytes).", drft, st.st_size);
246         for (i = LISTDSW; i != YESW;) {
247             if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
248                 done (1);
249             switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
250                 case NOSW: 
251                     done (0);
252                 case NEWSW: 
253                     dmsg = NULL;
254                     goto try_it_again;
255                 case YESW: 
256                     break;
257                 case LISTDSW: 
258                     (void) showfile (++argp, drft);
259                     break;
260                 case REFILSW: 
261                     if (refile (++argp, drft) == 0)
262                         i = YESW;
263                     break;
264                 default: 
265                     advise (NULLCP, "say what?");
266                     break;
267             }
268         }
269     }
270     if ((out = creat (drft, m_gmprot ())) == NOTOK)
271         adios (drft, "unable to create");
272
273     cpydata (in, out, form, drft);
274     (void) close (in);
275     (void) close (out);
276
277 /* \f */
278
279     if (file) {
280         anot = 0;
281         goto go_to_it;
282     }
283
284     if (!msg)
285         msg = "cur";
286     if (!folder)
287         folder = m_getfolder ();
288     maildir = m_maildir (folder);
289
290     if (chdir (maildir) == NOTOK)
291         adios (maildir, "unable to change directory to");
292     if (!(mp = m_gmsg (folder)))
293         adios (NULLCP, "unable to read folder %s", folder);
294     if (mp -> hghmsg == 0)
295         adios (NULLCP, "no messages in %s", folder);
296
297     if (!m_convert (mp, msg))
298         done (1);
299     m_setseq (mp);
300
301     if (mp -> numsel > 1)
302         adios (NULLCP, "only one message at a time!");
303
304 go_to_it: ;
305     if ((in = open (msgnam = file ? file : getcpy (m_name (mp -> lowsel)), 0))
306             == NOTOK)
307         adios (msgnam, "unable to open message");
308
309     if (!file) {
310         m_replace (pfolder, folder);
311         if (mp -> lowsel != mp -> curmsg)
312             m_setcur (mp, mp -> lowsel);
313         m_sync (mp);
314         m_update ();
315     }
316
317     if (nwhat)
318         done (0);
319     (void) what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
320         anot ? "Resent" : NULLCP, inplace, cwd);
321     done (1);
322 }