]>
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 colums", 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
;
87 setlocale(LC_ALL
, "");
89 invo_name
= r1bindex (argv
[0], '/');
91 /* read user profile/context */
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 adios (NULL
, "-%s unknown", cp
);
107 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
109 print_help (buf
, switches
, 1);
112 print_version(invo_name
);
116 if (!(ed
= *argp
++) || *ed
== '-')
117 adios (NULL
, "missing argument to %s", argp
[-2]);
125 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
138 if (!(form
= *argp
++) || *form
== '-')
139 adios (NULL
, "missing argument to %s", argp
[-2]);
149 case FILESW
: /* compatibility */
151 adios (NULL
, "only one file at a time!");
152 if (!(file
= *argp
++) || *file
== '-')
153 adios (NULL
, "missing argument to %s", argp
[-2]);
159 adios (NULL
, "only one draft folder at a time!");
160 if (!(cp
= *argp
++) || *cp
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
162 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
163 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
167 adios (NULL
, "only one draft message at a time!");
168 if (!(file
= *argp
++) || *file
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
179 to
= addlist(to
, cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
185 cc
= addlist(cc
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios (NULL
, "missing argument to %s", argp
[-2]);
191 from
= addlist(from
, cp
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
199 cp
= dp
= path(cp
+ 1, TSUBCWF
);
200 fcc
= addlist(fcc
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 if ((outputlinelen
= atoi(cp
)) < 10)
209 adios (NULL
, "impossible width %d", outputlinelen
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
219 if (*cp
== '+' || *cp
== '@') {
221 adios (NULL
, "only one folder at a time!");
223 folder
= pluspath (cp
);
226 adios (NULL
, "only one message at a time!");
232 cwd
= getcpy (pwd ());
234 if (!context_find ("path"))
235 free (path ("./", TFOLDER
));
238 * Check if we are using a draft folder
239 * and have specified a message in it.
241 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
245 if (form
&& (folder
|| msg
))
246 adios (NULL
, "can't mix forms and folders/msgs");
252 * Use a message as the "form" for the new message.
257 folder
= getfolder (1);
258 maildir
= m_maildir (folder
);
260 if (chdir (maildir
) == NOTOK
)
261 adios (maildir
, "unable to change directory to");
263 /* read folder and create message structure */
264 if (!(mp
= folder_read (folder
, 1)))
265 adios (NULL
, "unable to read folder %s", folder
);
267 /* check for empty folder */
269 adios (NULL
, "no messages in %s", folder
);
271 /* parse the message range/sequence/name and set SELECTED */
272 if (!m_convert (mp
, msg
))
274 seq_setprev (mp
); /* set the previous-sequence */
278 adios (NULL
, "only one message at a time!");
280 if ((in
= open (form
= getcpy (m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
281 adios (form
, "unable to open message");
288 cp
= new_fs(form
, NULL
, NULL
);
289 format_len
= strlen(cp
);
290 fmt_compile(cp
, &fmt
, 1);
293 * Set up any components that were fed to us on the command line
297 cptr
= fmt_findcomp("from");
302 cptr
= fmt_findcomp("to");
307 cptr
= fmt_findcomp("cc");
312 cptr
= fmt_findcomp("fcc");
317 cptr
= fmt_findcomp("subject");
319 cptr
->c_text
= subject
;
324 strncpy (drft
, build
? m_maildir ("draft")
325 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
328 * Check if we have an existing draft
330 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
331 i
= fdcompare (in
, out
);
335 * If we have given -use flag, or if the
336 * draft is just the same as the components
337 * file, then no need to ask any questions.
342 if (stat (drft
, &st
) == NOTOK
)
343 adios (drft
, "unable to stat");
344 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
345 for (i
= LISTDSW
; i
!= YESW
;) {
346 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrunl
: aqrul
)))
348 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
361 showfile (++argp
, drft
);
364 if (refile (++argp
, drft
) == 0)
368 advise (NULL
, "say what?");
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
= mh_xmalloc((size_t) i
+ 2);
387 dat
[3] = outputlinelen
;
389 fmt_scan(fmt
, scanl
, i
+ 1, i
, dat
, NULL
);
390 write(out
, scanl
, strlen(scanl
));
393 cpydata (in
, out
, form
, drft
);
399 context_save (); /* save the context file */
403 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULLMP
, NULL
, 0, cwd
, 0);