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