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