]>
diplodocus.org Git - nmh/blob - uip/comp.c
3 * comp.c -- compose a message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/fmt_scan.h>
15 #define COMP_SWITCHES \
16 X("draftfolder +folder", 0, DFOLDSW) \
17 X("draftmessage msg", 0, DMSGSW) \
18 X("nodraftfolder", 0, NDFLDSW) \
19 X("editor editor", 0, EDITRSW) \
20 X("noedit", 0, NEDITSW) \
21 X("file file", 0, FILESW) \
22 X("form formfile", 0, FORMSW) \
24 X("nouse", 0, NUSESW) \
25 X("whatnowproc program", 0, WHATSW) \
26 X("nowhatnowproc", 0, NWHATSW) \
27 X("build", 5, BILDSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30 X("to address", 0, TOSW) \
31 X("cc address", 0, CCSW) \
32 X("from address", 0, FROMSW) \
33 X("fcc mailbox", 0, FCCSW) \
34 X("width columns", 0, WIDTHSW) \
35 X("subject text", 0, SUBJECTSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(COMP
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(COMP
, switches
);
45 #define DISPO_SWITCHES \
47 X("replace", 0, YESW) \
49 X("list", 0, LISTDSW) \
50 X("refile +folder", 0, REFILSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(DISPO
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
61 static struct swit aqrul
[] = {
63 { "replace", 0, YESW
},
65 { "list", 0, LISTDSW
},
66 { "refile", 0, REFILSW
},
71 main (int argc
, char **argv
)
73 int use
= NOUSE
, nedit
= 0, nwhat
= 0, build
= 0;
74 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
75 int outputlinelen
= OUTPUTLINELEN
;
76 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
77 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
78 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
79 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
81 char drft
[BUFSIZ
], **argp
, **arguments
;
82 struct msgs
*mp
= NULL
;
86 if (nmh_init(argv
[0], 1)) { return 1; }
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
98 adios (NULL
, "-%s unknown", cp
);
101 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
110 if (!(ed
= *argp
++) || *ed
== '-')
111 adios (NULL
, "missing argument to %s", argp
[-2]);
119 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
120 adios (NULL
, "missing argument to %s", argp
[-2]);
132 if (!(form
= *argp
++) || *form
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
143 case FILESW
: /* compatibility */
145 adios (NULL
, "only one file at a time!");
146 if (!(file
= *argp
++) || *file
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
153 adios (NULL
, "only one draft folder at a time!");
154 if (!(cp
= *argp
++) || *cp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
156 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
157 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
161 adios (NULL
, "only one draft message at a time!");
162 if (!(file
= *argp
++) || *file
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
171 if (!(cp
= *argp
++) || *cp
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
173 to
= addlist(to
, cp
);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
179 cc
= addlist(cc
, cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
185 from
= addlist(from
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios (NULL
, "missing argument to %s", argp
[-2]);
193 cp
= dp
= path(cp
+ 1, TSUBCWF
);
194 fcc
= addlist(fcc
, cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 adios (NULL
, "missing argument to %s", argp
[-2]);
201 if ((outputlinelen
= atoi(cp
)) < 10)
202 adios (NULL
, "impossible width %d", outputlinelen
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
212 if (*cp
== '+' || *cp
== '@') {
214 adios (NULL
, "only one folder at a time!");
216 folder
= pluspath (cp
);
219 adios (NULL
, "only one message at a time!");
225 cwd
= mh_xstrdup(pwd ());
227 if (!context_find ("path"))
228 free (path ("./", TFOLDER
));
231 * Check if we are using a draft folder
232 * and have specified a message in it.
234 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
238 if (form
&& (folder
|| msg
))
239 adios (NULL
, "can't mix forms and folders/msgs");
245 * Use a message as the "form" for the new message.
250 folder
= getfolder (1);
251 maildir
= m_maildir (folder
);
253 if (chdir (maildir
) == NOTOK
)
254 adios (maildir
, "unable to change directory to");
256 /* read folder and create message structure */
257 if (!(mp
= folder_read (folder
, 1)))
258 adios (NULL
, "unable to read folder %s", folder
);
260 /* check for empty folder */
262 adios (NULL
, "no messages in %s", folder
);
264 /* parse the message range/sequence/name and set SELECTED */
265 if (!m_convert (mp
, msg
))
267 seq_setprev (mp
); /* set the previous-sequence */
271 adios (NULL
, "only one message at a time!");
273 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
274 adios (form
, "unable to open message");
281 cp
= new_fs(form
, NULL
, NULL
);
282 format_len
= strlen(cp
);
283 fmt_compile(cp
, &fmt
, 1);
286 * Set up any components that were fed to us on the command line
290 cptr
= fmt_findcomp("from");
295 cptr
= fmt_findcomp("to");
300 cptr
= fmt_findcomp("cc");
305 cptr
= fmt_findcomp("fcc");
310 cptr
= fmt_findcomp("subject");
312 cptr
->c_text
= subject
;
317 strncpy (drft
, build
? m_maildir ("draft")
318 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
321 * Check if we have an existing draft
323 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
324 i
= fdcompare (in
, out
);
328 * If we have given -use flag, or if the
329 * draft is just the same as the components
330 * file, then no need to ask any questions.
335 if (stat (drft
, &st
) == NOTOK
)
336 adios (drft
, "unable to stat");
337 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
338 for (i
= LISTDSW
; i
!= YESW
;) {
339 if (!(argp
= read_switch_multiword ("\nDisposition? ",
340 isdf
? aqrunl
: aqrul
)))
342 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
355 showfile (++argp
, drft
);
358 if (refile (++argp
, drft
) == 0)
362 advise (NULL
, "say what?");
368 adios (drft
, "unable to open");
371 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
372 adios (drft
, "unable to create");
376 i
= format_len
+ 1024;
377 scanl
= charstring_create (i
+ 2);
381 dat
[3] = outputlinelen
;
383 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
384 if (write(out
, charstring_buffer (scanl
),
385 charstring_bytes (scanl
)) < 0) {
386 advise (drft
, "write");
388 charstring_free(scanl
);
390 cpydata (in
, out
, form
, drft
);
396 context_save (); /* save the context file */
400 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULLMP
, NULL
, 0, cwd
, 0);