]>
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>
15 #define PACKF_SWITCHES \
16 X("file name", 0, FILESW) \
17 X("mbox", 0, MBOXSW) \
18 X("mmdf", 0, MMDFSW) \
19 X("version", 0, VERSIONSW) \
20 X("help", 0, HELPSW) \
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(PACKF
);
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
30 static int md
= NOTOK
;
31 static int mbx_style
= MBOX_FORMAT
;
32 static int mapping
= 0;
34 static void mbxclose_done(int) NORETURN
;
40 main (int argc
, char **argv
)
43 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
44 char **argp
, **arguments
;
45 struct msgs_array msgs
= { 0, 0, NULL
};
49 if (nmh_init(argv
[0], 1)) { return 1; }
53 arguments
= getarguments (invo_name
, argc
, argv
, 1);
59 while ((cp
= *argp
++)) {
61 switch (smatch (++cp
, switches
)) {
63 ambigsw (cp
, switches
);
66 adios (NULL
, "-%s unknown", cp
);
69 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
71 print_help (buf
, switches
, 1);
74 print_version(invo_name
);
79 adios (NULL
, "only one file at a time!");
80 if (!(file
= *argp
++) || *file
== '-')
81 adios (NULL
, "missing argument to %s", argp
[-2]);
85 mbx_style
= MBOX_FORMAT
;
89 mbx_style
= MMDF_FORMAT
;
94 if (*cp
== '+' || *cp
== '@') {
96 adios (NULL
, "only one folder at a time!");
97 folder
= pluspath (cp
);
99 app_msgarg(&msgs
, cp
);
104 file
= path (file
, TFILE
);
107 * Check if file to be created (or appended to)
108 * exists. If not, ask for confirmation.
110 if (stat (file
, &st
) == NOTOK
) {
112 adios (file
, "error on file");
113 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
114 if (!read_yes_or_no_if_tty (cp
))
119 if (!context_find ("path"))
120 free (path ("./", TFOLDER
));
122 /* default is to pack whole folder */
124 app_msgarg(&msgs
, "all");
127 folder
= getfolder (1);
128 maildir
= m_maildir (folder
);
130 if (chdir (maildir
) == NOTOK
)
131 adios (maildir
, "unable to change directory to ");
133 /* read folder and create message structure */
134 if (!(mp
= folder_read (folder
, 1)))
135 adios (NULL
, "unable to read folder %s", folder
);
137 /* check for empty folder */
139 adios (NULL
, "no messages in %s", folder
);
141 /* parse all the message ranges/sequences and set SELECTED */
142 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
143 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
145 seq_setprev (mp
); /* set the previous-sequence */
147 /* open and lock new maildrop file */
148 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
149 adios (file
, "unable to open");
151 /* copy all the SELECTED messages to the file */
152 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
153 if (is_selected(mp
, msgnum
)) {
154 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
155 admonish (msgnam
, "unable to read message");
159 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
160 adios (file
, "error writing to file");
165 /* close and unlock maildrop file */
166 mbx_close (file
, md
);
168 context_replace (pfolder
, folder
); /* update current folder */
169 if (mp
->hghsel
!= mp
->curmsg
)
170 seq_setcur (mp
, mp
->lowsel
);
172 context_save (); /* save the context file */
173 folder_free (mp
); /* free folder/message structure */
179 mbxclose_done (int status
)
181 mbx_close (file
, md
);