2 ** packf.c -- pack a nmh folder into a file
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/dropsbr.h>
18 static struct swit switches[] = {
26 char *version=VERSION;
29 main(int argc, char **argv)
32 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
33 char **argp, **arguments;
34 struct msgs_array msgs = { 0, 0, NULL };
37 setlocale(LC_ALL, "");
38 invo_name = mhbasename(argv[0]);
40 /* read user profile/context */
43 arguments = getarguments(invo_name, argc, argv, 1);
49 while ((cp = *argp++)) {
51 switch (smatch(++cp, switches)) {
53 ambigsw(cp, switches);
56 adios(EX_USAGE, NULL, "-%s unknown", cp);
59 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
60 print_help(buf, switches, 1);
61 exit(argc == 2 ? EX_OK : EX_USAGE);
63 print_version(invo_name);
64 exit(argc == 2 ? EX_OK : EX_USAGE);
67 if (*cp == '+' || *cp == '@') {
69 adios(EX_USAGE, NULL, "only one folder at a time!");
70 folder = mh_xstrdup(expandfol(cp));
72 app_msgarg(&msgs, cp);
75 /* default is to pack whole folder */
77 app_msgarg(&msgs, seq_all);
81 maildir = toabsdir(folder);
83 if (chdir(maildir) == NOTOK)
84 adios(EX_OSERR, maildir, "unable to change directory to ");
86 /* read folder and create message structure */
87 if (!(mp = folder_read(folder)))
88 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
90 /* check for empty folder */
92 adios(EX_DATAERR, NULL, "no messages in %s", folder);
94 /* parse all the message ranges/sequences and set SELECTED */
95 for (msgnum = 0; msgnum < msgs.size; msgnum++)
96 if (!m_convert(mp, msgs.msgs[msgnum]))
98 seq_setprev(mp); /* set the previous-sequence */
100 /* copy all the SELECTED messages to stdout */
101 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
102 if (is_selected(mp, msgnum)) {
103 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
105 admonish(msgnam, "unable to read message");
108 if (mbox_copy(fileno(stdout), fd) == NOTOK) {
109 adios(EX_IOERR, NULL, "error writing to stdout");
114 context_replace(curfolder, folder);
115 if (mp->hghsel != mp->curmsg)
116 seq_setcur(mp, mp->lowsel);