]>
diplodocus.org Git - nmh/blob - uip/packf.c
3 * packf.c -- pack a nmh folder into a file
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/dropsbr.h>
16 #define PACKF_SWITCHES \
17 X("file name", 0, FILESW) \
18 X("mbox", 0, MBOXSW) \
19 X("mmdf", 0, MMDFSW) \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(PACKF
);
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
31 static int md
= NOTOK
;
32 static int mbx_style
= MBOX_FORMAT
;
33 static int mapping
= 0;
35 static void mbxclose_done(int) NORETURN
;
41 main (int argc
, char **argv
)
44 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
45 char **argp
, **arguments
;
46 struct msgs_array msgs
= { 0, 0, NULL
};
53 setlocale(LC_ALL
, "");
55 invo_name
= r1bindex (argv
[0], '/');
57 /* read user profile/context */
60 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
73 adios (NULL
, "-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
86 adios (NULL
, "only one file at a time!");
87 if (!(file
= *argp
++) || *file
== '-')
88 adios (NULL
, "missing argument to %s", argp
[-2]);
92 mbx_style
= MBOX_FORMAT
;
96 mbx_style
= MMDF_FORMAT
;
101 if (*cp
== '+' || *cp
== '@') {
103 adios (NULL
, "only one folder at a time!");
104 folder
= pluspath (cp
);
106 app_msgarg(&msgs
, cp
);
111 file
= path (file
, TFILE
);
114 * Check if file to be created (or appended to)
115 * exists. If not, ask for confirmation.
117 if (stat (file
, &st
) == NOTOK
) {
119 adios (file
, "error on file");
120 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
126 if (!context_find ("path"))
127 free (path ("./", TFOLDER
));
129 /* default is to pack whole folder */
131 app_msgarg(&msgs
, "all");
134 folder
= getfolder (1);
135 maildir
= m_maildir (folder
);
137 if (chdir (maildir
) == NOTOK
)
138 adios (maildir
, "unable to change directory to ");
140 /* read folder and create message structure */
141 if (!(mp
= folder_read (folder
)))
142 adios (NULL
, "unable to read folder %s", folder
);
144 /* check for empty folder */
146 adios (NULL
, "no messages in %s", folder
);
148 /* parse all the message ranges/sequences and set SELECTED */
149 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
150 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
152 seq_setprev (mp
); /* set the previous-sequence */
154 /* open and lock new maildrop file */
155 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
156 adios (file
, "unable to open");
158 /* copy all the SELECTED messages to the file */
159 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
160 if (is_selected(mp
, msgnum
)) {
161 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
162 admonish (msgnam
, "unable to read message");
166 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
167 adios (file
, "error writing to file");
172 /* close and unlock maildrop file */
173 mbx_close (file
, md
);
175 context_replace (pfolder
, folder
); /* update current folder */
176 if (mp
->hghsel
!= mp
->curmsg
)
177 seq_setcur (mp
, mp
->lowsel
);
179 context_save (); /* save the context file */
180 folder_free (mp
); /* free folder/message structure */
186 mbxclose_done (int status
)
188 mbx_close (file
, md
);