]>
diplodocus.org Git - nmh/blob - uip/packf.c
a813630cc6b5709f3e417bf9d381ce120969abcb
1 /* packf.c -- pack a nmh folder into a file
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/seq_save.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/error.h"
24 #include "h/dropsbr.h"
27 #include "sbr/m_maildir.h"
29 #define PACKF_SWITCHES \
30 X("file name", 0, FILESW) \
31 X("mbox", 0, MBOXSW) \
32 X("mmdf", 0, MMDFSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(PACKF
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
44 static int md
= NOTOK
;
45 static int mbx_style
= MBOX_FORMAT
;
47 static void mbxclose_done(int) NORETURN
;
53 main (int argc
, char **argv
)
56 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
57 char **argp
, **arguments
;
58 struct msgs_array msgs
= { 0, 0, NULL
};
62 if (nmh_init(argv
[0], true, true)) { return 1; }
64 set_done(mbxclose_done
);
66 arguments
= getarguments (invo_name
, argc
, argv
, 1);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
79 die("-%s unknown", cp
);
82 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
84 print_help (buf
, switches
, 1);
87 print_version(invo_name
);
92 die("only one file at a time!");
93 if (!(file
= *argp
++) || *file
== '-')
94 die("missing argument to %s", argp
[-2]);
98 mbx_style
= MBOX_FORMAT
;
101 mbx_style
= MMDF_FORMAT
;
105 if (*cp
== '+' || *cp
== '@') {
107 die("only one folder at a time!");
108 folder
= pluspath (cp
);
110 app_msgarg(&msgs
, cp
);
115 file
= path (file
, TFILE
);
118 * Check if file to be created (or appended to)
119 * exists. If not, ask for confirmation.
121 if (stat (file
, &st
) == NOTOK
) {
123 adios (file
, "error on file");
124 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
125 if (!read_yes_or_no_if_tty (cp
))
130 if (!context_find ("path"))
131 free (path ("./", TFOLDER
));
133 /* default is to pack whole folder */
135 app_msgarg(&msgs
, "all");
138 folder
= getfolder (1);
139 maildir
= m_maildir (folder
);
141 if (chdir (maildir
) == NOTOK
)
142 adios (maildir
, "unable to change directory to ");
144 /* read folder and create message structure */
145 if (!(mp
= folder_read (folder
, 1)))
146 die("unable to read folder %s", folder
);
148 /* check for empty folder */
150 die("no messages in %s", folder
);
152 /* parse all the message ranges/sequences and set SELECTED */
153 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
154 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
156 seq_setprev (mp
); /* set the previous-sequence */
158 /* open and lock new maildrop file */
159 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
160 adios (file
, "unable to open");
162 /* copy all the SELECTED messages to the file */
163 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
164 if (is_selected(mp
, msgnum
)) {
165 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
166 admonish (msgnam
, "unable to read message");
170 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
171 adios (file
, "error writing to file");
176 /* close and unlock maildrop file */
177 mbx_close (file
, md
);
179 context_replace (pfolder
, folder
); /* update current folder */
180 if (mp
->hghsel
!= mp
->curmsg
)
181 seq_setcur (mp
, mp
->lowsel
);
183 context_save (); /* save the context file */
184 folder_free (mp
); /* free folder/message structure */
190 mbxclose_done (int status
)
192 mbx_close (file
, md
);