Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / refile.c,v
1 head    1.10;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.10
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    1.9;
12
13 1.9
14 date    92.11.04.00.58.11;      author jromine; state Exp;
15 branches;
16 next    1.8;
17
18 1.8
19 date    92.11.04.00.23.24;      author jromine; state Exp;
20 branches;
21 next    1.7;
22
23 1.7
24 date    92.01.31.22.25.26;      author jromine; state Exp;
25 branches;
26 next    1.6;
27
28 1.6
29 date    92.01.31.16.34.39;      author jromine; state Exp;
30 branches;
31 next    1.5;
32
33 1.5
34 date    91.01.14.16.27.20;      author mh;      state Exp;
35 branches;
36 next    1.4;
37
38 1.4
39 date    90.04.05.15.02.18;      author sources; state Exp;
40 branches;
41 next    1.3;
42
43 1.3
44 date    90.03.27.19.11.48;      author sources; state Exp;
45 branches;
46 next    1.2;
47
48 1.2
49 date    90.02.06.13.29.10;      author sources; state Exp;
50 branches;
51 next    1.1;
52
53 1.1
54 date    90.02.06.13.28.51;      author sources; state Exp;
55 branches;
56 next    ;
57
58
59 desc
60 @@
61
62
63 1.10
64 log
65 @endif sugar
66 @
67 text
68 @/* refile.c - file messages away */
69 #ifndef lint
70 static char ident[] = "@@(#)$Id: refile.c,v 1.9 1992/11/04 00:58:11 jromine Exp jromine $";
71 #endif  /* lint */
72
73 #include "../h/mh.h"
74 #include <errno.h>
75 #include <stdio.h>
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #ifdef LOCALE
79 #include        <locale.h>
80 #endif
81
82 /* \f */
83
84 static struct swit switches[] = {
85 #define DRAFTSW 0
86     "draft", 0,
87
88 #define LINKSW  1
89     "link", 0,
90 #define NLINKSW 2
91     "nolink", 0,
92
93 #define PRESSW  3
94     "preserve", 0,
95 #define NPRESSW 4
96     "nopreserve", 0,
97
98 #define SRCSW   5
99     "src +folder", 0,
100
101 #define FILESW  6
102     "file file", 0,
103
104 #define RPROCSW 7
105     "rmmproc program", 0,
106 #define NRPRCSW 8
107     "normmproc", 0,
108
109 #define HELPSW  9
110     "help", 4,
111
112     NULL, 0
113 };
114
115 /* \f */
116
117 extern int  errno;
118
119
120 static char maildir[BUFSIZ];
121
122
123 struct st_fold {
124     char   *f_name;
125     struct msgs *f_mp;
126 };
127
128 static opnfolds(), clsfolds(), removeit();
129 /* \f */
130
131 /* ARGSUSED */
132
133 main (argc, argv)
134 int     argc;
135 char  **argv;
136 {
137     int     linkf = 0,
138             prsrvf = 0,
139             filep = 0,
140             foldp = 0,
141             msgp = 0,
142             isdf = 0,
143             i,
144             msgnum;
145     char   *cp,
146            *folder = NULL,
147             buf[100],
148           **ap,
149           **argp,
150            *arguments[MAXARGS],
151            *filevec[NFOLDERS + 2],
152           **files = &filevec[1],        /* leave room for removeit:vec[0] */
153            *msgs[MAXARGS];
154     struct st_fold   folders[NFOLDERS + 1];
155     struct msgs *mp;
156
157 #ifdef LOCALE
158         setlocale(LC_ALL, "");
159 #endif
160     invo_name = r1bindex (argv[0], '/');
161     if ((cp = m_find (invo_name)) != NULL) {
162         ap = brkstring (cp = getcpy (cp), " ", "\n");
163         ap = copyip (ap, arguments);
164     }
165     else
166         ap = arguments;
167     (void) copyip (argv + 1, ap);
168     argp = arguments;
169
170 /* \f */
171
172     while (cp = *argp++) {
173         if (*cp == '-')
174             switch (smatch (++cp, switches)) {
175                 case AMBIGSW: 
176                     ambigsw (cp, switches);
177                     done (1);
178                 case UNKWNSW: 
179                     adios (NULLCP, "-%s unknown\n", cp);
180                 case HELPSW: 
181                     (void) sprintf (buf, "%s [msgs] [switches] +folder ...",
182                             invo_name);
183                     help (buf, switches);
184                     done (1);
185
186                 case LINKSW: 
187                     linkf++;
188                     continue;
189                 case NLINKSW: 
190                     linkf = 0;
191                     continue;
192
193                 case PRESSW: 
194                     prsrvf++;
195                     continue;
196                 case NPRESSW: 
197                     prsrvf = 0;
198                     continue;
199
200                 case SRCSW: 
201                     if (folder)
202                         adios (NULLCP, "only one source folder at a time!");
203                     if (!(cp = *argp++) || *cp == '-')
204                         adios (NULLCP, "missing argument to %s", argp[-2]);
205                     folder = path (*cp == '+' || *cp == '@@' ? cp + 1 : cp,
206                                    *cp != '@@' ? TFOLDER : TSUBCWF);
207                     continue;
208                 case DRAFTSW:
209                     if (filep > NFOLDERS)
210                         adios (NULLCP, "only %d files allowed!", NFOLDERS);
211                     isdf = 0;
212                     files[filep++] = getcpy (m_draft (NULLCP, NULLCP, 1, &isdf));
213                     continue;
214                 case FILESW: 
215                     if (filep > NFOLDERS)
216                         adios (NULLCP, "only %d files allowed!", NFOLDERS);
217                     if (!(cp = *argp++) || *cp == '-')
218                         adios (NULLCP, "missing argument to %s", argp[-2]);
219                     files[filep++] = path (cp, TFILE);
220                     continue;
221
222                 case RPROCSW: 
223                     if (!(rmmproc = *argp++) || *rmmproc == '-')
224                         adios (NULLCP, "missing argument to %s", argp[-2]);
225                     continue;
226                 case NRPRCSW: 
227                     rmmproc = (char *)0;
228                     continue;
229             }
230         if (*cp == '+' || *cp == '@@') {
231             if (foldp > NFOLDERS)
232                 adios (NULLCP, "only %d folders allowed!", NFOLDERS);
233             folders[foldp++].f_name =
234                     path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
235         }
236         else
237             msgs[msgp++] = cp;
238     }
239
240 /* \f */
241
242     if (!m_find ("path"))
243         free (path ("./", TFOLDER));
244     if (foldp == 0)
245         adios (NULLCP, "no folder specified");
246
247 #ifdef  WHATNOW
248     if (!msgp && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
249         files[filep++] = cp;
250 #endif  /* WHATNOW */
251
252     if (filep > 0) {
253         if (folder || msgp)
254             adios (NULLCP, "use -file or some messages, not both");
255         opnfolds (folders, foldp);
256         for (i = 0; i < filep; i++)
257             if (m_file (files[i], folders, foldp, prsrvf))
258                 done (1);
259         if (!linkf)
260             removeit (NULLMP, filep, filevec);
261         done (0);
262     }
263
264     if (!msgp)
265         msgs[msgp++] = "cur";
266     if (!folder)
267         folder = m_getfolder ();
268     (void) strcpy (maildir, m_maildir (folder));
269
270     if (chdir (maildir) == NOTOK)
271         adios (maildir, "unable to change directory to");
272     if (!(mp = m_gmsg (folder)))
273         adios (NULLCP, "unable to read folder %s", folder);
274     if (mp -> hghmsg == 0)
275         adios (NULLCP, "no messages in %s", folder);
276
277     for (msgnum = 0; msgnum < msgp; msgnum++)
278         if (!m_convert (mp, msgs[msgnum]))
279             done (1);
280     m_setseq (mp);
281
282     opnfolds (folders, foldp);
283     for (msgnum = mp -> lowsel; msgnum <= mp -> hghsel; msgnum++)
284         if (mp -> msgstats[msgnum] & SELECTED) {
285             cp = getcpy (m_name (msgnum));
286             if (m_file (cp, folders, foldp, prsrvf))
287                 done (1);
288             free (cp);
289             if (!linkf) {
290 #ifdef  notdef
291                 mp -> msgstats[msgnum] |= DELETED;
292 #endif  /* notdef */
293                 mp -> msgstats[msgnum] &= ~EXISTS;
294             }
295         }
296     if (!linkf)
297         mp -> msgflags |= SEQMOD;
298     clsfolds (folders, foldp);
299
300     m_replace (pfolder, folder);
301     if (mp -> hghsel != mp -> curmsg
302             && (mp -> numsel != mp -> nummsg || linkf))
303         m_setcur (mp, mp -> hghsel);
304     m_sync (mp);
305     m_update ();
306
307     if (!linkf)
308         removeit (mp, filep, filevec);
309
310     done (0);
311 }
312
313 /* \f */
314
315 static  opnfolds (folders, nfolders)
316 register struct st_fold *folders;
317 int     nfolders;
318 {
319     register char  *cp;
320     char    nmaildir[BUFSIZ];
321     register struct st_fold *fp,
322                             *ep;
323     register struct msgs   *mp;
324     struct stat st;
325
326     for (ep = (fp = folders) + nfolders; fp < ep; fp++) {
327         (void) chdir (m_maildir (""));
328         (void) strcpy (nmaildir, m_maildir (fp -> f_name));
329
330         if (stat (nmaildir, &st) == NOTOK) {
331             if (errno != ENOENT)
332                 adios (nmaildir, "error on folder");
333             cp = concat ("Create folder \"", nmaildir, "\"? ", NULLCP);
334             if (!getanswer (cp))
335                 done (1);
336             free (cp);
337             if (!makedir (nmaildir))
338                 adios (NULLCP, "unable to create folder %s", nmaildir);
339         }
340
341         if (chdir (nmaildir) == NOTOK)
342             adios (nmaildir, "unable to change directory to");
343         if (!(mp = m_gmsg (fp -> f_name)))
344             adios (NULLCP, "unable to read folder %s", fp -> f_name);
345         mp -> curmsg = 0;
346
347         fp -> f_mp = mp;
348
349         (void) chdir (maildir);
350     }
351 }
352
353 /* \f */
354
355 static  clsfolds (folders, nfolders)
356 register struct st_fold *folders;
357 int     nfolders;
358 {
359     register struct st_fold *fp,
360                            *ep;
361     register struct msgs   *mp;
362
363     for (ep = (fp = folders) + nfolders; fp < ep; fp++) {
364         mp = fp -> f_mp;
365         m_setseq (mp);
366         m_sync (mp);
367     }
368 }
369
370 /* \f */
371
372 static  removeit (mp, filep, files)
373 register struct msgs *mp;
374 register int filep;
375 register char **files;
376 {
377     register int    i,
378                     vecp;
379     register char  *cp,
380                   **vec;
381
382     if (rmmproc) {
383         if (filep > 0) {
384             vec = files++;      /* filevec[1] */
385             files[filep] = NULL;
386         }
387         else {
388             if (mp -> numsel > MAXARGS - 2)
389                 adios (NULLCP, "more than %d messages for %s exec",
390                         MAXARGS - 2, rmmproc);
391             vec = (char **) calloc ((unsigned) (mp -> numsel + 2), sizeof *vec);
392             if (vec == NULL)
393                 adios (NULLCP, "unable to allocate exec vector");
394             vecp = 1;
395             for (i = mp -> lowsel; i <= mp -> hghsel; i++)
396                 if (mp -> msgstats[i] & SELECTED)
397                     vec[vecp++] = getcpy (m_name (i));
398             vec[vecp] = NULL;
399         }
400
401         (void) fflush (stdout);
402         vec[0] = r1bindex (rmmproc, '/');
403         execvp (rmmproc, vec);
404         adios (rmmproc, "unable to exec");
405     }
406
407     if (filep > 0) {
408         files++;        /* filevec[1] */
409         for (i = 0; i < filep; i++)
410             if (unlink (files[i]) == NOTOK)
411                 admonish (files[i], "unable to unlink");
412     }
413     else
414         for (i = mp -> lowsel; i <= mp -> hghsel; i++)
415             if (mp -> msgstats[i] & SELECTED)
416                 if (unlink (cp = m_name (i)) == NOTOK)
417                     admonish (cp, "unable to unlink");
418 }
419
420 /* \f */
421
422 m_file (msg, folders, nfolders, prsrvf)
423 register char  *msg;
424 struct st_fold  *folders;
425 int     nfolders,
426         prsrvf;
427 {
428     int     in,
429             out,
430             linkerr,
431             msgnum;
432     register char  *nmsg;
433     char    newmsg[BUFSIZ];
434     register struct st_fold *fp,
435                             *ep;
436     register struct msgs *mp;
437     struct stat st,
438                 s1;
439
440     for (ep = (fp = folders) + nfolders; fp < ep; fp++) {
441         mp = fp -> f_mp;
442         if (prsrvf && (msgnum = m_atoi (nmsg = msg)) > 0) {
443             if (msgnum >= mp -> hghoff)
444                 if (mp = m_remsg (mp, 0, msgnum + MAXFOLDER))
445                     fp -> f_mp = mp;
446                 else
447                     adios (NULLCP, "unable to allocate folder storage");
448             if (!(mp -> msgstats[msgnum] & EXISTS)) {
449                 mp -> msgstats[msgnum] |= EXISTS;
450 #ifdef  notdef
451                 mp -> msgstats[msgnum] &= ~DELETED;
452 #endif  /* notdef */
453                 mp -> nummsg++;
454             }
455             mp -> msgstats[msgnum] |= SELECTED;             
456             if (msgnum > mp -> hghmsg)
457                 mp -> hghmsg = msgnum;
458         }
459         else {
460             if (mp -> hghmsg >= mp -> hghoff)
461                 if (mp = m_remsg (mp, 0, mp -> hghoff + MAXFOLDER))
462                     fp -> f_mp = mp;
463                 else
464                     adios (NULLCP, "unable to allocate folder storage");
465
466             nmsg = m_name (msgnum = ++mp -> hghmsg);
467             mp -> nummsg++;
468             mp -> msgstats[msgnum] |= EXISTS | SELECTED;
469         }
470         if (mp -> lowmsg == 0)
471             mp -> lowmsg = msgnum;
472         if (mp -> lowsel == 0 || msgnum < mp -> lowsel)
473             mp -> lowsel = msgnum;
474         if (msgnum > mp -> hghsel)
475             mp -> hghsel = msgnum;
476
477 /* \f */
478
479         (void) sprintf (newmsg, "%s/%s", mp -> foldpath, nmsg);
480         if (link (msg, newmsg) == NOTOK) {
481 #ifndef EISREMOTE
482             linkerr = errno;
483 #else   /* EISREMOTE */
484             if ((linkerr = errno) == EISREMOTE)
485                 linkerr = EXDEV;
486 #endif  /* EISREMOTE */
487             if (linkerr == EEXIST
488                     || (linkerr == EXDEV && stat (newmsg, &st) != NOTOK)) {
489                 if (linkerr != EEXIST
490                         || stat (msg, &s1) == NOTOK
491                         || stat (newmsg, &st) == NOTOK
492                         || s1.st_ino != st.st_ino) {
493                     advise (NULLCP, "message %s:%s already exists",
494                             fp -> f_name, newmsg);
495                     return 1;
496                 }
497                 continue;
498             }
499             if (linkerr == EXDEV) {
500                 if ((in = open (msg, 0)) == NOTOK) {
501                     advise (msg, "unable to open message %s");
502                     return 1;
503                 }
504                 (void) fstat (in, &st);
505                 if ((out = creat (newmsg, (int) st.st_mode & 0777))
506                         == NOTOK) {
507                     advise (newmsg, "unable to create");
508                     (void) close (in);
509                     return 1;
510                 }
511                 cpydata (in, out, msg, newmsg);
512                 (void) close (in);
513                 (void) close (out);
514             }
515             else {
516                 advise (newmsg, "error linking %s to", msg);
517                 return 1;
518             }
519         }
520     }
521
522     return 0;
523 }
524 @
525
526
527 1.9
528 log
529 @LOCALE
530 @
531 text
532 @d3 2
533 a4 2
534 static char ident[] = "@@(#)$Id: refile.c,v 1.8 1992/11/04 00:23:24 jromine Exp jromine $";
535 #endif  lint
536 d183 1
537 a183 1
538 #endif  WHATNOW
539 d225 1
540 a225 1
541 #endif  notdef
542 d385 1
543 a385 1
544 #endif  notdef
545 d416 1
546 a416 1
547 #else   EISREMOTE
548 d419 1
549 a419 1
550 #endif  EISREMOTE
551 @
552
553
554 1.8
555 log
556 @when called with -file, make sure to pass the first filename
557 to the rmmproc.  (hack!)
558 @
559 text
560 @d3 1
561 a3 1
562 static char ident[] = "@@(#)$Id: refile.c,v 1.7 1992/01/31 22:25:26 jromine Exp jromine $";
563 d11 3
564 d90 3
565 @
566
567
568 1.7
569 log
570 @kerberos
571 @
572 text
573 @d3 1
574 a3 1
575 static char ident[] = "@@(#)$Id: refile.c,v 1.6 1992/01/31 16:34:39 jromine Exp jromine $";
576 d81 2
577 a82 1
578            *files[NFOLDERS + 1],
579 d187 1
580 a187 1
581             removeit (NULLMP, filep, files);
582 d235 1
583 a235 1
584         removeit (mp, filep, files);
585 d310 4
586 a313 2
587         if (filep > 0)
588             vec = files;
589 d335 1
590 @
591
592
593 1.6
594 log
595 @Multimedia MH
596 @
597 text
598 @d3 1
599 a3 1
600 static char ident[] = "@@(#)$Id: refile.c,v 1.5 91/01/14 16:27:20 mh Exp $";
601 d42 1
602 a42 1
603     NULL, NULL
604 @
605
606
607 1.5
608 log
609 @add -[no]rmmproc
610 jlr
611 @
612 text
613 @d3 1
614 a3 1
615 static char ident[] = "@@(#)$Id: refile.c,v 1.4 90/04/05 15:02:18 sources Exp Locker: mh $";
616 d174 1
617 a174 1
618     if (!msgp && !filep && (cp = getenv ("mhdraft")) && *cp)
619 @
620
621
622 1.4
623 log
624 @add ID
625 @
626 text
627 @d3 1
628 a3 1
629 static char ident[] = "@@(#)$Id:$";
630 d34 6
631 a39 1
632 #define HELPSW  7
633 d146 8
634 @
635
636
637 1.3
638 log
639 @"remove" -> "removeit"
640 @
641 text
642 @d2 3
643 @
644
645
646 1.2
647 log
648 @ANSI Compilance
649 @
650 text
651 @d50 1
652 a50 1
653 static opnfolds(), clsfolds(), remove();
654 d170 1
655 a170 1
656             remove (NULLMP, filep, files);
657 d218 1
658 a218 1
659         remove (mp, filep, files);
660 d282 1
661 a282 1
662 static  remove (mp, filep, files)
663 @
664
665
666 1.1
667 log
668 @Initial revision
669 @
670 text
671 @d50 1
672 @