]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/uip/comp.c
Always check that mktemp()/mktemp2() succeeds before trying to
[nmh] / docs / historical / mh-6.8.5 / uip / comp.c
1 /* comp.c - compose a message */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: comp.c,v 1.7 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 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 /* \f */
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 /* \f */
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 invo_name = r1bindex (argv[0], '/');
117 if ((cp = m_find (invo_name)) != NULL) {
118 ap = brkstring (cp = getcpy (cp), " ", "\n");
119 ap = copyip (ap, arguments);
120 }
121 else
122 ap = arguments;
123 (void) copyip (argv + 1, ap);
124 argp = arguments;
125
126 /* \f */
127
128 while (cp = *argp++) {
129 if (*cp == '-')
130 switch (smatch (++cp, switches)) {
131 case AMBIGSW:
132 ambigsw (cp, switches);
133 done (1);
134 case UNKWNSW:
135 adios (NULLCP, "-%s unknown", cp);
136 case HELPSW:
137 (void) sprintf (buf, "%s [+folder] [msg] [switches]",
138 invo_name);
139 help (buf, switches);
140 done (1);
141
142 case EDITRSW:
143 if (!(ed = *argp++) || *ed == '-')
144 adios (NULLCP, "missing argument to %s", argp[-2]);
145 nedit = 0;
146 continue;
147 case NEDITSW:
148 nedit++;
149 continue;
150
151 case WHATSW:
152 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
153 adios (NULLCP, "missing argument to %s", argp[-2]);
154 nwhat = 0;
155 continue;
156 case NWHATSW:
157 nwhat++;
158 continue;
159
160 case FORMSW:
161 if (!(form = *argp++) || *form == '-')
162 adios (NULLCP, "missing argument to %s", argp[-2]);
163 continue;
164
165 case USESW:
166 use++;
167 continue;
168 case NUSESW:
169 use = NOUSE;
170 continue;
171
172 case FILESW: /* compatibility */
173 if (file)
174 adios (NULLCP, "only one file at a time!");
175 if (!(file = *argp++) || *file == '-')
176 adios (NULLCP, "missing argument to %s", argp[-2]);
177 isdf = NOTOK;
178 continue;
179
180 case DFOLDSW:
181 if (dfolder)
182 adios (NULLCP, "only one draft folder at a time!");
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULLCP, "missing argument to %s", argp[-2]);
185 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
186 *cp != '@' ? TFOLDER : TSUBCWF);
187 continue;
188 case DMSGSW:
189 if (file)
190 adios (NULLCP, "only one draft message at a time!");
191 if (!(file = *argp++) || *file == '-')
192 adios (NULLCP, "missing argument to %s", argp[-2]);
193 continue;
194 case NDFLDSW:
195 dfolder = NULL;
196 isdf = NOTOK;
197 continue;
198 }
199 if (*cp == '+' || *cp == '@') {
200 if (folder)
201 adios (NULLCP, "only one folder at a time!");
202 else
203 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
204 }
205 else
206 if (msg)
207 adios (NULLCP, "only one message at a time!");
208 else
209 msg = cp;
210 }
211
212 /* \f */
213
214 cwd = getcpy (pwd ());
215
216 if (!m_find ("path"))
217 free (path ("./", TFOLDER));
218
219 if ((dfolder || m_find ("Draft-Folder")) && !folder && msg && !file)
220 file = msg, msg = NULL;
221 if (form && (folder || msg))
222 adios (NULLCP, "can't mix forms and folders/msgs");
223
224 if (folder || msg) {
225 if (!msg)
226 msg = "cur";
227 if (!folder)
228 folder = m_getfolder ();
229 maildir = m_maildir (folder);
230
231 if (chdir (maildir) == NOTOK)
232 adios (maildir, "unable to change directory to");
233 if (!(mp = m_gmsg (folder)))
234 adios (NULLCP, "unable to read folder %s", folder);
235 if (mp -> hghmsg == 0)
236 adios (NULLCP, "no messages in %s", folder);
237
238 if (!m_convert (mp, msg))
239 done (1);
240 m_setseq (mp);
241
242 if (mp -> numsel > 1)
243 adios (NULLCP, "only one message at a time!");
244
245 if ((in = open (form = getcpy (m_name (mp -> lowsel)), 0)) == NOTOK)
246 adios (form, "unable to open message");
247 }
248 else
249 if (form) {
250 if ((in = open (libpath (form), 0)) == NOTOK)
251 adios (form, "unable to open form file");
252 }
253 else {
254 if ((in = open (libpath (components), 0)) == NOTOK)
255 adios (components, "unable to open default components file");
256 form = components;
257 }
258
259 /* \f */
260
261 try_it_again: ;
262 (void) strcpy (drft, m_draft (dfolder, file, use, &isdf));
263 if ((out = open (drft, 0)) != NOTOK) {
264 i = fdcompare (in, out);
265 (void) close (out);
266 if (use || i)
267 goto edit_it;
268
269 if (stat (drft, &st) == NOTOK)
270 adios (drft, "unable to stat");
271 printf ("Draft \"%s\" exists (%ld bytes).", drft, st.st_size);
272 for (i = LISTDSW; i != YESW;) {
273 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
274 done (1);
275 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
276 case NOSW:
277 done (0);
278 case NEWSW:
279 file = NULL;
280 use = NOUSE;
281 goto try_it_again;
282 case YESW:
283 break;
284 case USELSW:
285 use++;
286 goto edit_it;
287 case LISTDSW:
288 (void) showfile (++argp, drft);
289 break;
290 case REFILSW:
291 if (refile (++argp, drft) == 0)
292 i = YESW;
293 break;
294 default:
295 advise (NULLCP, "say what?");
296 break;
297 }
298 }
299 }
300 else
301 if (use)
302 adios (drft, "unable to open");
303
304 if ((out = creat (drft, m_gmprot ())) == NOTOK)
305 adios (drft, "unable to create");
306 cpydata (in, out, form, drft);
307 (void) close (in);
308 (void) close (out);
309
310 edit_it: ;
311 m_update ();
312
313 if (nwhat)
314 done (0);
315 (void) what_now (ed, nedit, use, drft, NULLCP, 0, NULLMP, NULLCP, 0, cwd);
316 done (1);
317 }