9 date 92.12.15.00.20.22; author jromine; state Exp;
14 date 92.11.04.00.39.40; author jromine; state Exp;
19 date 92.10.29.03.49.31; author jromine; state Exp;
24 date 92.01.31.22.07.23; author jromine; state Exp;
29 date 90.04.05.15.00.42; author sources; state Exp;
34 date 90.02.06.13.16.51; author sources; state Exp;
39 date 90.02.06.13.16.32; author sources; state Exp;
53 @/* burst.c - explode digests into individual messages */
55 static char ident[] = "@@(#)$Id: burst.c,v 1.6 1992/11/04 00:39:40 jromine Exp jromine $";
60 #include <sys/types.h>
66 static cpybrst(), burst();
69 static struct swit switches[] = {
93 static char delim3[] = "-------";
96 static struct msgs *mp;
128 setlocale(LC_ALL, "");
130 invo_name = r1bindex (argv[0], '/');
131 if ((cp = m_find (invo_name)) != NULL) {
132 ap = brkstring (cp = getcpy (cp), " ", "\n");
133 ap = copyip (ap, arguments);
137 (void) copyip (argv + 1, ap);
142 while (cp = *argp++) {
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULLCP, "-%s unknown\n", cp);
151 (void) sprintf (buf, "%s [+folder] [msgs] [switches]",
153 help (buf, switches);
177 if (*cp == '+' || *cp == '@@') {
179 adios (NULLCP, "only one folder at a time!");
181 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
189 if (!m_find ("path"))
190 free (path ("./", TFOLDER));
192 msgs[msgp++] = "cur";
194 folder = m_getfolder ();
195 maildir = m_maildir (folder);
197 if (chdir (maildir) == NOTOK)
198 adios (maildir, "unable to change directory to");
199 if (!(mp = m_gmsg (folder)))
200 adios (NULLCP, "unable to read folder %s", folder);
201 if (mp -> hghmsg == 0)
202 adios (NULLCP, "no messages in %s", folder);
204 for (msgnum = 0; msgnum < msgp; msgnum++)
205 if (!m_convert (mp, msgs[msgnum]))
209 smsgs = (struct smsg *)
210 calloc ((unsigned) (MAXFOLDER + 2), sizeof *smsgs);
212 adios (NULLCP, "unable to allocate burst storage");
214 hi = mp -> hghmsg + 1;
215 for (msgnum = mp -> lowsel; msgnum <= mp -> hghsel; msgnum++)
216 if (mp -> msgstats[msgnum] & SELECTED)
217 burst (smsgs, msgnum, inplace, quietsw, verbosw);
219 free ((char *) smsgs);
221 m_replace (pfolder, folder);
223 if (mp -> lowsel != mp -> curmsg)
224 m_setcur (mp, mp -> lowsel);
227 if (hi <= mp -> hghmsg)
237 static burst (smsgs, msgnum, inplace, quietsw, verbosw)
238 register struct smsg *smsgs;
262 ld3 = strlen (delim3);
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)
269 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
270 while (fgets (buffer, sizeof buffer, in) != NULL
271 && buffer[0] == '\n')
272 pos += (long) strlen (buffer);
275 (void) fseek (in, pos, 0);
276 smsgs[msgp].s_start = pos;
279 fgets (buffer, sizeof buffer, in) != NULL;
281 if (strncmp (buffer, delim3, ld3) == 0
282 && (msgp == 1 || c == '\n')
283 && ((cc = peekc (in)) == '\n' || cc == EOF))
286 pos += (long) strlen (buffer);
288 wasdlm = strncmp (buffer, delim3, ld3) == 0;
289 if (smsgs[msgp].s_start != pos)
290 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
294 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
295 msgp++; /* fake "End of XXX Digest" */
300 pos += (long) strlen (buffer);
305 switch (msgp--) { /* toss "End of XXX Digest" */
307 adios (NULLCP, "burst() botch -- you lose big");
311 admonish (NULLCP, "message %d not in digest format", msgnum);
317 printf ("%d message%s exploded from digest %d\n",
318 msgp, msgp != 1 ? "s" : "", msgnum);
321 /* msgp now contains the number of new msgs to be created */
323 if ((mp = m_remsg (mp, 0, mp -> hghmsg + msgp)) == NULL)
324 adios (NULLCP, "unable to allocate folder storage");
328 j = mp -> hghmsg; /* old value */
329 mp -> hghmsg += msgp;
330 mp -> nummsg += msgp;
331 if (mp -> hghsel > msgnum)
332 mp -> hghsel += msgp;
335 for (i = mp -> hghmsg; j > msgnum; i--, j--) {
336 (void) strcpy (f1, m_name (i));
337 (void) strcpy (f2, m_name (j));
338 if (mp -> msgstats[j] & EXISTS) {
340 printf ("message %d becomes message %d\n", j, i);
342 if (rename (f2, f1) == NOTOK)
343 admonish (f1, "unable to rename %s to", f2);
344 mp -> msgstats[i] = mp -> msgstats[j];
345 mp -> msgstats[j] = 0;
346 mp -> msgflags |= SEQMOD;
350 mp -> msgstats[msgnum] &= ~SELECTED;
351 i = inplace ? msgnum + msgp : mp -> hghmsg; /* new hghmsg is hghmsg+msgp */
352 for (j = msgp; j >= (inplace ? 0 : 1); i--, j--) {
353 (void) strcpy (f1, m_name (i));
354 (void) strcpy (f2, m_scratch ("", invo_name));
355 if (verbosw && i != msgnum)
356 printf ("message %d of digest %d becomes message %d\n",
359 if ((out = fopen (f2, "w")) == NULL)
360 adios (f2, "unable to write message");
361 (void) chmod (f2, mode);
362 (void) fseek (in, pos = smsgs[j].s_start, 0);
363 cpybrst (in, out, msgnam, f2,
364 (int) (smsgs[j].s_stop - smsgs[j].s_start));
368 (void) strcpy (f3, m_backup (f1));
369 if (rename (f1, f3) == NOTOK)
370 admonish (f3, "unable to rename %s to", f1);
372 if (rename (f2, f1) == NOTOK)
373 admonish (f1, "unable to rename %s to", f2);
374 mp -> msgstats[i] = mp -> msgstats[msgnum];
375 mp -> msgflags |= SEQMOD;
388 static cpybrst (in, out, ifile, ofile, len)
391 register char *ifile,
398 for (state = S1; (c = fgetc (in)) != EOF && len > 0; len--) {
411 (void) fputc (c, out);
421 (void) fputc (c, out);
433 state = c == '\n' ? S1 : S2;
434 (void) fputc ('-', out);
435 (void) fputc (c, out);
442 if (ferror (in) && !feof (in))
443 adios (ifile, "error reading");
445 adios (ofile, "error writing");
457 static char ident[] = "@@(#)$Id: burst.c,v 1.5 1992/10/29 03:49:31 jromine Exp jromine $";
464 @fix burst -- previously it was losing the last message(!)
469 static char ident[] = "@@(#)$Id: burst.c,v 1.4 1992/01/31 22:07:23 jromine Exp jromine $";
482 static char ident[] = "@@(#)$Id: burst.c,v 1.3 1990/04/05 15:00:42 sources Exp jromine $";
486 for (msgp = 1, pos = 0L; msgp <= MAXFOLDER;) {
489 && peekc (in) == '\n'
490 && (msgp == 1 || c == '\n'))
493 smsgs[msgp++].s_stop = c == '\n' && wasdlm ? pos - 1 : pos;
498 switch (--msgp) { /* toss "End of XXX Digest" */
501 msgp - 1, msgp - 1 != 1 ? "s" : "", msgnum);
502 if (msgp == 2) /* XXX */
509 mp -> hghmsg += msgp - 1;
510 mp -> nummsg += msgp - 1;
513 mp -> hghsel += msgp - 1;
516 if (inplace && msgp > 1)
519 i = inplace ? msgnum + msgp - 1 : mp -> hghmsg;
520 for (j = msgp; j >= (inplace ? 1 : 2); i--, j--) {
531 static char ident[] = "@@(#)$Id:$";
540 mp -> msgstats[j] = NULL;