]>
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",
161 } /* this pair of braces was missing before 1999-07-15 */
163 adios (NULL
, "burst() botch -- you lose big");
168 free ((char *) smsgs
);
169 context_replace (pfolder
, folder
); /* update current folder */
172 * If -inplace is given, then the first message burst becomes
173 * the current message (which will now show a table of contents).
174 * Otherwise, the first message extracted from the first digest
175 * becomes the current message.
178 if (mp
->lowsel
!= mp
->curmsg
)
179 seq_setcur (mp
, mp
->lowsel
);
181 if (hi
<= mp
->hghmsg
)
185 seq_save (mp
); /* synchronize message sequences */
186 context_save (); /* save the context file */
187 folder_free (mp
); /* free folder/message structure */
193 * Scan the message and find the beginning and
194 * end of all the messages in the digest.
198 find_delim (int msgnum
, struct smsg
*smsgs
)
200 int ld3
, wasdlm
, msgp
;
207 ld3
= strlen (delim3
);
209 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
210 adios (msgnam
, "unable to read message");
212 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
213 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
214 pos
+= (long) strlen (buffer
);
217 fseek (in
, pos
, SEEK_SET
);
218 smsgs
[msgp
].s_start
= pos
;
220 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
221 if (strncmp (buffer
, delim3
, ld3
) == 0
222 && (msgp
== 1 || c
== '\n')
223 && ((cc
= peekc (in
)) == '\n' || cc
== EOF
))
226 pos
+= (long) strlen (buffer
);
229 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
230 if (smsgs
[msgp
].s_start
!= pos
)
231 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
235 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
236 msgp
++; /* fake "End of XXX Digest" */
241 pos
+= (long) strlen (buffer
);
245 return (msgp
- 1); /* toss "End of XXX Digest" */
250 * Burst out the messages in the digest into the folder
254 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
255 int inplace
, int verbosw
)
259 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
264 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
265 adios (msgnam
, "unable to read message");
267 mode
= fstat (fileno(in
), &st
) != NOTOK
? (st
.st_mode
& 0777) : m_gmprot();
271 * See if we have enough space in the folder
272 * structure for all the new messages.
274 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
275 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
276 adios (NULL
, "unable to allocate folder storage");
279 j
= mp
->hghmsg
; /* old value */
280 mp
->hghmsg
+= numburst
;
281 mp
->nummsg
+= numburst
;
284 * If this is not the highest SELECTED message, then
285 * increment mp->hghsel by numburst, since the highest
286 * SELECTED is about to be slid down by that amount.
288 if (msgnum
< mp
->hghsel
)
289 mp
->hghsel
+= numburst
;
292 * If -inplace is given, renumber the messages after the
293 * source message, to make room for each of the messages
294 * contained within the digest.
297 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
298 strncpy (f1
, m_name (i
), sizeof(f1
));
299 strncpy (f2
, m_name (j
), sizeof(f2
));
300 if (does_exist (mp
, j
)) {
302 printf ("message %d becomes message %d\n", j
, i
);
304 if (rename (f2
, f1
) == NOTOK
)
305 admonish (f1
, "unable to rename %s to", f2
);
306 copy_msg_flags (mp
, i
, j
);
307 clear_msg_flags (mp
, j
);
308 mp
->msgflags
|= SEQMOD
;
313 unset_selected (mp
, msgnum
);
315 /* new hghmsg is hghmsg + numburst */
316 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
317 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
318 strncpy (f1
, m_name (i
), sizeof(f1
));
319 strncpy (f2
, m_scratch ("", invo_name
), sizeof(f2
));
320 if (verbosw
&& i
!= msgnum
)
321 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
323 if ((out
= fopen (f2
, "w")) == NULL
)
324 adios (f2
, "unable to write message");
326 fseek (in
, smsgs
[j
].s_start
, SEEK_SET
);
327 cpybrst (in
, out
, msgnam
, f2
,
328 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
332 strncpy (f3
, m_backup (f1
), sizeof(f3
));
333 if (rename (f1
, f3
) == NOTOK
)
334 admonish (f3
, "unable to rename %s to", f1
);
336 if (rename (f2
, f1
) == NOTOK
)
337 admonish (f1
, "unable to rename %s to", f2
);
338 copy_msg_flags (mp
, i
, msgnum
);
339 mp
->msgflags
|= SEQMOD
;
351 * Copy a mesage which is being burst out of a digest.
352 * It will remove any "dashstuffing" in the message.
356 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
358 register int c
, state
;
360 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
395 state
= (c
== '\n') ? S1
: S2
;
404 if (ferror (in
) && !feof (in
))
405 adios (ifile
, "error reading");
407 adios (ofile
, "error writing");