]>
diplodocus.org Git - nmh/blob - uip/comp.c
c4f31b75611db2e389a77d72476e7883cb5ffec5
1 /* comp.c -- compose a message
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
15 #include "h/fmt_scan.h"
17 #include "sbr/m_maildir.h"
20 #define COMP_SWITCHES \
21 X("draftfolder +folder", 0, DFOLDSW) \
22 X("draftmessage msg", 0, DMSGSW) \
23 X("nodraftfolder", 0, NDFLDSW) \
24 X("editor editor", 0, EDITRSW) \
25 X("noedit", 0, NEDITSW) \
26 X("file file", 0, FILESW) \
27 X("form formfile", 0, FORMSW) \
29 X("nouse", 0, NUSESW) \
30 X("whatnowproc program", 0, WHATSW) \
31 X("nowhatnowproc", 0, NWHATSW) \
32 X("build", 5, BILDSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
35 X("to address", 0, TOSW) \
36 X("cc address", 0, CCSW) \
37 X("from address", 0, FROMSW) \
38 X("fcc mailbox", 0, FCCSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("subject text", 0, SUBJECTSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(COMP
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(COMP
, switches
);
50 #define DISPO_SWITCHES \
52 X("replace", 0, YESW) \
54 X("list", 0, LISTDSW) \
55 X("refile +folder", 0, REFILSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(DISPO
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
66 static struct swit aqrul
[] = {
68 { "replace", 0, YESW
},
70 { "list", 0, LISTDSW
},
71 { "refile", 0, REFILSW
},
76 main (int argc
, char **argv
)
82 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
83 int outputlinelen
= OUTPUTLINELEN
;
84 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
85 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
86 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
87 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
89 char drft
[BUFSIZ
], **argp
, **arguments
;
90 struct msgs
*mp
= NULL
;
94 if (nmh_init(argv
[0], true, true)) { return 1; }
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 die("-%s unknown", cp
);
109 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
118 if (!(ed
= *argp
++) || *ed
== '-')
119 die("missing argument to %s", argp
[-2]);
127 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
128 die("missing argument to %s", argp
[-2]);
140 if (!(form
= *argp
++) || *form
== '-')
141 die("missing argument to %s", argp
[-2]);
151 case FILESW
: /* compatibility */
153 die("only one file at a time!");
154 if (!(file
= *argp
++) || *file
== '-')
155 die("missing argument to %s", argp
[-2]);
161 die("only one draft folder at a time!");
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
165 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
169 die("only one draft message at a time!");
170 if (!(file
= *argp
++) || *file
== '-')
171 die("missing argument to %s", argp
[-2]);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 to
= addlist(to
, cp
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 cc
= addlist(cc
, cp
);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 from
= addlist(from
, cp
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
201 cp
= dp
= path(cp
+ 1, TSUBCWF
);
202 fcc
= addlist(fcc
, cp
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 if ((outputlinelen
= atoi(cp
)) < 10)
210 die("impossible width %d", outputlinelen
);
214 if (!(cp
= *argp
++) || *cp
== '-')
215 die("missing argument to %s", argp
[-2]);
220 if (*cp
== '+' || *cp
== '@') {
222 die("only one folder at a time!");
223 folder
= pluspath (cp
);
226 die("only one message at a time!");
231 cwd
= mh_xstrdup(pwd ());
233 if (!context_find ("path"))
234 free (path ("./", TFOLDER
));
237 * Check if we are using a draft folder
238 * and have specified a message in it.
240 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
244 if (form
&& (folder
|| msg
))
245 die("can't mix forms and folders/msgs");
251 * Use a message as the "form" for the new message.
256 folder
= getfolder (1);
257 maildir
= m_maildir (folder
);
259 if (chdir (maildir
) == NOTOK
)
260 adios (maildir
, "unable to change directory to");
262 /* read folder and create message structure */
263 if (!(mp
= folder_read (folder
, 1)))
264 die("unable to read folder %s", folder
);
266 /* check for empty folder */
268 die("no messages in %s", folder
);
270 /* parse the message range/sequence/name and set SELECTED */
271 if (!m_convert (mp
, msg
))
273 seq_setprev (mp
); /* set the previous-sequence */
277 die("only one message at a time!");
279 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
280 adios (form
, "unable to open message");
287 cp
= new_fs(form
, NULL
, NULL
);
288 format_len
= strlen(cp
);
289 fmt_compile(cp
, &fmt
, 1);
292 * Set up any components that were fed to us on the command line
296 cptr
= fmt_findcomp("from");
301 cptr
= fmt_findcomp("to");
306 cptr
= fmt_findcomp("cc");
311 cptr
= fmt_findcomp("fcc");
316 cptr
= fmt_findcomp("subject");
318 cptr
->c_text
= subject
;
323 strncpy (drft
, build
? m_maildir ("draft")
324 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
327 * Check if we have an existing draft
329 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
330 i
= fdcompare (in
, out
);
334 * If we have given -use flag, or if the
335 * draft is just the same as the components
336 * file, then no need to ask any questions.
341 if (stat (drft
, &st
) == NOTOK
)
342 adios (drft
, "unable to stat");
343 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
344 for (i
= LISTDSW
; i
!= YESW
;) {
345 if (!(argp
= read_switch_multiword ("\nDisposition? ",
346 isdf
? aqrunl
: aqrul
)))
348 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
361 showfile (++argp
, drft
);
364 if (refile (++argp
, drft
) == 0)
374 adios (drft
, "unable to open");
377 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
378 adios (drft
, "unable to create");
382 i
= format_len
+ 1024;
383 scanl
= charstring_create (i
+ 2);
387 dat
[3] = outputlinelen
;
389 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
390 if (write(out
, charstring_buffer (scanl
),
391 charstring_bytes (scanl
)) < 0) {
392 advise (drft
, "write");
394 charstring_free(scanl
);
396 cpydata (in
, out
, form
, drft
);
402 context_save (); /* save the context file */
406 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);