]> diplodocus.org Git - nmh/blob - uip/msh.c
Ignore a.out.DSYM (this file sometims shows up when using the debugger)
[nmh] / uip / msh.c
1
2 /*
3 * msh.c -- The nmh shell
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 /*
11 * TODO:
12 * Keep more status information in maildrop map
13 */
14
15 #include <h/mh.h>
16 #include <fcntl.h>
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
21 #include <h/tws.h>
22 #include <h/mts.h>
23 #include <h/utils.h>
24
25 #include <termios.h>
26
27 #include <pwd.h>
28 #include <setjmp.h>
29 #include <signal.h>
30 #include <h/msh.h>
31 #include <h/vmhsbr.h>
32
33 #define QUOTE '\\' /* sigh */
34
35 static struct swit switches[] = {
36 #define IDSW 0
37 { "idstart number", -7 }, /* interface from bbc */
38 #define FDSW 1
39 { "idstop number", -6 }, /* .. */
40 #define QDSW 2
41 { "idquit number", -6 }, /* .. */
42 #define NMSW 3
43 { "idname BBoard", -6 }, /* .. */
44 #define PRMPTSW 4
45 { "prompt string", 0 },
46 #define SCANSW 5
47 { "scan", 0 },
48 #define NSCANSW 6
49 { "noscan", 0 },
50 #define READSW 7
51 { "vmhread fd", -7 },
52 #define WRITESW 8
53 { "vmhwrite fd", -8 },
54 #define PREADSW 9
55 { "popread fd", -7 },
56 #define PWRITSW 10
57 { "popwrite fd", -8 },
58 #define TCURSW 11
59 { "topcur", 0 },
60 #define NTCURSW 12
61 { "notopcur", 0 },
62 #define VERSIONSW 13
63 { "version", 0 },
64 #define HELPSW 14
65 { "help", 0 },
66 { NULL, 0 }
67 };
68
69 static int mbx_style = MMDF_FORMAT;
70
71 /*
72 * FOLDER
73 */
74 char*fmsh = NULL; /* folder instead of file */
75 int modified; /* command modified folder */
76 struct msgs *mp; /* used a lot */
77 static int nMsgs = 0;
78 struct Msg *Msgs = NULL; /* Msgs[0] not used */
79 static FILE *fp; /* input file */
80 static FILE *yp = NULL; /* temporary file */
81 static int mode; /* mode of file */
82 static int numfds = 0; /* number of files cached */
83 static int maxfds = 0; /* number of files cached to be cached */
84 static time_t mtime = (time_t) 0; /* mtime of file */
85
86 /*
87 * VMH
88 */
89 #define ALARM ((unsigned int) 10)
90 #define ttyN(c) ttyNaux ((c), NULL)
91
92 static int vmh = 0;
93
94 static int vmhpid = OK;
95 static int vmhfd0;
96 static int vmhfd1;
97 static int vmhfd2;
98
99 static int vmhtty = NOTOK;
100
101 #define SCAN 1
102 #define STATUS 2
103 #define DISPLAY 3
104 #define NWIN DISPLAY
105
106 static int topcur = 0;
107
108 static int numwins = 0;
109 static int windows[NWIN + 1];
110
111 static jmp_buf peerenv;
112
113 /*
114 * PARENT
115 */
116 static int pfd = NOTOK; /* fd parent is reading from */
117 static int ppid = 0; /* pid of parent */
118
119 /*
120 * COMMAND
121 */
122 int interactive; /* running from a /dev/tty */
123 int redirected; /* re-directing output */
124 FILE *sp = NULL; /* original stdout */
125
126 char *cmd_name; /* command being run */
127 char myfilter[BUFSIZ]; /* path to mhl.forward */
128
129 static char *myprompt = "(%s) ";/* prompting string */
130
131 /*
132 * BBOARDS
133 */
134 static int gap; /* gap in BBoard-ID:s */
135 static char *myname = NULL; /* BBoard name */
136 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
137
138 /*
139 * SIGNALS
140 */
141 SIGNAL_HANDLER istat; /* original SIGINT */
142 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
143 SIGNAL_HANDLER qstat; /* original SIGQUIT */
144
145 #ifdef SIGTSTP
146 SIGNAL_HANDLER tstat; /* original SIGTSTP */
147 #endif
148
149 int interrupted; /* SIGINT detected */
150 int broken_pipe; /* SIGPIPE detected */
151 int told_to_quit; /* SIGQUIT detected */
152
153 /*
154 * prototypes
155 */
156 int SOprintf (char *, ...); /* from termsbr.c */
157 int sc_width (void); /* from termsbr.c */
158 void fsetup (char *);
159 void setup (char *);
160 FILE *msh_ready (int, int);
161 void readids (int);
162 int readid (int);
163 void display_info (int);
164 int expand (char *);
165 void m_reset (void);
166 void seq_setcur (struct msgs *, int);
167 void padios (char *, char *, ...);
168 void padvise (char *, char *, ...);
169
170
171 /*
172 * static prototypes
173 */
174 static void msh (int);
175 static int read_map (char *, long);
176 static int read_file (long, int);
177
178 static void m_gMsgs (int);
179 FILE *msh_ready (int, int);
180 static int check_folder (int);
181 static void scanrange (int, int);
182 static void scanstring (char *);
183 static void write_ids (void);
184 static void quit (void);
185 static int getargs (char *, struct swit *, struct Cmd *);
186 static int getcmds (struct swit *, struct Cmd *, int);
187 static int parse (char *, struct Cmd *);
188 static int init_io (struct Cmd *, int);
189 static int initaux_io (struct Cmd *);
190 static void fin_io (struct Cmd *, int);
191 static void finaux_io (struct Cmd *);
192 static void m_init (void);
193 static void intrser (int);
194 static void pipeser (int);
195 static void quitser (int);
196 static void alrmser (int);
197 static int pINI (void);
198 static int pQRY (char *, int);
199 static int pQRY1 (int);
200 static int pQRY2 (void);
201 static int pCMD (char *, struct swit *, struct Cmd *);
202 static int pFIN (void);
203 static int peerwait (void);
204 static int ttyNaux (struct Cmd *, char *);
205 static int ttyR (struct Cmd *);
206 static int winN (struct Cmd *, int, int);
207 static int winR (struct Cmd *);
208 static int winX (int);
209
210
211 int
212 main (int argc, char **argv)
213 {
214 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
215 char *cp, *file = NULL, *folder = NULL;
216 char **argp, **arguments, buf[BUFSIZ];
217
218 #ifdef LOCALE
219 setlocale(LC_ALL, "");
220 #endif
221 invo_name = r1bindex (argv[0], '/');
222
223 /* read user profile/context */
224 context_read();
225
226 mts_init (invo_name);
227 arguments = getarguments (invo_name, argc,argv, 1);
228 argp = arguments;
229
230 while ((cp = *argp++)) {
231 if (*cp == '-')
232 switch (smatch (++cp, switches)) {
233 case AMBIGSW:
234 ambigsw (cp, switches);
235 done (1);
236 case UNKWNSW:
237 adios (NULL, "-%s unknown", cp);
238
239 case HELPSW:
240 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
241 print_help (buf, switches, 1);
242 done (1);
243 case VERSIONSW:
244 print_version(invo_name);
245 done (1);
246
247 case IDSW:
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 if ((id = atoi (cp)) < 1)
251 adios (NULL, "bad argument %s %s", argp[-2], cp);
252 continue;
253 case FDSW:
254 if (!(cp = *argp++) || *cp == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
256 if ((pfd = atoi (cp)) <= 1)
257 adios (NULL, "bad argument %s %s", argp[-2], cp);
258 continue;
259 case QDSW:
260 if (!(cp = *argp++) || *cp == '-')
261 adios (NULL, "missing argument to %s", argp[-2]);
262 if ((ppid = atoi (cp)) <= 1)
263 adios (NULL, "bad argument %s %s", argp[-2], cp);
264 continue;
265 case NMSW:
266 if (!(myname = *argp++) || *myname == '-')
267 adios (NULL, "missing argument to %s", argp[-2]);
268 continue;
269
270 case SCANSW:
271 scansw++;
272 continue;
273 case NSCANSW:
274 scansw = 0;
275 continue;
276
277 case PRMPTSW:
278 if (!(myprompt = *argp++) || *myprompt == '-')
279 adios (NULL, "missing argument to %s", argp[-2]);
280 continue;
281
282 case READSW:
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
285 if ((vmh1 = atoi (cp)) < 1)
286 adios (NULL, "bad argument %s %s", argp[-2], cp);
287 continue;
288 case WRITESW:
289 if (!(cp = *argp++) || *cp == '-')
290 adios (NULL, "missing argument to %s", argp[-2]);
291 if ((vmh2 = atoi (cp)) < 1)
292 adios (NULL, "bad argument %s %s", argp[-2], cp);
293 continue;
294
295 case PREADSW:
296 if (!(cp = *argp++) || *cp == '-')
297 adios (NULL, "missing argument to %s", argp[-2]);
298 continue;
299 case PWRITSW:
300 if (!(cp = *argp++) || *cp == '-')
301 adios (NULL, "missing argument to %s", argp[-2]);
302 continue;
303
304 case TCURSW:
305 topcur++;
306 continue;
307 case NTCURSW:
308 topcur = 0;
309 continue;
310 }
311 if (*cp == '+' || *cp == '@') {
312 if (folder)
313 adios (NULL, "only one folder at a time!");
314 else
315 folder = pluspath (cp);
316 }
317 else
318 if (file)
319 adios (NULL, "only one file at a time!");
320 else
321 file = cp;
322 }
323
324 if (!file && !folder)
325 file = "./msgbox";
326 if (file && folder)
327 adios (NULL, "use a file or a folder, not both");
328 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
329 #ifdef FIOCLEX
330 if (pfd > 1)
331 ioctl (pfd, FIOCLEX, NULL);
332 #endif /* FIOCLEX */
333
334 istat = SIGNAL2 (SIGINT, intrser);
335 qstat = SIGNAL2 (SIGQUIT, quitser);
336
337 sc_width (); /* MAGIC... */
338
339 if ((vmh = vmh1 && vmh2)) {
340 rcinit (vmh1, vmh2);
341 pINI ();
342 SIGNAL (SIGINT, SIG_IGN);
343 SIGNAL (SIGQUIT, SIG_IGN);
344 #ifdef SIGTSTP
345 tstat = SIGNAL (SIGTSTP, SIG_IGN);
346 #endif /* SIGTSTP */
347 }
348
349 if (folder)
350 fsetup (folder);
351 else
352 setup (file);
353 readids (id);
354 display_info (id > 0 ? scansw : 0);
355
356 msh (id > 0 ? scansw : 0);
357
358 m_reset ();
359
360 done (0);
361 return 1;
362 }
363
364
365 static struct swit mshcmds[] = {
366 #define ADVCMD 0
367 { "advance", -7 },
368 #define ALICMD 1
369 { "ali", 0 },
370 #define EXPLCMD 2
371 { "burst", 0 },
372 #define COMPCMD 3
373 { "comp", 0 },
374 #define DISTCMD 4
375 { "dist", 0 },
376 #define EXITCMD 5
377 { "exit", 0 },
378 #define FOLDCMD 6
379 { "folder", 0 },
380 #define FORWCMD 7
381 { "forw", 0 },
382 #define HELPCMD 8
383 { "help", 0 },
384 #define INCMD 9
385 { "inc", 0 },
386 #define MARKCMD 10
387 { "mark", 0 },
388 #define MAILCMD 11
389 { "mhmail", 0 },
390 #define MHNCMD 12
391 { "mhn", 0 },
392 #define MSGKCMD 13
393 { "msgchk", 0 },
394 #define NEXTCMD 14
395 { "next", 0 },
396 #define PACKCMD 15
397 { "packf", 0 },
398 #define PICKCMD 16
399 { "pick", 0 },
400 #define PREVCMD 17
401 { "prev", 0 },
402 #define QUITCMD 18
403 { "quit", 0 },
404 #define FILECMD 19
405 { "refile", 0 },
406 #define REPLCMD 20
407 { "repl", 0 },
408 #define RMMCMD 21
409 { "rmm", 0 },
410 #define SCANCMD 22
411 { "scan", 0 },
412 #define SENDCMD 23
413 { "send", 0 },
414 #define SHOWCMD 24
415 { "show", 0 },
416 #define SORTCMD 25
417 { "sortm", 0 },
418 #define WHATCMD 26
419 { "whatnow", 0 },
420 #define WHOMCMD 27
421 { "whom", 0 },
422 { NULL, 0 }
423 };
424
425
426 static void
427 msh (int scansw)
428 {
429 int i;
430 register char *cp, **ap;
431 char prompt[BUFSIZ], *vec[MAXARGS];
432 struct Cmd typein;
433 register struct Cmd *cmdp;
434 static int once_only = ADVCMD;
435
436 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
437 cmdp = &typein;
438
439 for (;;) {
440 if (yp) {
441 fclose (yp);
442 yp = NULL;
443 }
444 if (vmh) {
445 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
446 rcdone ();
447 return;
448 }
449 } else {
450 check_folder (scansw);
451 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
452 putchar ('\n');
453 return;
454 }
455 }
456 cmd_name = mshcmds[i].sw;
457
458 switch (i) {
459 case QUITCMD:
460 quit ();
461 return;
462
463 case ADVCMD:
464 if (once_only == ADVCMD)
465 once_only = i = SHOWCMD;
466 else
467 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
468 cmd_name = mshcmds[i].sw;
469 /* and fall... */
470
471 case EXITCMD:
472 case EXPLCMD:
473 case FOLDCMD:
474 case FORWCMD: /* sigh */
475 case MARKCMD:
476 case NEXTCMD:
477 case PACKCMD:
478 case PICKCMD:
479 case PREVCMD:
480 case RMMCMD:
481 case SHOWCMD:
482 case SCANCMD:
483 case SORTCMD:
484 if ((cp = context_find (cmd_name))) {
485 cp = getcpy (cp);
486 ap = brkstring (cp, " ", "\n");
487 ap = copyip (ap, vec, MAXARGS);
488 } else {
489 ap = vec;
490 }
491 break;
492
493 default:
494 cp = NULL;
495 ap = vec;
496 break;
497 }
498 copyip (cmdp->args + 1, ap, MAXARGS);
499
500 m_init ();
501
502 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
503 if (cp != NULL)
504 free (cp);
505 continue;
506 }
507 modified = 0;
508 redirected = vmh || cmdp->direction != STDIO;
509
510 switch (i) {
511 case ALICMD:
512 case COMPCMD:
513 case INCMD:
514 case MAILCMD:
515 case MSGKCMD:
516 case SENDCMD:
517 case WHATCMD:
518 case WHOMCMD:
519 if (!vmh || ttyN (cmdp) != NOTOK)
520 forkcmd (vec, cmd_name);
521 break;
522
523 case DISTCMD:
524 if (!vmh || ttyN (cmdp) != NOTOK)
525 distcmd (vec);
526 break;
527
528 case EXPLCMD:
529 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
530 explcmd (vec);
531 break;
532
533 case FILECMD:
534 if (!vmh
535 || (filehak (vec) == OK ? ttyN (cmdp)
536 : winN (cmdp, DISPLAY, 1)) != NOTOK)
537 filecmd (vec);
538 break;
539
540 case FOLDCMD:
541 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
542 foldcmd (vec);
543 break;
544
545 case FORWCMD:
546 if (!vmh || ttyN (cmdp) != NOTOK)
547 forwcmd (vec);
548 break;
549
550 case HELPCMD:
551 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
552 helpcmd (vec);
553 break;
554
555 case EXITCMD:
556 case MARKCMD:
557 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
558 markcmd (vec);
559 break;
560
561 case MHNCMD:
562 if (!vmh || ttyN (cmdp) != NOTOK)
563 mhncmd (vec);
564 break;
565
566 case NEXTCMD:
567 case PREVCMD:
568 case SHOWCMD:
569 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
570 showcmd (vec);
571 break;
572
573 case PACKCMD:
574 if (!vmh
575 || (packhak (vec) == OK ? ttyN (cmdp)
576 : winN (cmdp, DISPLAY, 1)) != NOTOK)
577 packcmd (vec);
578 break;
579
580 case PICKCMD:
581 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
582 pickcmd (vec);
583 break;
584
585 case REPLCMD:
586 if (!vmh || ttyN (cmdp) != NOTOK)
587 replcmd (vec);
588 break;
589
590 case RMMCMD:
591 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
592 rmmcmd (vec);
593 break;
594
595 case SCANCMD:
596 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
597 scancmd (vec);
598 break;
599
600 case SORTCMD:
601 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
602 sortcmd (vec);
603 break;
604
605 default:
606 padios (NULL, "no dispatch for %s", cmd_name);
607 }
608
609 if (vmh) {
610 if (vmhtty != NOTOK)
611 ttyR (cmdp);
612 if (vmhpid > OK)
613 winR (cmdp);
614 }
615 else
616 fin_io (cmdp, vmh);
617 if (cp != NULL)
618 free (cp);
619 if (i == EXITCMD) {
620 quit ();
621 return;
622 }
623 }
624 }
625
626
627 void
628 fsetup (char *folder)
629 {
630 register int msgnum;
631 char *maildir;
632 struct stat st;
633
634 maildir = m_maildir (folder);
635 if (chdir (maildir) == NOTOK)
636 padios (maildir, "unable to change directory to");
637
638 /* read folder and create message structure */
639 if (!(mp = folder_read (folder)))
640 padios (NULL, "unable to read folder %s", folder);
641
642 /* check for empty folder */
643 if (mp->nummsg == 0)
644 padios (NULL, "no messages in %s", folder);
645
646 mode = m_gmprot ();
647 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
648
649 m_gMsgs (mp->hghmsg);
650
651 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
652 Msgs[msgnum].m_bboard_id = 0;
653 Msgs[msgnum].m_top = NOTOK;
654 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
655 Msgs[msgnum].m_scanl = NULL;
656 }
657
658 m_init ();
659
660 fmsh = getcpy (folder);
661
662 maxfds = OPEN_MAX / 2;
663
664 if ((maxfds -= 2) < 1)
665 maxfds = 1;
666 }
667
668
669 void
670 setup (char *file)
671 {
672 int i, msgp;
673 struct stat st;
674 if ((fp = fopen (file, "r")) == NULL)
675 padios (file, "unable to read");
676 #ifdef FIOCLEX
677 ioctl (fileno (fp), FIOCLEX, NULL);
678 #endif /* FIOCLEX */
679 if (fstat (fileno (fp), &st) != NOTOK) {
680 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
681 msgp = read_map (file, (long) st.st_size);
682 }
683 else {
684 mode = m_gmprot (), mtime = 0;
685 msgp = 0;
686 }
687
688 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
689 padios (NULL, "no messages in %s", myname ? myname : file);
690
691 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
692 padios (NULL, "unable to allocate folder storage");
693
694 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
695 padios (NULL, "unable to allocate message status storage");
696
697 mp->hghmsg = msgp;
698 mp->nummsg = msgp;
699 mp->lowmsg = 1;
700 mp->curmsg = 0;
701 mp->foldpath = getcpy (myname ? myname : file);
702 clear_folder_flags (mp);
703
704 stat (file, &st);
705 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
706 set_readonly (mp);
707
708 mp->lowoff = 1;
709 mp->hghoff = mp->hghmsg + 1;
710
711 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
712 clear_msg_flags (mp, i);
713 set_exists (mp, i);
714 }
715 m_init ();
716
717 mp->msgattrs[0] = getcpy ("unseen");
718 mp->msgattrs[1] = NULL;
719
720 m_unknown (fp); /* the MAGIC invocation */
721 if (fmsh) {
722 free (fmsh);
723 fmsh = NULL;
724 }
725 }
726
727
728 static int
729 read_map (char *file, long size)
730 {
731 register int i, msgp;
732 register struct drop *dp, *mp;
733 struct drop *rp;
734
735 if ((i = map_read (file, size, &rp, 1)) == 0)
736 return 0;
737
738 m_gMsgs (i);
739
740 msgp = 1;
741 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
742 mp = &Msgs[msgp].m_drop;
743 mp->d_id = dp->d_id;
744 mp->d_size = dp->d_size;
745 mp->d_start = dp->d_start;
746 mp->d_stop = dp->d_stop;
747 Msgs[msgp].m_scanl = NULL;
748 }
749 free ((char *) rp);
750
751 return (msgp - 1);
752 }
753
754
755 static int
756 read_file (long pos, int msgp)
757 {
758 register int i;
759 register struct drop *dp, *mp;
760 struct drop *rp;
761
762 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
763 return (msgp - 1);
764
765 m_gMsgs ((msgp - 1) + i);
766
767 for (dp = rp; i-- > 0; msgp++, dp++) {
768 mp = &Msgs[msgp].m_drop;
769 mp->d_id = 0;
770 mp->d_size = dp->d_size;
771 mp->d_start = dp->d_start;
772 mp->d_stop = dp->d_stop;
773 Msgs[msgp].m_scanl = NULL;
774 }
775 free ((char *) rp);
776
777 return (msgp - 1);
778 }
779
780
781 static void
782 m_gMsgs (int n)
783 {
784 int nmsgs;
785
786 if (Msgs == NULL) {
787 nMsgs = n + MAXFOLDER / 2;
788 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
789 if (Msgs == NULL)
790 padios (NULL, "unable to allocate Msgs structure");
791 return;
792 }
793
794 if (nMsgs >= n)
795 return;
796
797 nmsgs = nMsgs + n + MAXFOLDER / 2;
798 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
799 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
800
801 nMsgs = nmsgs;
802 }
803
804
805 FILE *
806 msh_ready (int msgnum, int full)
807 {
808 register int msgp;
809 int fd;
810 char *cp;
811
812 if (yp) {
813 fclose (yp);
814 yp = NULL;
815 }
816
817 if (fmsh) {
818 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
819 if (numfds >= maxfds)
820 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
821 if (Msgs[msgp].m_top != NOTOK) {
822 close (Msgs[msgp].m_top);
823 Msgs[msgp].m_top = NOTOK;
824 numfds--;
825 break;
826 }
827
828 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
829 padios (cp, "unable to open message");
830 Msgs[msgnum].m_top = fd;
831 numfds++;
832 }
833
834 if ((fd = dup (fd)) == NOTOK)
835 padios ("cached message", "unable to dup");
836 if ((yp = fdopen (fd, "r")) == NULL)
837 padios (NULL, "unable to fdopen cached message");
838 fseek (yp, 0L, SEEK_SET);
839 return yp;
840 }
841
842 m_eomsbr ((int (*)()) 0); /* XXX */
843 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
844 return fp;
845 }
846
847
848 static int
849 check_folder (int scansw)
850 {
851 int seqnum, i, low, hgh, msgp;
852 struct stat st;
853
854 if (fmsh) {
855 if (stat (mp->foldpath, &st) == NOTOK)
856 padios (mp->foldpath, "unable to stat");
857 if (mtime == st.st_mtime)
858 return 0;
859 mtime = st.st_mtime;
860
861 low = mp->hghmsg + 1;
862 folder_free (mp); /* free folder/message structure */
863
864 if (!(mp = folder_read (fmsh)))
865 padios (NULL, "unable to re-read folder %s", fmsh);
866
867 hgh = mp->hghmsg;
868
869 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
870 if (Msgs[msgp].m_top != NOTOK) {
871 close (Msgs[msgp].m_top);
872 Msgs[msgp].m_top = NOTOK;
873 numfds--;
874 }
875 if (Msgs[msgp].m_scanl) {
876 free (Msgs[msgp].m_scanl);
877 Msgs[msgp].m_scanl = NULL;
878 }
879 }
880
881 m_init ();
882
883 if (modified || low > hgh)
884 return 1;
885 goto check_vmh;
886 }
887 if (fstat (fileno (fp), &st) == NOTOK)
888 padios (mp->foldpath, "unable to fstat");
889 if (mtime == st.st_mtime)
890 return 0;
891 mode = (int) (st.st_mode & 0777);
892 mtime = st.st_mtime;
893
894 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
895 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
896 if (msgp >= MAXFOLDER)
897 padios (NULL, "more than %d messages in %s", MAXFOLDER,
898 mp->foldpath);
899 if (msgp <= mp->hghmsg)
900 return 0; /* XXX */
901
902 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
903 padios (NULL, "unable to allocate folder storage");
904
905 low = mp->hghmsg + 1, hgh = msgp;
906 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
907 for (i = mp->hghmsg + 1; i <= msgp; i++) {
908 set_exists(mp, i);
909 if (seqnum != -1)
910 add_sequence(mp, seqnum, i);
911 mp->nummsg++;
912 }
913 mp->hghmsg = msgp;
914 m_init ();
915
916 check_vmh: ;
917 if (vmh)
918 return 1;
919
920 advise (NULL, "new messages have arrived!\007");
921 if (scansw)
922 scanrange (low, hgh);
923
924 return 1;
925 }
926
927
928 static void
929 scanrange (int low, int hgh)
930 {
931 char buffer[BUFSIZ];
932
933 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
934 scanstring (buffer);
935 }
936
937
938 static void
939 scanstring (char *arg)
940 {
941 char *cp, **ap, *vec[MAXARGS];
942
943 /*
944 * This should be replace with a call to getarguments()
945 */
946 if ((cp = context_find (cmd_name = "scan"))) {
947 cp = getcpy (cp);
948 ap = brkstring (cp, " ", "\n");
949 ap = copyip (ap, vec, MAXARGS);
950 } else {
951 ap = vec;
952 }
953 *ap++ = arg;
954 *ap = NULL;
955 m_init ();
956 scancmd (vec);
957 if (cp != NULL)
958 free (cp);
959 }
960
961
962 void
963 readids (int id)
964 {
965 register int cur, seqnum, i=0, msgnum;
966
967 if (mp->curmsg == 0)
968 seq_setcur (mp, mp->lowmsg);
969 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
970 return;
971
972 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
973 add_sequence(mp, seqnum, msgnum);
974
975 if (id != 1) {
976 cur = mp->curmsg;
977
978 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
979 if (does_exist(mp, msgnum)) /* FIX */
980 if ((i = readid (msgnum)) > 0 && i < id) {
981 cur = msgnum + 1;
982 clear_sequence(mp, seqnum, msgnum);
983 break;
984 }
985 for (i = mp->lowmsg; i < msgnum; i++)
986 clear_sequence(mp, seqnum, i);
987
988 if (cur > mp->hghmsg)
989 cur = mp->hghmsg;
990
991 seq_setcur (mp, cur);
992 }
993
994 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
995 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
996 id - 1, i);
997 }
998
999
1000 int
1001 readid (int msgnum)
1002 {
1003 int i, state;
1004 char *bp, buf[BUFSIZ], name[NAMESZ];
1005 register FILE *zp;
1006
1007 if (Msgs[msgnum].m_bboard_id)
1008 return Msgs[msgnum].m_bboard_id;
1009
1010 zp = msh_ready (msgnum, 0);
1011 for (state = FLD;;)
1012 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1013 case FLD:
1014 case FLDEOF:
1015 case FLDPLUS:
1016 if (!mh_strcasecmp (name, BBoard_ID)) {
1017 bp = getcpy (buf);
1018 while (state == FLDPLUS) {
1019 state = m_getfld (state, name, buf, sizeof(buf), zp);
1020 bp = add (buf, bp);
1021 }
1022 i = atoi (bp);
1023 free (bp);
1024 if (i > 0)
1025 return (Msgs[msgnum].m_bboard_id = i);
1026 else
1027 continue;
1028 }
1029 while (state == FLDPLUS)
1030 state = m_getfld (state, name, buf, sizeof(buf), zp);
1031 if (state != FLDEOF)
1032 continue;
1033
1034 default:
1035 return 0;
1036 }
1037 }
1038
1039
1040 void
1041 display_info (int scansw)
1042 {
1043 int seqnum, sd;
1044
1045 interactive = isatty (fileno (stdout));
1046 if (sp == NULL) {
1047 if ((sd = dup (fileno (stdout))) == NOTOK)
1048 padios ("standard output", "unable to dup");
1049 #ifdef FIOCLEX
1050 ioctl (sd, FIOCLEX, NULL);
1051 #endif /* FIOCLEX */
1052 if ((sp = fdopen (sd, "w")) == NULL)
1053 padios ("standard output", "unable to fdopen");
1054 }
1055
1056 m_putenv ("mhfolder", mp->foldpath);
1057 if (vmh)
1058 return;
1059
1060 if (myname) {
1061 printf ("Reading ");
1062 if (SOprintf ("%s", myname))
1063 printf ("%s", myname);
1064 printf (", currently at message %d of %d\n",
1065 mp->curmsg, mp->hghmsg);
1066 }
1067 else {
1068 printf ("Reading ");
1069 if (fmsh)
1070 printf ("+%s", fmsh);
1071 else
1072 printf ("%s", mp->foldpath);
1073 printf (", currently at message %d of %d\n",
1074 mp->curmsg, mp->hghmsg);
1075 }
1076
1077 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1078 && scansw
1079 && in_sequence(mp, seqnum, mp->hghmsg))
1080 scanstring ("unseen");
1081 }
1082
1083
1084 static void
1085 write_ids (void)
1086 {
1087 int i = 0, seqnum, msgnum;
1088 char buffer[80];
1089
1090 if (pfd <= 1)
1091 return;
1092
1093 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1094 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1095 if (!in_sequence(mp, seqnum, msgnum)) {
1096 if (Msgs[msgnum].m_bboard_id == 0)
1097 readid (msgnum);
1098 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1099 break;
1100 }
1101
1102 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1103 write (pfd, buffer, sizeof(buffer));
1104 close (pfd);
1105 pfd = NOTOK;
1106 }
1107
1108
1109 static void
1110 quit (void)
1111 {
1112 int i, md, msgnum;
1113 char *cp, tmpfil[BUFSIZ];
1114 char map1[BUFSIZ], map2[BUFSIZ];
1115 struct stat st;
1116 FILE *dp;
1117
1118 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1119 if (vmh)
1120 rc2peer (RC_FIN, 0, NULL);
1121 return;
1122 }
1123
1124 if (vmh)
1125 ttyNaux (NULLCMD, "FAST");
1126 cp = NULL;
1127 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1128 advise (mp->foldpath, "unable to lock");
1129 if (vmh) {
1130 ttyR (NULLCMD);
1131 pFIN ();
1132 }
1133 return;
1134 }
1135 if (fstat (fileno (dp), &st) == NOTOK) {
1136 advise (mp->foldpath, "unable to stat");
1137 goto release;
1138 }
1139 if (mtime != st.st_mtime) {
1140 advise (NULL, "new messages have arrived, no update");
1141 goto release;
1142 }
1143 mode = (int) (st.st_mode & 0777);
1144
1145 if (mp->nummsg == 0) {
1146 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1147 if (getanswer (cp)) {
1148 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1149 close (i);
1150 else
1151 advise (mp->foldpath, "error zero'ing");
1152 unlink (map_name (mp->foldpath));/* XXX */
1153 }
1154 goto release;
1155 }
1156
1157 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1158 if (!getanswer (cp))
1159 goto release;
1160 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1161 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1162 advise (tmpfil, "unable to open");
1163 goto release;
1164 }
1165
1166 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1167 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1168 mbx_close (tmpfil, md);
1169 unlink (tmpfil);
1170 unlink (map_name (tmpfil));
1171 goto release;
1172 }
1173 mbx_close (tmpfil, md);
1174
1175 if (rename (tmpfil, mp->foldpath) == NOTOK)
1176 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1177 else {
1178 strncpy (map1, map_name (tmpfil), sizeof(map1));
1179 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1180
1181 if (rename (map1, map2) == NOTOK) {
1182 admonish (map2, "unable to rename %s to", map1);
1183 unlink (map1);
1184 unlink (map2);
1185 }
1186 }
1187
1188 release: ;
1189 if (cp)
1190 free (cp);
1191 lkfclose (dp, mp->foldpath);
1192 if (vmh) {
1193 ttyR (NULLCMD);
1194 pFIN ();
1195 }
1196 }
1197
1198
1199 static int
1200 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1201 {
1202 int i;
1203 char *cp;
1204 static char buffer[BUFSIZ];
1205
1206 told_to_quit = 0;
1207 for (;;) {
1208 interrupted = 0;
1209 if (interactive) {
1210 printf ("%s", prompt);
1211 fflush (stdout);
1212 }
1213 for (cp = buffer; (i = getchar ()) != '\n';) {
1214 if (interrupted && !told_to_quit) {
1215 buffer[0] = '\0';
1216 putchar ('\n');
1217 break;
1218 }
1219 if (told_to_quit || i == EOF) {
1220 if (ppid > 0)
1221 #ifdef SIGEMT
1222 kill (ppid, SIGEMT);
1223 #else
1224 kill (ppid, SIGTERM);
1225 #endif
1226 return EOF;
1227 }
1228 if (cp < &buffer[sizeof buffer - 2])
1229 *cp++ = i;
1230 }
1231 *cp = 0;
1232
1233 if (buffer[0] == 0)
1234 continue;
1235 if (buffer[0] == '?') {
1236 printf ("commands:\n");
1237 print_sw (ALL, sw, "", stdout);
1238 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1239 invo_name);
1240 continue;
1241 }
1242
1243 if (parse (buffer, cmdp) == NOTOK)
1244 continue;
1245
1246 switch (i = smatch (cmdp->args[0], sw)) {
1247 case AMBIGSW:
1248 ambigsw (cmdp->args[0], sw);
1249 continue;
1250 case UNKWNSW:
1251 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1252 cmdp->args[0]);
1253 continue;
1254 default:
1255 return i;
1256 }
1257 }
1258 }
1259
1260
1261 static int
1262 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1263 {
1264 int i;
1265 struct record rcs, *rc;
1266
1267 rc = &rcs;
1268 initrc (rc);
1269
1270 for (;;)
1271 switch (peer2rc (rc)) {
1272 case RC_QRY:
1273 pQRY (rc->rc_data, scansw);
1274 break;
1275
1276 case RC_CMD:
1277 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1278 return i;
1279 break;
1280
1281 case RC_FIN:
1282 if (ppid > 0)
1283 #ifdef SIGEMT
1284 kill (ppid, SIGEMT);
1285 #else
1286 kill (ppid, SIGTERM);
1287 #endif
1288 return EOF;
1289
1290 case RC_XXX:
1291 padios (NULL, "%s", rc->rc_data);
1292
1293 default:
1294 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1295 done (1);
1296 }
1297 }
1298
1299
1300 static int
1301 parse (char *buffer, struct Cmd *cmdp)
1302 {
1303 int argp = 0;
1304 unsigned char c, *cp;
1305 char *pp;
1306
1307 cmdp->line[0] = 0;
1308 pp = cmdp->args[argp++] = cmdp->line;
1309 cmdp->redirect = NULL;
1310 cmdp->direction = STDIO;
1311 cmdp->stream = NULL;
1312
1313 for (cp = buffer; (c = *cp); cp++) {
1314 if (!isspace (c))
1315 break;
1316 }
1317 if (c == '\0') {
1318 if (vmh)
1319 fmt2peer (RC_EOF, "null command");
1320 return NOTOK;
1321 }
1322
1323 while ((c = *cp++)) {
1324 if (isspace (c)) {
1325 while (isspace (c))
1326 c = *cp++;
1327 if (c == 0)
1328 break;
1329 *pp++ = 0;
1330 cmdp->args[argp++] = pp;
1331 *pp = 0;
1332 }
1333
1334 switch (c) {
1335 case '"':
1336 for (;;) {
1337 switch (c = *cp++) {
1338 case 0:
1339 padvise (NULL, "unmatched \"");
1340 return NOTOK;
1341 case '"':
1342 break;
1343 case QUOTE:
1344 if ((c = *cp++) == 0)
1345 goto no_quoting;
1346 default:
1347 *pp++ = c;
1348 continue;
1349 }
1350 break;
1351 }
1352 continue;
1353
1354 case QUOTE:
1355 if ((c = *cp++) == 0) {
1356 no_quoting: ;
1357 padvise (NULL, "the newline character can not be quoted");
1358 return NOTOK;
1359 }
1360
1361 default: ;
1362 *pp++ = c;
1363 continue;
1364
1365 case '>':
1366 case '|':
1367 if (pp == cmdp->line) {
1368 padvise (NULL, "invalid null command");
1369 return NOTOK;
1370 }
1371 if (*cmdp->args[argp - 1] == 0)
1372 argp--;
1373 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1374 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1375 cmdp->direction = APPIO;
1376 cp++;
1377 }
1378 cmdp->redirect = pp + 1;/* sigh */
1379 for (; (c = *cp); cp++)
1380 if (!isspace (c))
1381 break;
1382 if (c == 0) {
1383 padvise (NULL, cmdp->direction != PIPIO
1384 ? "missing name for redirect"
1385 : "invalid null command");
1386 return NOTOK;
1387 }
1388 strcpy (cmdp->redirect, cp);
1389 if (cmdp->direction != PIPIO) {
1390 for (; *cp; cp++)
1391 if (isspace (*cp)) {
1392 padvise (NULL, "bad name for redirect");
1393 return NOTOK;
1394 }
1395 if (expand (cmdp->redirect) == NOTOK)
1396 return NOTOK;
1397 }
1398 break;
1399 }
1400 break;
1401 }
1402
1403 *pp++ = 0;
1404 cmdp->args[argp] = NULL;
1405
1406 return OK;
1407 }
1408
1409
1410 int
1411 expand (char *redirect)
1412 {
1413 char *cp, *pp;
1414 char path[BUFSIZ];
1415 struct passwd *pw;
1416
1417 if (*redirect != '~')
1418 return OK;
1419
1420 if ((cp = strchr(pp = redirect + 1, '/')))
1421 *cp++ = 0;
1422 if (*pp == 0)
1423 pp = mypath;
1424 else
1425 if ((pw = getpwnam (pp)))
1426 pp = pw->pw_dir;
1427 else {
1428 padvise (NULL, "unknown user: %s", pp);
1429 return NOTOK;
1430 }
1431
1432 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1433 strcpy (redirect, path);
1434 return OK;
1435 }
1436
1437
1438 static int
1439 init_io (struct Cmd *cmdp, int vio)
1440 {
1441 int io, result;
1442
1443 io = vmh;
1444
1445 vmh = vio;
1446 result = initaux_io (cmdp);
1447 vmh = io;
1448
1449 return result;
1450 }
1451
1452
1453 static int
1454 initaux_io (struct Cmd *cmdp)
1455 {
1456 char *mode;
1457
1458 switch (cmdp->direction) {
1459 case STDIO:
1460 return OK;
1461
1462 case CRTIO:
1463 case APPIO:
1464 mode = cmdp->direction == CRTIO ? "write" : "append";
1465 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1466 padvise (cmdp->redirect, "unable to %s ", mode);
1467 cmdp->direction = STDIO;
1468 return NOTOK;
1469 }
1470 break;
1471
1472 case PIPIO:
1473 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1474 padvise (cmdp->redirect, "unable to pipe");
1475 cmdp->direction = STDIO;
1476 return NOTOK;
1477 }
1478 SIGNAL (SIGPIPE, pipeser);
1479 broken_pipe = 0;
1480 break;
1481
1482 default:
1483 padios (NULL, "unknown redirection for command");
1484 }
1485
1486 fflush (stdout);
1487 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1488 padios ("standard output", "unable to dup2");
1489 clearerr (stdout);
1490
1491 return OK;
1492 }
1493
1494
1495 static void
1496 fin_io (struct Cmd *cmdp, int vio)
1497 {
1498 int io;
1499
1500 io = vmh;
1501 vmh = vio;
1502 finaux_io (cmdp);
1503 vmh = io;
1504 }
1505
1506
1507 static void
1508 finaux_io (struct Cmd *cmdp)
1509 {
1510 switch (cmdp->direction) {
1511 case STDIO:
1512 return;
1513
1514 case CRTIO:
1515 case APPIO:
1516 fflush (stdout);
1517 close (fileno (stdout));
1518 if (ferror (stdout))
1519 padvise (NULL, "problems writing %s", cmdp->redirect);
1520 fclose (cmdp->stream);
1521 break;
1522
1523 case PIPIO:
1524 fflush (stdout);
1525 close (fileno (stdout));
1526 pclose (cmdp->stream);
1527 SIGNAL (SIGPIPE, SIG_DFL);
1528 break;
1529
1530 default:
1531 padios (NULL, "unknown redirection for command");
1532 }
1533
1534 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1535 padios ("standard output", "unable to dup2");
1536 clearerr (stdout);
1537
1538 cmdp->direction = STDIO;
1539 }
1540
1541
1542 static void
1543 m_init (void)
1544 {
1545 int msgnum;
1546
1547 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1548 unset_selected (mp, msgnum);
1549 mp->lowsel = mp->hghsel = mp->numsel = 0;
1550 }
1551
1552
1553 void
1554 m_reset (void)
1555 {
1556 write_ids ();
1557 folder_free (mp); /* free folder/message structure */
1558 myname = NULL;
1559 }
1560
1561
1562 void
1563 seq_setcur (struct msgs *mp, int msgnum)
1564 {
1565 if (mp->curmsg == msgnum)
1566 return;
1567
1568 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1569 free (Msgs[mp->curmsg].m_scanl);
1570 Msgs[mp->curmsg].m_scanl = NULL;
1571 }
1572 if (Msgs[msgnum].m_scanl) {
1573 free (Msgs[msgnum].m_scanl);
1574 Msgs[msgnum].m_scanl = NULL;
1575 }
1576
1577 mp->curmsg = msgnum;
1578 }
1579
1580
1581
1582 static void
1583 intrser (int i)
1584 {
1585 discard (stdout);
1586 interrupted++;
1587 }
1588
1589
1590 static void
1591 pipeser (int i)
1592 {
1593 if (broken_pipe++ == 0)
1594 fprintf (stderr, "broken pipe\n");
1595 told_to_quit++;
1596 interrupted++;
1597 }
1598
1599
1600 static void
1601 quitser (int i)
1602 {
1603 told_to_quit++;
1604 interrupted++;
1605 }
1606
1607
1608 static void
1609 alrmser (int i)
1610 {
1611 longjmp (peerenv, DONE);
1612 }
1613
1614
1615 static int
1616 pINI (void)
1617 {
1618 int i, vrsn;
1619 unsigned char *bp;
1620 struct record rcs, *rc;
1621
1622 rc = &rcs;
1623 initrc (rc);
1624
1625 switch (peer2rc (rc)) {
1626 case RC_INI:
1627 bp = rc->rc_data;
1628 while (isspace (*bp))
1629 bp++;
1630 if (sscanf (bp, "%d", &vrsn) != 1) {
1631 bad_init: ;
1632 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1633 done (1);
1634 }
1635 if (vrsn != RC_VRSN) {
1636 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1637 done (1);
1638 }
1639
1640 while (*bp && !isspace (*bp))
1641 bp++;
1642 while (isspace (*bp))
1643 bp++;
1644 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1645 goto bad_init;
1646 if (numwins > NWIN)
1647 numwins = NWIN;
1648
1649 for (i = 1; i <= numwins; i++) {
1650 while (*bp && !isspace (*bp))
1651 bp++;
1652 while (isspace (*bp))
1653 bp++;
1654 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1655 goto bad_init;
1656 }
1657 rc2peer (RC_ACK, 0, NULL);
1658 return OK;
1659
1660 case RC_XXX:
1661 padios (NULL, "%s", rc->rc_data);
1662
1663 default:
1664 fmt2peer (RC_ERR, "pINI protocol screw-up");
1665 done (1); /* NOTREACHED */
1666 }
1667
1668 return 1; /* dead code to satisfy the compiler */
1669 }
1670
1671
1672 static int
1673 pQRY (char *str, int scansw)
1674 {
1675 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1676 return NOTOK;
1677
1678 rc2peer (RC_EOF, 0, NULL);
1679 return OK;
1680 }
1681
1682
1683 static int
1684 pQRY1 (int scansw)
1685 {
1686 int oldhgh;
1687 static int lastlow = 0,
1688 lastcur = 0,
1689 lasthgh = 0,
1690 lastnum = 0;
1691
1692 oldhgh = mp->hghmsg;
1693 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1694 switch (winX (STATUS)) {
1695 case NOTOK:
1696 return NOTOK;
1697
1698 case OK:
1699 printf ("new messages have arrived!");
1700 fflush (stdout);
1701 fflush (stderr);
1702 _exit (0); /* NOTREACHED */
1703
1704 default:
1705 lastlow = lastcur = lasthgh = lastnum = 0;
1706 break;
1707 }
1708
1709 switch (winX (DISPLAY)) {
1710 case NOTOK:
1711 return NOTOK;
1712
1713 case OK:
1714 scanrange (oldhgh + 1, mp->hghmsg);
1715 fflush (stdout);
1716 fflush (stderr);
1717 _exit (0); /* NOTREACHED */
1718
1719 default:
1720 break;
1721 }
1722 return OK;
1723 }
1724
1725 if (gap)
1726 switch (winX (STATUS)) {
1727 case NOTOK:
1728 return NOTOK;
1729
1730 case OK:
1731 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1732 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
1733 readid (mp->lowmsg));
1734 fflush (stdout);
1735 fflush (stderr);
1736 _exit (0); /* NOTREACHED */
1737
1738 default:
1739 gap = 0;
1740 return OK;
1741 }
1742
1743 if (mp->lowmsg != lastlow
1744 || mp->curmsg != lastcur
1745 || mp->hghmsg != lasthgh
1746 || mp->nummsg != lastnum)
1747 switch (winX (STATUS)) {
1748 case NOTOK:
1749 return NOTOK;
1750
1751 case OK:
1752 foldcmd (NULL);
1753 fflush (stdout);
1754 fflush (stderr);
1755 _exit (0); /* NOTREACHED */
1756
1757 default:
1758 lastlow = mp->lowmsg;
1759 lastcur = mp->curmsg;
1760 lasthgh = mp->hghmsg;
1761 lastnum = mp->nummsg;
1762 return OK;
1763 }
1764
1765 return OK;
1766 }
1767
1768
1769 static int
1770 pQRY2 (void)
1771 {
1772 int i, j, k, msgnum, n;
1773 static int cur = 0,
1774 num = 0,
1775 lo = 0,
1776 hi = 0;
1777
1778 if (mp->nummsg == 0 && mp->nummsg != num)
1779 switch (winX (SCAN)) {
1780 case NOTOK:
1781 return NOTOK;
1782
1783 case OK:
1784 printf ("empty!");
1785 fflush (stdout);
1786 fflush (stderr);
1787 _exit (0); /* NOTREACHED */
1788
1789 default:
1790 num = mp->nummsg;
1791 return OK;
1792 }
1793 num = mp->nummsg;
1794
1795 i = 0;
1796 j = (k = windows[SCAN]) / 2;
1797 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
1798 if (does_exist (mp, msgnum))
1799 i++;
1800 if (i-- > 0) {
1801 if (topcur)
1802 k = i >= k ? 1 : k - i;
1803 else
1804 k -= i > j ? j : i;
1805 }
1806
1807 i = j = 0;
1808 n = 1;
1809 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
1810 if (does_exist (mp, msgnum)) {
1811 i = msgnum;
1812 if (j == 0)
1813 j = msgnum;
1814 if (n++ >= k)
1815 break;
1816 }
1817 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
1818 if (does_exist (mp, msgnum)) {
1819 if (i == 0)
1820 i = msgnum;
1821 j = msgnum;
1822 if (n++ >= windows[SCAN])
1823 break;
1824 }
1825 if (!topcur
1826 && lo > 0
1827 && hi > 0
1828 && does_exist (mp, lo)
1829 && does_exist (mp, hi)
1830 && (lo < mp->curmsg
1831 || (lo == mp->curmsg && lo == mp->lowmsg))
1832 && (mp->curmsg < hi
1833 || (hi == mp->curmsg && hi == mp->hghmsg))
1834 && hi - lo == j - i)
1835 i = lo, j = hi;
1836
1837 if (mp->curmsg != cur || modified)
1838 switch (winN (NULLCMD, SCAN, 0)) {
1839 case NOTOK:
1840 return NOTOK;
1841
1842 case OK:
1843 return OK;
1844
1845 default:
1846 scanrange (lo = i, hi = j);
1847 cur = mp->curmsg;
1848 winR (NULLCMD);
1849 return OK;
1850 }
1851
1852 return OK;
1853 }
1854
1855
1856 static int
1857 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
1858 {
1859 int i;
1860
1861 if (*str == '?')
1862 switch (winX (DISPLAY)) {
1863 case NOTOK:
1864 return NOTOK;
1865
1866 case OK:
1867 printf ("commands:\n");
1868 print_sw (ALL, sw, "", stdout);
1869 printf ("type ``quit'' to leave %s\n", invo_name);
1870 fflush (stdout);
1871 fflush (stderr);
1872 _exit (0); /* NOTREACHED */
1873
1874 default:
1875 rc2peer (RC_EOF, 0, NULL);
1876 return NOTOK;
1877 }
1878
1879 if (parse (str, cmdp) == NOTOK)
1880 return NOTOK;
1881
1882 switch (i = smatch (cmdp->args[0], sw)) {
1883 case AMBIGSW:
1884 switch (winX (DISPLAY)) {
1885 case NOTOK:
1886 return NOTOK;
1887
1888 case OK:
1889 ambigsw (cmdp->args[0], sw);
1890 fflush (stdout);
1891 fflush (stderr);
1892 _exit (0); /* NOTREACHED */
1893
1894 default:
1895 rc2peer (RC_EOF, 0, NULL);
1896 return NOTOK;
1897 }
1898
1899 case UNKWNSW:
1900 fmt2peer (RC_ERR,
1901 "say what: ``%s'' -- type ? (or help) for help",
1902 cmdp->args[0]);
1903 return NOTOK;
1904
1905 default:
1906 return i;
1907 }
1908 }
1909
1910
1911 static int
1912 pFIN (void)
1913 {
1914 int status;
1915
1916 switch (setjmp (peerenv)) {
1917 case OK:
1918 SIGNAL (SIGALRM, alrmser);
1919 alarm (ALARM);
1920
1921 status = peerwait ();
1922
1923 alarm (0);
1924 return status;
1925
1926 default:
1927 return NOTOK;
1928 }
1929 }
1930
1931
1932 static int
1933 peerwait (void)
1934 {
1935 struct record rcs, *rc;
1936
1937 rc = &rcs;
1938 initrc (rc);
1939
1940 switch (peer2rc (rc)) {
1941 case RC_QRY:
1942 case RC_CMD:
1943 rc2peer (RC_FIN, 0, NULL);
1944 return OK;
1945
1946 case RC_XXX:
1947 advise (NULL, "%s", rc->rc_data);
1948 return NOTOK;
1949
1950 default:
1951 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
1952 return NOTOK;
1953 }
1954 }
1955
1956
1957 static int
1958 ttyNaux (struct Cmd *cmdp, char *s)
1959 {
1960 struct record rcs, *rc;
1961
1962 rc = &rcs;
1963 initrc (rc);
1964
1965 if (cmdp && init_io (cmdp, vmh) == NOTOK)
1966 return NOTOK;
1967
1968 /* XXX: fseek() too tricky for our own good */
1969 if (!fmsh)
1970 fseek (fp, 0L, SEEK_SET);
1971
1972 vmhtty = NOTOK;
1973 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
1974 case RC_ACK:
1975 vmhtty = OK; /* fall */
1976 case RC_ERR:
1977 break;
1978
1979 case RC_XXX:
1980 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
1981
1982 default:
1983 fmt2peer (RC_ERR, "pTTY protocol screw-up");
1984 done (1); /* NOTREACHED */
1985 }
1986
1987 #ifdef SIGTSTP
1988 SIGNAL (SIGTSTP, tstat);
1989 #endif
1990 return vmhtty;
1991 }
1992
1993
1994 static int
1995 ttyR (struct Cmd *cmdp)
1996 {
1997 struct record rcs, *rc;
1998
1999 rc = &rcs;
2000
2001 #ifdef SIGTSTP
2002 SIGNAL (SIGTSTP, SIG_IGN);
2003 #endif
2004
2005 if (vmhtty != OK)
2006 return NOTOK;
2007
2008 initrc (rc);
2009
2010 if (cmdp)
2011 fin_io (cmdp, 0);
2012
2013 vmhtty = NOTOK;
2014 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2015 case RC_ACK:
2016 rc2peer (RC_EOF, 0, NULL);
2017 return OK;
2018
2019 case RC_XXX:
2020 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2021
2022 default:
2023 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2024 done (1); /* NOTREACHED */
2025 }
2026
2027 return 1; /* dead code to satisfy compiler */
2028 }
2029
2030
2031 static int
2032 winN (struct Cmd *cmdp, int n, int eof)
2033 {
2034 int i, pd[2];
2035 char buffer[BUFSIZ];
2036 struct record rcs, *rc;
2037
2038 rc = &rcs;
2039 if (vmhpid == NOTOK)
2040 return OK;
2041
2042 initrc (rc);
2043
2044 /* XXX: fseek() too tricky for our own good */
2045 if (!fmsh)
2046 fseek (fp, 0L, SEEK_SET);
2047
2048 vmhpid = OK;
2049
2050 snprintf (buffer, sizeof(buffer), "%d", n);
2051 switch (str2rc (RC_WIN, buffer, rc)) {
2052 case RC_ACK:
2053 break;
2054
2055 case RC_ERR:
2056 return NOTOK;
2057
2058 case RC_XXX:
2059 padios (NULL, "%s", rc->rc_data);
2060
2061 default:
2062 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2063 done (1);
2064 }
2065
2066 if (pipe (pd) == NOTOK) {
2067 err2peer (RC_ERR, "pipe", "unable to");
2068 return NOTOK;
2069 }
2070
2071 switch (vmhpid = fork()) {
2072 case NOTOK:
2073 err2peer (RC_ERR, "fork", "unable to");
2074 close (pd[0]);
2075 close (pd[1]);
2076 return NOTOK;
2077
2078 case OK:
2079 close (pd[1]);
2080 SIGNAL (SIGPIPE, SIG_IGN);
2081 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2082 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2083 case RC_ACK:
2084 break;
2085
2086 case RC_ERR:
2087 _exit (1);
2088
2089 case RC_XXX:
2090 advise (NULL, "%s", rc->rc_data);
2091 _exit (2);
2092
2093 default:
2094 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2095 _exit (2);
2096 }
2097 if (i == OK)
2098 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2099 case RC_ACK:
2100 if (eof)
2101 rc2peer (RC_EOF, 0, NULL);
2102 i = 0;
2103 break;
2104
2105 case RC_XXX:
2106 advise (NULL, "%s", rc->rc_data);
2107 i = 2;
2108 break;
2109
2110 default:
2111 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2112 i = 2;
2113 break;
2114 }
2115 if (i == NOTOK)
2116 err2peer (RC_ERR, "pipe", "error reading from");
2117 close (pd[0]);
2118 _exit (i != NOTOK ? i : 1);
2119
2120 default:
2121 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2122 padios ("standard input", "unable to dup");
2123 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2124 padios ("standard output", "unable to dup");
2125 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2126 padios ("diagnostic output", "unable to dup");
2127
2128 close (0);
2129 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2130 dup2 (i, fileno (stdin));
2131 close (i);
2132 }
2133
2134 fflush (stdout);
2135 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2136 padios ("standard output", "unable to dup2");
2137 clearerr (stdout);
2138
2139 fflush (stderr);
2140 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2141 padios ("diagnostic output", "unable to dup2");
2142 clearerr (stderr);
2143
2144 if (cmdp && init_io (cmdp, 0) == NOTOK)
2145 return NOTOK;
2146 pstat = SIGNAL (SIGPIPE, pipeser);
2147 broken_pipe = 1;
2148
2149 close (pd[0]);
2150 close (pd[1]);
2151
2152 return vmhpid;
2153 }
2154 }
2155
2156
2157 static int
2158 winR (struct Cmd *cmdp)
2159 {
2160 int status;
2161
2162 if (vmhpid <= OK)
2163 return NOTOK;
2164
2165 if (cmdp)
2166 fin_io (cmdp, 0);
2167
2168 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2169 padios ("standard input", "unable to dup2");
2170 clearerr (stdin);
2171 close (vmhfd0);
2172
2173 fflush (stdout);
2174 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2175 padios ("standard output", "unable to dup2");
2176 clearerr (stdout);
2177 close (vmhfd1);
2178
2179 fflush (stderr);
2180 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2181 padios ("diagnostic output", "unable to dup2");
2182 clearerr (stderr);
2183 close (vmhfd2);
2184
2185 SIGNAL (SIGPIPE, pstat);
2186
2187 if ((status = pidwait (vmhpid, OK)) == 2)
2188 done (1);
2189
2190 vmhpid = OK;
2191 return (status == 0 ? OK : NOTOK);
2192 }
2193
2194
2195 static int
2196 winX (int n)
2197 {
2198 int i, pid, pd[2];
2199 char buffer[BUFSIZ];
2200 struct record rcs, *rc;
2201
2202 rc = &rcs;
2203 initrc (rc);
2204
2205 /* XXX: fseek() too tricky for our own good */
2206 if (!fmsh)
2207 fseek (fp, 0L, SEEK_SET);
2208
2209 snprintf (buffer, sizeof(buffer), "%d", n);
2210 switch (str2rc (RC_WIN, buffer, rc)) {
2211 case RC_ACK:
2212 break;
2213
2214 case RC_ERR:
2215 return NOTOK;
2216
2217 case RC_XXX:
2218 padios (NULL, "%s", rc->rc_data);
2219
2220 default:
2221 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2222 done (1);
2223 }
2224
2225 if (pipe (pd) == NOTOK) {
2226 err2peer (RC_ERR, "pipe", "unable to");
2227 return NOTOK;
2228 }
2229
2230 switch (pid = fork ()) {
2231 case NOTOK:
2232 err2peer (RC_ERR, "fork", "unable to");
2233 close (pd[0]);
2234 close (pd[1]);
2235 return NOTOK;
2236
2237 case OK:
2238 close (fileno (stdin));
2239 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2240 dup2 (i, fileno (stdin));
2241 close (i);
2242 }
2243 dup2 (pd[1], fileno (stdout));
2244 dup2 (pd[1], fileno (stderr));
2245 close (pd[0]);
2246 close (pd[1]);
2247 vmhpid = NOTOK;
2248 return OK;
2249
2250 default:
2251 close (pd[1]);
2252 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2253 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2254 case RC_ACK:
2255 break;
2256
2257 case RC_ERR:
2258 close (pd[0]);
2259 pidwait (pid, OK);
2260 return NOTOK;
2261
2262 case RC_XXX:
2263 padios (NULL, "%s", rc->rc_data);
2264
2265 default:
2266 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2267 done (1);
2268 }
2269 if (i == OK)
2270 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2271 case RC_ACK:
2272 break;
2273
2274 case RC_XXX:
2275 padios (NULL, "%s", rc->rc_data);
2276
2277 default:
2278 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2279 done (1);
2280 }
2281 if (i == NOTOK)
2282 err2peer (RC_ERR, "pipe", "error reading from");
2283
2284 close (pd[0]);
2285 pidwait (pid, OK);
2286 return (i != NOTOK ? pid : NOTOK);
2287 }
2288 }
2289
2290
2291 void
2292 padios (char *what, char *fmt, ...)
2293 {
2294 va_list ap;
2295
2296 va_start(ap, fmt);
2297 if (vmh) {
2298 verr2peer (RC_FIN, what, fmt, ap);
2299 rcdone ();
2300 } else {
2301 advertise (what, NULL, fmt, ap);
2302 }
2303 va_end(ap);
2304
2305 done (1);
2306 }
2307
2308
2309 void
2310 padvise (char *what, char *fmt, ...)
2311 {
2312 va_list ap;
2313
2314 va_start(ap, fmt);
2315 if (vmh) {
2316 verr2peer (RC_ERR, what, fmt, ap);
2317 } else {
2318 advertise (what, NULL, fmt, ap);
2319 }
2320 va_end(ap);
2321 }