]>
diplodocus.org Git - nmh/blob - uip/packf.c
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.
10 #include <h/dropsbr.h>
13 #define PACKF_SWITCHES \
14 X("file name", 0, FILESW) \
15 X("mbox", 0, MBOXSW) \
16 X("mmdf", 0, MMDFSW) \
17 X("version", 0, VERSIONSW) \
18 X("help", 0, HELPSW) \
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(PACKF
);
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
28 static int md
= NOTOK
;
29 static int mbx_style
= MBOX_FORMAT
;
30 static int mapping
= 0;
32 static void mbxclose_done(int) NORETURN
;
38 main (int argc
, char **argv
)
41 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
42 char **argp
, **arguments
;
43 struct msgs_array msgs
= { 0, 0, NULL
};
47 if (nmh_init(argv
[0], 1)) { return 1; }
51 arguments
= getarguments (invo_name
, argc
, argv
, 1);
57 while ((cp
= *argp
++)) {
59 switch (smatch (++cp
, switches
)) {
61 ambigsw (cp
, switches
);
64 adios (NULL
, "-%s unknown", cp
);
67 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
69 print_help (buf
, switches
, 1);
72 print_version(invo_name
);
77 adios (NULL
, "only one file at a time!");
78 if (!(file
= *argp
++) || *file
== '-')
79 adios (NULL
, "missing argument to %s", argp
[-2]);
83 mbx_style
= MBOX_FORMAT
;
87 mbx_style
= MMDF_FORMAT
;
92 if (*cp
== '+' || *cp
== '@') {
94 adios (NULL
, "only one folder at a time!");
95 folder
= pluspath (cp
);
97 app_msgarg(&msgs
, cp
);
102 file
= path (file
, TFILE
);
105 * Check if file to be created (or appended to)
106 * exists. If not, ask for confirmation.
108 if (stat (file
, &st
) == NOTOK
) {
110 adios (file
, "error on file");
111 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
112 if (!read_yes_or_no_if_tty (cp
))
117 if (!context_find ("path"))
118 free (path ("./", TFOLDER
));
120 /* default is to pack whole folder */
122 app_msgarg(&msgs
, "all");
125 folder
= getfolder (1);
126 maildir
= m_maildir (folder
);
128 if (chdir (maildir
) == NOTOK
)
129 adios (maildir
, "unable to change directory to ");
131 /* read folder and create message structure */
132 if (!(mp
= folder_read (folder
, 1)))
133 adios (NULL
, "unable to read folder %s", folder
);
135 /* check for empty folder */
137 adios (NULL
, "no messages in %s", folder
);
139 /* parse all the message ranges/sequences and set SELECTED */
140 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
141 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
143 seq_setprev (mp
); /* set the previous-sequence */
145 /* open and lock new maildrop file */
146 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
147 adios (file
, "unable to open");
149 /* copy all the SELECTED messages to the file */
150 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
151 if (is_selected(mp
, msgnum
)) {
152 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
153 admonish (msgnam
, "unable to read message");
157 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
158 adios (file
, "error writing to file");
163 /* close and unlock maildrop file */
164 mbx_close (file
, md
);
166 context_replace (pfolder
, folder
); /* update current folder */
167 if (mp
->hghsel
!= mp
->curmsg
)
168 seq_setcur (mp
, mp
->lowsel
);
170 context_save (); /* save the context file */
171 folder_free (mp
); /* free folder/message structure */
177 mbxclose_done (int status
)
179 mbx_close (file
, md
);