]> diplodocus.org Git - nmh/blob - uip/packf.c
Update manpages to use .TP for tagged paragraphs (part I).
[nmh] / uip / packf.c
1
2 /*
3 * packf.c -- pack a nmh folder into a file
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/dropsbr.h>
13 #include <h/utils.h>
14 #include <errno.h>
15
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) \
22
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(PACKF);
25 #undef X
26
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(PACKF, switches);
29 #undef X
30
31 static int md = NOTOK;
32 static int mbx_style = MBOX_FORMAT;
33 static int mapping = 0;
34
35 static void mbxclose_done(int) NORETURN;
36
37 char *file = NULL;
38
39
40 int
41 main (int argc, char **argv)
42 {
43 int fd, msgnum;
44 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
45 char **argp, **arguments;
46 struct msgs_array msgs = { 0, 0, NULL };
47 struct msgs *mp;
48 struct stat st;
49
50 done=mbxclose_done;
51
52 #ifdef LOCALE
53 setlocale(LC_ALL, "");
54 #endif
55 invo_name = r1bindex (argv[0], '/');
56
57 /* read user profile/context */
58 context_read();
59
60 arguments = getarguments (invo_name, argc, argv, 1);
61 argp = arguments;
62
63 /*
64 * Parse arguments
65 */
66 while ((cp = *argp++)) {
67 if (*cp == '-') {
68 switch (smatch (++cp, switches)) {
69 case AMBIGSW:
70 ambigsw (cp, switches);
71 done (1);
72 case UNKWNSW:
73 adios (NULL, "-%s unknown", cp);
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
77 invo_name);
78 print_help (buf, switches, 1);
79 done (0);
80 case VERSIONSW:
81 print_version(invo_name);
82 done (0);
83
84 case FILESW:
85 if (file)
86 adios (NULL, "only one file at a time!");
87 if (!(file = *argp++) || *file == '-')
88 adios (NULL, "missing argument to %s", argp[-2]);
89 continue;
90
91 case MBOXSW:
92 mbx_style = MBOX_FORMAT;
93 mapping = 0;
94 continue;
95 case MMDFSW:
96 mbx_style = MMDF_FORMAT;
97 mapping = 1;
98 continue;
99 }
100 }
101 if (*cp == '+' || *cp == '@') {
102 if (folder)
103 adios (NULL, "only one folder at a time!");
104 folder = pluspath (cp);
105 } else
106 app_msgarg(&msgs, cp);
107 }
108
109 if (!file)
110 file = "./msgbox";
111 file = path (file, TFILE);
112
113 /*
114 * Check if file to be created (or appended to)
115 * exists. If not, ask for confirmation.
116 */
117 if (stat (file, &st) == NOTOK) {
118 if (errno != ENOENT)
119 adios (file, "error on file");
120 cp = concat ("Create file \"", file, "\"? ", NULL);
121 if (!getanswer (cp))
122 done (1);
123 free (cp);
124 }
125
126 if (!context_find ("path"))
127 free (path ("./", TFOLDER));
128
129 /* default is to pack whole folder */
130 if (!msgs.size)
131 app_msgarg(&msgs, "all");
132
133 if (!folder)
134 folder = getfolder (1);
135 maildir = m_maildir (folder);
136
137 if (chdir (maildir) == NOTOK)
138 adios (maildir, "unable to change directory to ");
139
140 /* read folder and create message structure */
141 if (!(mp = folder_read (folder)))
142 adios (NULL, "unable to read folder %s", folder);
143
144 /* check for empty folder */
145 if (mp->nummsg == 0)
146 adios (NULL, "no messages in %s", folder);
147
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]))
151 done (1);
152 seq_setprev (mp); /* set the previous-sequence */
153
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");
157
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");
163 break;
164 }
165
166 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
167 adios (file, "error writing to file");
168
169 close (fd);
170 }
171
172 /* close and unlock maildrop file */
173 mbx_close (file, md);
174
175 context_replace (pfolder, folder); /* update current folder */
176 if (mp->hghsel != mp->curmsg)
177 seq_setcur (mp, mp->lowsel);
178 seq_save (mp);
179 context_save (); /* save the context file */
180 folder_free (mp); /* free folder/message structure */
181 done (0);
182 return 1;
183 }
184
185 static void
186 mbxclose_done (int status)
187 {
188 mbx_close (file, md);
189 exit (status);
190 }