]>
diplodocus.org Git - nmh/blob - uip/burst.c
3 * burst.c -- explode digests into individual messages
10 static struct swit switches
[] = {
30 static char delim3
[] = "-------";
40 static int find_delim (int, struct smsg
*);
41 static void burst (struct msgs
**, int, struct smsg
*, int, int, int);
42 static void cpybrst (FILE *, FILE *, char *, char *, int);
46 main (int argc
, char **argv
)
48 int inplace
= 0, quietsw
= 0, verbosw
= 0;
49 int msgp
= 0, hi
, msgnum
, numburst
;
50 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
51 char **argp
, **arguments
, *msgs
[MAXARGS
];
56 setlocale(LC_ALL
, "");
58 invo_name
= r1bindex (argv
[0], '/');
60 /* read user profile/context */
63 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\n", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
106 if (*cp
== '+' || *cp
== '@') {
108 adios (NULL
, "only one folder at a time!");
110 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
116 if (!context_find ("path"))
117 free (path ("./", TFOLDER
));
119 msgs
[msgp
++] = "cur";
121 folder
= getfolder (1);
122 maildir
= m_maildir (folder
);
124 if (chdir (maildir
) == NOTOK
)
125 adios (maildir
, "unable to change directory to");
127 /* read folder and create message structure */
128 if (!(mp
= folder_read (folder
)))
129 adios (NULL
, "unable to read folder %s", folder
);
131 /* check for empty folder */
133 adios (NULL
, "no messages in %s", folder
);
135 /* parse all the message ranges/sequences and set SELECTED */
136 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
137 if (!m_convert (mp
, msgs
[msgnum
]))
139 seq_setprev (mp
); /* set the previous-sequence */
141 smsgs
= (struct smsg
*)
142 calloc ((size_t) (MAXFOLDER
+ 2), sizeof(*smsgs
));
144 adios (NULL
, "unable to allocate burst storage");
148 /* burst all the SELECTED messages */
149 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
150 if (is_selected (mp
, msgnum
)) {
151 if ((numburst
= find_delim (msgnum
, smsgs
)) >= 1) {
153 printf ("%d message%s exploded from digest %d\n",
154 numburst
, numburst
> 1 ? "s" : "", msgnum
);
155 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
);
159 admonish (NULL
, "message %d not in digest format", msgnum
);
161 adios (NULL
, "burst() botch -- you lose big");
166 free ((char *) smsgs
);
167 context_replace (pfolder
, folder
); /* update current folder */
170 * If -inplace is given, then the first message burst becomes
171 * the current message (which will now show a table of contents).
172 * Otherwise, the first message extracted from the first digest
173 * becomes the current message.
176 if (mp
->lowsel
!= mp
->curmsg
)
177 seq_setcur (mp
, mp
->lowsel
);
179 if (hi
<= mp
->hghmsg
)
183 seq_save (mp
); /* synchronize message sequences */
184 context_save (); /* save the context file */
185 folder_free (mp
); /* free folder/message structure */
191 * Scan the message and find the beginning and
192 * end of all the messages in the digest.
196 find_delim (int msgnum
, struct smsg
*smsgs
)
198 int ld3
, wasdlm
, msgp
;
205 ld3
= strlen (delim3
);
207 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
208 adios (msgnam
, "unable to read message");
210 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
211 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
212 pos
+= (long) strlen (buffer
);
215 fseek (in
, pos
, SEEK_SET
);
216 smsgs
[msgp
].s_start
= pos
;
218 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
219 if (strncmp (buffer
, delim3
, ld3
) == 0
220 && (msgp
== 1 || c
== '\n')
221 && ((cc
= peekc (in
)) == '\n' || cc
== EOF
))
224 pos
+= (long) strlen (buffer
);
227 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
228 if (smsgs
[msgp
].s_start
!= pos
)
229 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
233 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
234 msgp
++; /* fake "End of XXX Digest" */
239 pos
+= (long) strlen (buffer
);
243 return (msgp
- 1); /* toss "End of XXX Digest" */
248 * Burst out the messages in the digest into the folder
252 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
253 int inplace
, int verbosw
)
257 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
262 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
263 adios (msgnam
, "unable to read message");
265 mode
= fstat (fileno(in
), &st
) != NOTOK
? (st
.st_mode
& 0777) : m_gmprot();
269 * See if we have enough space in the folder
270 * structure for all the new messages.
272 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
273 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
274 adios (NULL
, "unable to allocate folder storage");
277 j
= mp
->hghmsg
; /* old value */
278 mp
->hghmsg
+= numburst
;
279 mp
->nummsg
+= numburst
;
282 * If this is not the highest SELECTED message, then
283 * increment mp->hghsel by numburst, since the highest
284 * SELECTED is about to be slid down by that amount.
286 if (msgnum
< mp
->hghsel
)
287 mp
->hghsel
+= numburst
;
290 * If -inplace is given, renumber the messages after the
291 * source message, to make room for each of the messages
292 * contained within the digest.
295 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
296 strncpy (f1
, m_name (i
), sizeof(f1
));
297 strncpy (f2
, m_name (j
), sizeof(f2
));
298 if (does_exist (mp
, j
)) {
300 printf ("message %d becomes message %d\n", j
, i
);
302 if (rename (f2
, f1
) == NOTOK
)
303 admonish (f1
, "unable to rename %s to", f2
);
304 copy_msg_flags (mp
, i
, j
);
305 clear_msg_flags (mp
, j
);
306 mp
->msgflags
|= SEQMOD
;
311 unset_selected (mp
, msgnum
);
313 /* new hghmsg is hghmsg + numburst */
314 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
315 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
316 strncpy (f1
, m_name (i
), sizeof(f1
));
317 strncpy (f2
, m_scratch ("", invo_name
), sizeof(f2
));
318 if (verbosw
&& i
!= msgnum
)
319 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
321 if ((out
= fopen (f2
, "w")) == NULL
)
322 adios (f2
, "unable to write message");
324 fseek (in
, smsgs
[j
].s_start
, SEEK_SET
);
325 cpybrst (in
, out
, msgnam
, f2
,
326 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
330 strncpy (f3
, m_backup (f1
), sizeof(f3
));
331 if (rename (f1
, f3
) == NOTOK
)
332 admonish (f3
, "unable to rename %s to", f1
);
334 if (rename (f2
, f1
) == NOTOK
)
335 admonish (f1
, "unable to rename %s to", f2
);
336 copy_msg_flags (mp
, i
, msgnum
);
337 mp
->msgflags
|= SEQMOD
;
349 * Copy a mesage which is being burst out of a digest.
350 * It will remove any "dashstuffing" in the message.
354 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
356 register int c
, state
;
358 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
393 state
= (c
== '\n') ? S1
: S2
;
402 if (ferror (in
) && !feof (in
))
403 adios (ifile
, "error reading");
405 adios (ofile
, "error writing");