Mercurial > hg > Applications > mh
comparison uip/dist.c @ 0:bce86c4163a3
Initial revision
author | kono |
---|---|
date | Mon, 18 Apr 2005 23:46:02 +0900 |
parents | |
children | 441a2190cfae |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bce86c4163a3 |
---|---|
1 /* dist.c - re-distribute a message */ | |
2 #ifndef lint | |
3 static char ident[] = "@(#)$Id$"; | |
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 /* */ | |
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 /* */ | |
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 /* */ | |
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 #ifdef JAPAN | |
122 ml_init(); | |
123 #endif /* JAPAN */ | |
124 invo_name = r1bindex (argv[0], '/'); | |
125 if ((cp = m_find (invo_name)) != NULL) { | |
126 ap = brkstring (cp = getcpy (cp), " ", "\n"); | |
127 ap = copyip (ap, arguments); | |
128 } | |
129 else | |
130 ap = arguments; | |
131 (void) copyip (argv + 1, ap); | |
132 argp = arguments; | |
133 | |
134 /* */ | |
135 | |
136 while (cp = *argp++) { | |
137 if (*cp == '-') | |
138 switch (smatch (++cp, switches)) { | |
139 case AMBIGSW: | |
140 ambigsw (cp, switches); | |
141 done (1); | |
142 case UNKWNSW: | |
143 adios (NULLCP, "-%s unknown", cp); | |
144 case HELPSW: | |
145 (void) sprintf (buf, "%s [+folder] [msg] [switches]", | |
146 invo_name); | |
147 help (buf, switches); | |
148 done (1); | |
149 | |
150 case ANNOSW: | |
151 anot++; | |
152 continue; | |
153 case NANNOSW: | |
154 anot = 0; | |
155 continue; | |
156 | |
157 case EDITRSW: | |
158 if (!(ed = *argp++) || *ed == '-') | |
159 adios (NULLCP, "missing argument to %s", argp[-2]); | |
160 nedit = 0; | |
161 continue; | |
162 case NEDITSW: | |
163 nedit++; | |
164 continue; | |
165 | |
166 case WHATSW: | |
167 if (!(whatnowproc = *argp++) || *whatnowproc == '-') | |
168 adios (NULLCP, "missing argument to %s", argp[-2]); | |
169 nwhat = 0; | |
170 continue; | |
171 case NWHATSW: | |
172 nwhat++; | |
173 continue; | |
174 | |
175 case FILESW: | |
176 if (file) | |
177 adios (NULLCP, "only one file at a time!"); | |
178 if (!(cp = *argp++) || *cp == '-') | |
179 adios (NULLCP, "missing argument to %s", argp[-2]); | |
180 file = path (cp, TFILE); | |
181 continue; | |
182 case FORMSW: | |
183 if (!(form = *argp++) || *form == '-') | |
184 adios (NULLCP, "missing argument to %s", argp[-2]); | |
185 continue; | |
186 | |
187 case INPLSW: | |
188 inplace++; | |
189 continue; | |
190 case NINPLSW: | |
191 inplace = 0; | |
192 continue; | |
193 | |
194 case DFOLDSW: | |
195 if (dfolder) | |
196 adios (NULLCP, "only one draft folder at a time!"); | |
197 if (!(cp = *argp++) || *cp == '-') | |
198 adios (NULLCP, "missing argument to %s", argp[-2]); | |
199 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp, | |
200 *cp != '@' ? TFOLDER : TSUBCWF); | |
201 continue; | |
202 case DMSGSW: | |
203 if (dmsg) | |
204 adios (NULLCP, "only one draft message at a time!"); | |
205 if (!(dmsg = *argp++) || *dmsg == '-') | |
206 adios (NULLCP, "missing argument to %s", argp[-2]); | |
207 continue; | |
208 case NDFLDSW: | |
209 dfolder = NULL; | |
210 isdf = NOTOK; | |
211 continue; | |
212 } | |
213 if (*cp == '+' || *cp == '@') { | |
214 if (folder) | |
215 adios (NULLCP, "only one folder at a time!"); | |
216 else | |
217 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); | |
218 } | |
219 else | |
220 if (msg) | |
221 adios (NULLCP, "only one message at a time!"); | |
222 else | |
223 msg = cp; | |
224 } | |
225 | |
226 /* */ | |
227 | |
228 cwd = getcpy (pwd ()); | |
229 | |
230 if (!m_find ("path")) | |
231 free (path ("./", TFOLDER)); | |
232 if (file && (msg || folder)) | |
233 adios (NULLCP, "can't mix files and folders/msgs"); | |
234 | |
235 if (form) { | |
236 if ((in = open (libpath (form), 0)) == NOTOK) | |
237 adios (form, "unable to open form file"); | |
238 } | |
239 else { | |
240 if ((in = open (libpath (distcomps), 0)) == NOTOK) | |
241 adios (distcomps, "unable to open default components file"); | |
242 form = distcomps; | |
243 } | |
244 | |
245 try_it_again: ; | |
246 (void) strcpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf)); | |
247 if (stat (drft, &st) != NOTOK) { | |
248 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); | |
249 for (i = LISTDSW; i != YESW;) { | |
250 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl))) | |
251 done (1); | |
252 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) { | |
253 case NOSW: | |
254 done (0); | |
255 case NEWSW: | |
256 dmsg = NULL; | |
257 goto try_it_again; | |
258 case YESW: | |
259 break; | |
260 case LISTDSW: | |
261 (void) showfile (++argp, drft); | |
262 break; | |
263 case REFILSW: | |
264 if (refile (++argp, drft) == 0) | |
265 i = YESW; | |
266 break; | |
267 default: | |
268 advise (NULLCP, "say what?"); | |
269 break; | |
270 } | |
271 } | |
272 } | |
273 if ((out = creat (drft, m_gmprot ())) == NOTOK) | |
274 adios (drft, "unable to create"); | |
275 | |
276 cpydata (in, out, form, drft); | |
277 (void) close (in); | |
278 (void) close (out); | |
279 | |
280 /* */ | |
281 | |
282 if (file) { | |
283 anot = 0; | |
284 goto go_to_it; | |
285 } | |
286 | |
287 if (!msg) | |
288 msg = "cur"; | |
289 if (!folder) | |
290 folder = m_getfolder (); | |
291 maildir = m_maildir (folder); | |
292 | |
293 if (chdir (maildir) == NOTOK) | |
294 adios (maildir, "unable to change directory to"); | |
295 if (!(mp = m_gmsg (folder))) | |
296 adios (NULLCP, "unable to read folder %s", folder); | |
297 if (mp -> hghmsg == 0) | |
298 adios (NULLCP, "no messages in %s", folder); | |
299 | |
300 if (!m_convert (mp, msg)) | |
301 done (1); | |
302 m_setseq (mp); | |
303 | |
304 if (mp -> numsel > 1) | |
305 adios (NULLCP, "only one message at a time!"); | |
306 | |
307 go_to_it: ; | |
308 if ((in = open (msgnam = file ? file : getcpy (m_name (mp -> lowsel)), 0)) | |
309 == NOTOK) | |
310 adios (msgnam, "unable to open message"); | |
311 | |
312 if (!file) { | |
313 m_replace (pfolder, folder); | |
314 if (mp -> lowsel != mp -> curmsg) | |
315 m_setcur (mp, mp -> lowsel); | |
316 m_sync (mp); | |
317 m_update (); | |
318 } | |
319 | |
320 if (nwhat) | |
321 done (0); | |
322 (void) what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, | |
323 anot ? "Resent" : NULLCP, inplace, cwd); | |
324 done (1); | |
325 } |