]> diplodocus.org Git - nmh/blob - uip/msh.c
Ken noted that "make check" can be run before installation because the only program...
[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 <h/m_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 NMH_UNUSED (full);
809 register int msgp;
810 int fd;
811 char *cp;
812
813 if (yp) {
814 fclose (yp);
815 yp = NULL;
816 }
817
818 if (fmsh) {
819 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
820 if (numfds >= maxfds)
821 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
822 if (Msgs[msgp].m_top != NOTOK) {
823 close (Msgs[msgp].m_top);
824 Msgs[msgp].m_top = NOTOK;
825 numfds--;
826 break;
827 }
828
829 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
830 padios (cp, "unable to open message");
831 Msgs[msgnum].m_top = fd;
832 numfds++;
833 }
834
835 if ((fd = dup (fd)) == NOTOK)
836 padios ("cached message", "unable to dup");
837 if ((yp = fdopen (fd, "r")) == NULL)
838 padios (NULL, "unable to fdopen cached message");
839 fseek (yp, 0L, SEEK_SET);
840 return yp;
841 }
842
843 m_eomsbr ((int (*)()) 0); /* XXX */
844 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
845 return fp;
846 }
847
848
849 static int
850 check_folder (int scansw)
851 {
852 int seqnum, i, low, hgh, msgp;
853 struct stat st;
854
855 if (fmsh) {
856 if (stat (mp->foldpath, &st) == NOTOK)
857 padios (mp->foldpath, "unable to stat");
858 if (mtime == st.st_mtime)
859 return 0;
860 mtime = st.st_mtime;
861
862 low = mp->hghmsg + 1;
863 folder_free (mp); /* free folder/message structure */
864
865 if (!(mp = folder_read (fmsh)))
866 padios (NULL, "unable to re-read folder %s", fmsh);
867
868 hgh = mp->hghmsg;
869
870 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
871 if (Msgs[msgp].m_top != NOTOK) {
872 close (Msgs[msgp].m_top);
873 Msgs[msgp].m_top = NOTOK;
874 numfds--;
875 }
876 if (Msgs[msgp].m_scanl) {
877 free (Msgs[msgp].m_scanl);
878 Msgs[msgp].m_scanl = NULL;
879 }
880 }
881
882 m_init ();
883
884 if (modified || low > hgh)
885 return 1;
886 goto check_vmh;
887 }
888 if (fstat (fileno (fp), &st) == NOTOK)
889 padios (mp->foldpath, "unable to fstat");
890 if (mtime == st.st_mtime)
891 return 0;
892 mode = (int) (st.st_mode & 0777);
893 mtime = st.st_mtime;
894
895 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
896 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
897 if (msgp >= MAXFOLDER)
898 padios (NULL, "more than %d messages in %s", MAXFOLDER,
899 mp->foldpath);
900 if (msgp <= mp->hghmsg)
901 return 0; /* XXX */
902
903 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
904 padios (NULL, "unable to allocate folder storage");
905
906 low = mp->hghmsg + 1, hgh = msgp;
907 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
908 for (i = mp->hghmsg + 1; i <= msgp; i++) {
909 set_exists(mp, i);
910 if (seqnum != -1)
911 add_sequence(mp, seqnum, i);
912 mp->nummsg++;
913 }
914 mp->hghmsg = msgp;
915 m_init ();
916
917 check_vmh: ;
918 if (vmh)
919 return 1;
920
921 advise (NULL, "new messages have arrived!\007");
922 if (scansw)
923 scanrange (low, hgh);
924
925 return 1;
926 }
927
928
929 static void
930 scanrange (int low, int hgh)
931 {
932 char buffer[BUFSIZ];
933
934 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
935 scanstring (buffer);
936 }
937
938
939 static void
940 scanstring (char *arg)
941 {
942 char *cp, **ap, *vec[MAXARGS];
943
944 /*
945 * This should be replace with a call to getarguments()
946 */
947 if ((cp = context_find (cmd_name = "scan"))) {
948 cp = getcpy (cp);
949 ap = brkstring (cp, " ", "\n");
950 ap = copyip (ap, vec, MAXARGS);
951 } else {
952 ap = vec;
953 }
954 *ap++ = arg;
955 *ap = NULL;
956 m_init ();
957 scancmd (vec);
958 if (cp != NULL)
959 free (cp);
960 }
961
962
963 void
964 readids (int id)
965 {
966 register int cur, seqnum, i=0, msgnum;
967
968 if (mp->curmsg == 0)
969 seq_setcur (mp, mp->lowmsg);
970 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
971 return;
972
973 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
974 add_sequence(mp, seqnum, msgnum);
975
976 if (id != 1) {
977 cur = mp->curmsg;
978
979 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
980 if (does_exist(mp, msgnum)) /* FIX */
981 if ((i = readid (msgnum)) > 0 && i < id) {
982 cur = msgnum + 1;
983 clear_sequence(mp, seqnum, msgnum);
984 break;
985 }
986 for (i = mp->lowmsg; i < msgnum; i++)
987 clear_sequence(mp, seqnum, i);
988
989 if (cur > mp->hghmsg)
990 cur = mp->hghmsg;
991
992 seq_setcur (mp, cur);
993 }
994
995 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
996 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
997 id - 1, i);
998 }
999
1000
1001 int
1002 readid (int msgnum)
1003 {
1004 int i, state;
1005 char *bp, buf[BUFSIZ], name[NAMESZ];
1006 register FILE *zp;
1007
1008 if (Msgs[msgnum].m_bboard_id)
1009 return Msgs[msgnum].m_bboard_id;
1010
1011 zp = msh_ready (msgnum, 0);
1012 for (state = FLD;;)
1013 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1014 case FLD:
1015 case FLDEOF:
1016 case FLDPLUS:
1017 if (!mh_strcasecmp (name, BBoard_ID)) {
1018 bp = getcpy (buf);
1019 while (state == FLDPLUS) {
1020 state = m_getfld (state, name, buf, sizeof(buf), zp);
1021 bp = add (buf, bp);
1022 }
1023 i = atoi (bp);
1024 free (bp);
1025 if (i > 0)
1026 return (Msgs[msgnum].m_bboard_id = i);
1027 else
1028 continue;
1029 }
1030 while (state == FLDPLUS)
1031 state = m_getfld (state, name, buf, sizeof(buf), zp);
1032 if (state != FLDEOF)
1033 continue;
1034
1035 default:
1036 return 0;
1037 }
1038 }
1039
1040
1041 void
1042 display_info (int scansw)
1043 {
1044 int seqnum, sd;
1045
1046 interactive = isatty (fileno (stdout));
1047 if (sp == NULL) {
1048 if ((sd = dup (fileno (stdout))) == NOTOK)
1049 padios ("standard output", "unable to dup");
1050 #ifdef FIOCLEX
1051 ioctl (sd, FIOCLEX, NULL);
1052 #endif /* FIOCLEX */
1053 if ((sp = fdopen (sd, "w")) == NULL)
1054 padios ("standard output", "unable to fdopen");
1055 }
1056
1057 m_putenv ("mhfolder", mp->foldpath);
1058 if (vmh)
1059 return;
1060
1061 if (myname) {
1062 printf ("Reading ");
1063 if (SOprintf ("%s", myname))
1064 printf ("%s", myname);
1065 printf (", currently at message %d of %d\n",
1066 mp->curmsg, mp->hghmsg);
1067 }
1068 else {
1069 printf ("Reading ");
1070 if (fmsh)
1071 printf ("+%s", fmsh);
1072 else
1073 printf ("%s", mp->foldpath);
1074 printf (", currently at message %d of %d\n",
1075 mp->curmsg, mp->hghmsg);
1076 }
1077
1078 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1079 && scansw
1080 && in_sequence(mp, seqnum, mp->hghmsg))
1081 scanstring ("unseen");
1082 }
1083
1084
1085 static void
1086 write_ids (void)
1087 {
1088 int i = 0, seqnum, msgnum;
1089 char buffer[80];
1090
1091 if (pfd <= 1)
1092 return;
1093
1094 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1095 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1096 if (!in_sequence(mp, seqnum, msgnum)) {
1097 if (Msgs[msgnum].m_bboard_id == 0)
1098 readid (msgnum);
1099 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1100 break;
1101 }
1102
1103 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1104 write (pfd, buffer, sizeof(buffer));
1105 close (pfd);
1106 pfd = NOTOK;
1107 }
1108
1109
1110 static void
1111 quit (void)
1112 {
1113 int i, md, msgnum;
1114 char *cp, tmpfil[BUFSIZ];
1115 char map1[BUFSIZ], map2[BUFSIZ];
1116 struct stat st;
1117 FILE *dp;
1118
1119 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1120 if (vmh)
1121 rc2peer (RC_FIN, 0, NULL);
1122 return;
1123 }
1124
1125 if (vmh)
1126 ttyNaux (NULLCMD, "FAST");
1127 cp = NULL;
1128 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1129 advise (mp->foldpath, "unable to lock");
1130 if (vmh) {
1131 ttyR (NULLCMD);
1132 pFIN ();
1133 }
1134 return;
1135 }
1136 if (fstat (fileno (dp), &st) == NOTOK) {
1137 advise (mp->foldpath, "unable to stat");
1138 goto release;
1139 }
1140 if (mtime != st.st_mtime) {
1141 advise (NULL, "new messages have arrived, no update");
1142 goto release;
1143 }
1144 mode = (int) (st.st_mode & 0777);
1145
1146 if (mp->nummsg == 0) {
1147 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1148 if (getanswer (cp)) {
1149 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1150 close (i);
1151 else
1152 advise (mp->foldpath, "error zero'ing");
1153 unlink (map_name (mp->foldpath));/* XXX */
1154 }
1155 goto release;
1156 }
1157
1158 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1159 if (!getanswer (cp))
1160 goto release;
1161 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1162 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1163 advise (tmpfil, "unable to open");
1164 goto release;
1165 }
1166
1167 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1168 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1169 mbx_close (tmpfil, md);
1170 unlink (tmpfil);
1171 unlink (map_name (tmpfil));
1172 goto release;
1173 }
1174 mbx_close (tmpfil, md);
1175
1176 if (rename (tmpfil, mp->foldpath) == NOTOK)
1177 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1178 else {
1179 strncpy (map1, map_name (tmpfil), sizeof(map1));
1180 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1181
1182 if (rename (map1, map2) == NOTOK) {
1183 admonish (map2, "unable to rename %s to", map1);
1184 unlink (map1);
1185 unlink (map2);
1186 }
1187 }
1188
1189 release: ;
1190 if (cp)
1191 free (cp);
1192 lkfclose (dp, mp->foldpath);
1193 if (vmh) {
1194 ttyR (NULLCMD);
1195 pFIN ();
1196 }
1197 }
1198
1199
1200 static int
1201 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1202 {
1203 int i;
1204 char *cp;
1205 static char buffer[BUFSIZ];
1206
1207 told_to_quit = 0;
1208 for (;;) {
1209 interrupted = 0;
1210 if (interactive) {
1211 printf ("%s", prompt);
1212 fflush (stdout);
1213 }
1214 for (cp = buffer; (i = getchar ()) != '\n';) {
1215 if (interrupted && !told_to_quit) {
1216 buffer[0] = '\0';
1217 putchar ('\n');
1218 break;
1219 }
1220 if (told_to_quit || i == EOF) {
1221 if (ppid > 0)
1222 #ifdef SIGEMT
1223 kill (ppid, SIGEMT);
1224 #else
1225 kill (ppid, SIGTERM);
1226 #endif
1227 return EOF;
1228 }
1229 if (cp < &buffer[sizeof buffer - 2])
1230 *cp++ = i;
1231 }
1232 *cp = 0;
1233
1234 if (buffer[0] == 0)
1235 continue;
1236 if (buffer[0] == '?') {
1237 printf ("commands:\n");
1238 print_sw (ALL, sw, "", stdout);
1239 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1240 invo_name);
1241 continue;
1242 }
1243
1244 if (parse (buffer, cmdp) == NOTOK)
1245 continue;
1246
1247 switch (i = smatch (cmdp->args[0], sw)) {
1248 case AMBIGSW:
1249 ambigsw (cmdp->args[0], sw);
1250 continue;
1251 case UNKWNSW:
1252 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1253 cmdp->args[0]);
1254 continue;
1255 default:
1256 return i;
1257 }
1258 }
1259 }
1260
1261
1262 static int
1263 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1264 {
1265 int i;
1266 struct record rcs, *rc;
1267
1268 rc = &rcs;
1269 initrc (rc);
1270
1271 for (;;)
1272 switch (peer2rc (rc)) {
1273 case RC_QRY:
1274 pQRY (rc->rc_data, scansw);
1275 break;
1276
1277 case RC_CMD:
1278 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1279 return i;
1280 break;
1281
1282 case RC_FIN:
1283 if (ppid > 0)
1284 #ifdef SIGEMT
1285 kill (ppid, SIGEMT);
1286 #else
1287 kill (ppid, SIGTERM);
1288 #endif
1289 return EOF;
1290
1291 case RC_XXX:
1292 padios (NULL, "%s", rc->rc_data);
1293
1294 default:
1295 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1296 done (1);
1297 }
1298 }
1299
1300
1301 static int
1302 parse (char *buffer, struct Cmd *cmdp)
1303 {
1304 int argp = 0;
1305 unsigned char c, *cp;
1306 char *pp;
1307
1308 cmdp->line[0] = 0;
1309 pp = cmdp->args[argp++] = cmdp->line;
1310 cmdp->redirect = NULL;
1311 cmdp->direction = STDIO;
1312 cmdp->stream = NULL;
1313
1314 for (cp = buffer; (c = *cp); cp++) {
1315 if (!isspace (c))
1316 break;
1317 }
1318 if (c == '\0') {
1319 if (vmh)
1320 fmt2peer (RC_EOF, "null command");
1321 return NOTOK;
1322 }
1323
1324 while ((c = *cp++)) {
1325 if (isspace (c)) {
1326 while (isspace (c))
1327 c = *cp++;
1328 if (c == 0)
1329 break;
1330 *pp++ = 0;
1331 cmdp->args[argp++] = pp;
1332 *pp = 0;
1333 }
1334
1335 switch (c) {
1336 case '"':
1337 for (;;) {
1338 switch (c = *cp++) {
1339 case 0:
1340 padvise (NULL, "unmatched \"");
1341 return NOTOK;
1342 case '"':
1343 break;
1344 case QUOTE:
1345 if ((c = *cp++) == 0)
1346 goto no_quoting;
1347 default:
1348 *pp++ = c;
1349 continue;
1350 }
1351 break;
1352 }
1353 continue;
1354
1355 case QUOTE:
1356 if ((c = *cp++) == 0) {
1357 no_quoting: ;
1358 padvise (NULL, "the newline character can not be quoted");
1359 return NOTOK;
1360 }
1361
1362 default: ;
1363 *pp++ = c;
1364 continue;
1365
1366 case '>':
1367 case '|':
1368 if (pp == cmdp->line) {
1369 padvise (NULL, "invalid null command");
1370 return NOTOK;
1371 }
1372 if (*cmdp->args[argp - 1] == 0)
1373 argp--;
1374 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1375 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1376 cmdp->direction = APPIO;
1377 cp++;
1378 }
1379 cmdp->redirect = pp + 1;/* sigh */
1380 for (; (c = *cp); cp++)
1381 if (!isspace (c))
1382 break;
1383 if (c == 0) {
1384 padvise (NULL, cmdp->direction != PIPIO
1385 ? "missing name for redirect"
1386 : "invalid null command");
1387 return NOTOK;
1388 }
1389 strcpy (cmdp->redirect, cp);
1390 if (cmdp->direction != PIPIO) {
1391 for (; *cp; cp++)
1392 if (isspace (*cp)) {
1393 padvise (NULL, "bad name for redirect");
1394 return NOTOK;
1395 }
1396 if (expand (cmdp->redirect) == NOTOK)
1397 return NOTOK;
1398 }
1399 break;
1400 }
1401 break;
1402 }
1403
1404 *pp++ = 0;
1405 cmdp->args[argp] = NULL;
1406
1407 return OK;
1408 }
1409
1410
1411 int
1412 expand (char *redirect)
1413 {
1414 char *cp, *pp;
1415 char path[BUFSIZ];
1416 struct passwd *pw;
1417
1418 if (*redirect != '~')
1419 return OK;
1420
1421 if ((cp = strchr(pp = redirect + 1, '/')))
1422 *cp++ = 0;
1423 if (*pp == 0)
1424 pp = mypath;
1425 else
1426 if ((pw = getpwnam (pp)))
1427 pp = pw->pw_dir;
1428 else {
1429 padvise (NULL, "unknown user: %s", pp);
1430 return NOTOK;
1431 }
1432
1433 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1434 strcpy (redirect, path);
1435 return OK;
1436 }
1437
1438
1439 static int
1440 init_io (struct Cmd *cmdp, int vio)
1441 {
1442 int io, result;
1443
1444 io = vmh;
1445
1446 vmh = vio;
1447 result = initaux_io (cmdp);
1448 vmh = io;
1449
1450 return result;
1451 }
1452
1453
1454 static int
1455 initaux_io (struct Cmd *cmdp)
1456 {
1457 char *mode;
1458
1459 switch (cmdp->direction) {
1460 case STDIO:
1461 return OK;
1462
1463 case CRTIO:
1464 case APPIO:
1465 mode = cmdp->direction == CRTIO ? "write" : "append";
1466 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1467 padvise (cmdp->redirect, "unable to %s ", mode);
1468 cmdp->direction = STDIO;
1469 return NOTOK;
1470 }
1471 break;
1472
1473 case PIPIO:
1474 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1475 padvise (cmdp->redirect, "unable to pipe");
1476 cmdp->direction = STDIO;
1477 return NOTOK;
1478 }
1479 SIGNAL (SIGPIPE, pipeser);
1480 broken_pipe = 0;
1481 break;
1482
1483 default:
1484 padios (NULL, "unknown redirection for command");
1485 }
1486
1487 fflush (stdout);
1488 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1489 padios ("standard output", "unable to dup2");
1490 clearerr (stdout);
1491
1492 return OK;
1493 }
1494
1495
1496 static void
1497 fin_io (struct Cmd *cmdp, int vio)
1498 {
1499 int io;
1500
1501 io = vmh;
1502 vmh = vio;
1503 finaux_io (cmdp);
1504 vmh = io;
1505 }
1506
1507
1508 static void
1509 finaux_io (struct Cmd *cmdp)
1510 {
1511 switch (cmdp->direction) {
1512 case STDIO:
1513 return;
1514
1515 case CRTIO:
1516 case APPIO:
1517 fflush (stdout);
1518 close (fileno (stdout));
1519 if (ferror (stdout))
1520 padvise (NULL, "problems writing %s", cmdp->redirect);
1521 fclose (cmdp->stream);
1522 break;
1523
1524 case PIPIO:
1525 fflush (stdout);
1526 close (fileno (stdout));
1527 pclose (cmdp->stream);
1528 SIGNAL (SIGPIPE, SIG_DFL);
1529 break;
1530
1531 default:
1532 padios (NULL, "unknown redirection for command");
1533 }
1534
1535 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1536 padios ("standard output", "unable to dup2");
1537 clearerr (stdout);
1538
1539 cmdp->direction = STDIO;
1540 }
1541
1542
1543 static void
1544 m_init (void)
1545 {
1546 int msgnum;
1547
1548 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1549 unset_selected (mp, msgnum);
1550 mp->lowsel = mp->hghsel = mp->numsel = 0;
1551 }
1552
1553
1554 void
1555 m_reset (void)
1556 {
1557 write_ids ();
1558 folder_free (mp); /* free folder/message structure */
1559 myname = NULL;
1560 }
1561
1562
1563 void
1564 seq_setcur (struct msgs *mp, int msgnum)
1565 {
1566 if (mp->curmsg == msgnum)
1567 return;
1568
1569 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1570 free (Msgs[mp->curmsg].m_scanl);
1571 Msgs[mp->curmsg].m_scanl = NULL;
1572 }
1573 if (Msgs[msgnum].m_scanl) {
1574 free (Msgs[msgnum].m_scanl);
1575 Msgs[msgnum].m_scanl = NULL;
1576 }
1577
1578 mp->curmsg = msgnum;
1579 }
1580
1581
1582
1583 static void
1584 intrser (int i)
1585 {
1586 NMH_UNUSED (i);
1587 discard (stdout);
1588 interrupted++;
1589 }
1590
1591
1592 static void
1593 pipeser (int i)
1594 {
1595 NMH_UNUSED (i);
1596 if (broken_pipe++ == 0)
1597 fprintf (stderr, "broken pipe\n");
1598 told_to_quit++;
1599 interrupted++;
1600 }
1601
1602
1603 static void
1604 quitser (int i)
1605 {
1606 NMH_UNUSED (i);
1607 told_to_quit++;
1608 interrupted++;
1609 }
1610
1611
1612 static void
1613 alrmser (int i)
1614 {
1615 NMH_UNUSED (i);
1616 longjmp (peerenv, DONE);
1617 }
1618
1619
1620 static int
1621 pINI (void)
1622 {
1623 int i, vrsn;
1624 unsigned char *bp;
1625 struct record rcs, *rc;
1626
1627 rc = &rcs;
1628 initrc (rc);
1629
1630 switch (peer2rc (rc)) {
1631 case RC_INI:
1632 bp = rc->rc_data;
1633 while (isspace (*bp))
1634 bp++;
1635 if (sscanf (bp, "%d", &vrsn) != 1) {
1636 bad_init: ;
1637 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1638 done (1);
1639 }
1640 if (vrsn != RC_VRSN) {
1641 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1642 done (1);
1643 }
1644
1645 while (*bp && !isspace (*bp))
1646 bp++;
1647 while (isspace (*bp))
1648 bp++;
1649 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1650 goto bad_init;
1651 if (numwins > NWIN)
1652 numwins = NWIN;
1653
1654 for (i = 1; i <= numwins; i++) {
1655 while (*bp && !isspace (*bp))
1656 bp++;
1657 while (isspace (*bp))
1658 bp++;
1659 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1660 goto bad_init;
1661 }
1662 rc2peer (RC_ACK, 0, NULL);
1663 return OK;
1664
1665 case RC_XXX:
1666 padios (NULL, "%s", rc->rc_data);
1667
1668 default:
1669 fmt2peer (RC_ERR, "pINI protocol screw-up");
1670 done (1); /* NOTREACHED */
1671 }
1672
1673 return 1; /* dead code to satisfy the compiler */
1674 }
1675
1676
1677 static int
1678 pQRY (char *str, int scansw)
1679 {
1680 NMH_UNUSED (str);
1681 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1682 return NOTOK;
1683
1684 rc2peer (RC_EOF, 0, NULL);
1685 return OK;
1686 }
1687
1688
1689 static int
1690 pQRY1 (int scansw)
1691 {
1692 int oldhgh;
1693 static int lastlow = 0,
1694 lastcur = 0,
1695 lasthgh = 0,
1696 lastnum = 0;
1697
1698 oldhgh = mp->hghmsg;
1699 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1700 switch (winX (STATUS)) {
1701 case NOTOK:
1702 return NOTOK;
1703
1704 case OK:
1705 printf ("new messages have arrived!");
1706 fflush (stdout);
1707 fflush (stderr);
1708 _exit (0); /* NOTREACHED */
1709
1710 default:
1711 lastlow = lastcur = lasthgh = lastnum = 0;
1712 break;
1713 }
1714
1715 switch (winX (DISPLAY)) {
1716 case NOTOK:
1717 return NOTOK;
1718
1719 case OK:
1720 scanrange (oldhgh + 1, mp->hghmsg);
1721 fflush (stdout);
1722 fflush (stderr);
1723 _exit (0); /* NOTREACHED */
1724
1725 default:
1726 break;
1727 }
1728 return OK;
1729 }
1730
1731 if (gap)
1732 switch (winX (STATUS)) {
1733 case NOTOK:
1734 return NOTOK;
1735
1736 case OK:
1737 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1738 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
1739 readid (mp->lowmsg));
1740 fflush (stdout);
1741 fflush (stderr);
1742 _exit (0); /* NOTREACHED */
1743
1744 default:
1745 gap = 0;
1746 return OK;
1747 }
1748
1749 if (mp->lowmsg != lastlow
1750 || mp->curmsg != lastcur
1751 || mp->hghmsg != lasthgh
1752 || mp->nummsg != lastnum)
1753 switch (winX (STATUS)) {
1754 case NOTOK:
1755 return NOTOK;
1756
1757 case OK:
1758 foldcmd (NULL);
1759 fflush (stdout);
1760 fflush (stderr);
1761 _exit (0); /* NOTREACHED */
1762
1763 default:
1764 lastlow = mp->lowmsg;
1765 lastcur = mp->curmsg;
1766 lasthgh = mp->hghmsg;
1767 lastnum = mp->nummsg;
1768 return OK;
1769 }
1770
1771 return OK;
1772 }
1773
1774
1775 static int
1776 pQRY2 (void)
1777 {
1778 int i, j, k, msgnum, n;
1779 static int cur = 0,
1780 num = 0,
1781 lo = 0,
1782 hi = 0;
1783
1784 if (mp->nummsg == 0 && mp->nummsg != num)
1785 switch (winX (SCAN)) {
1786 case NOTOK:
1787 return NOTOK;
1788
1789 case OK:
1790 printf ("empty!");
1791 fflush (stdout);
1792 fflush (stderr);
1793 _exit (0); /* NOTREACHED */
1794
1795 default:
1796 num = mp->nummsg;
1797 return OK;
1798 }
1799 num = mp->nummsg;
1800
1801 i = 0;
1802 j = (k = windows[SCAN]) / 2;
1803 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
1804 if (does_exist (mp, msgnum))
1805 i++;
1806 if (i-- > 0) {
1807 if (topcur)
1808 k = i >= k ? 1 : k - i;
1809 else
1810 k -= i > j ? j : i;
1811 }
1812
1813 i = j = 0;
1814 n = 1;
1815 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
1816 if (does_exist (mp, msgnum)) {
1817 i = msgnum;
1818 if (j == 0)
1819 j = msgnum;
1820 if (n++ >= k)
1821 break;
1822 }
1823 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
1824 if (does_exist (mp, msgnum)) {
1825 if (i == 0)
1826 i = msgnum;
1827 j = msgnum;
1828 if (n++ >= windows[SCAN])
1829 break;
1830 }
1831 if (!topcur
1832 && lo > 0
1833 && hi > 0
1834 && does_exist (mp, lo)
1835 && does_exist (mp, hi)
1836 && (lo < mp->curmsg
1837 || (lo == mp->curmsg && lo == mp->lowmsg))
1838 && (mp->curmsg < hi
1839 || (hi == mp->curmsg && hi == mp->hghmsg))
1840 && hi - lo == j - i)
1841 i = lo, j = hi;
1842
1843 if (mp->curmsg != cur || modified)
1844 switch (winN (NULLCMD, SCAN, 0)) {
1845 case NOTOK:
1846 return NOTOK;
1847
1848 case OK:
1849 return OK;
1850
1851 default:
1852 scanrange (lo = i, hi = j);
1853 cur = mp->curmsg;
1854 winR (NULLCMD);
1855 return OK;
1856 }
1857
1858 return OK;
1859 }
1860
1861
1862 static int
1863 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
1864 {
1865 int i;
1866
1867 if (*str == '?')
1868 switch (winX (DISPLAY)) {
1869 case NOTOK:
1870 return NOTOK;
1871
1872 case OK:
1873 printf ("commands:\n");
1874 print_sw (ALL, sw, "", stdout);
1875 printf ("type ``quit'' to leave %s\n", invo_name);
1876 fflush (stdout);
1877 fflush (stderr);
1878 _exit (0); /* NOTREACHED */
1879
1880 default:
1881 rc2peer (RC_EOF, 0, NULL);
1882 return NOTOK;
1883 }
1884
1885 if (parse (str, cmdp) == NOTOK)
1886 return NOTOK;
1887
1888 switch (i = smatch (cmdp->args[0], sw)) {
1889 case AMBIGSW:
1890 switch (winX (DISPLAY)) {
1891 case NOTOK:
1892 return NOTOK;
1893
1894 case OK:
1895 ambigsw (cmdp->args[0], sw);
1896 fflush (stdout);
1897 fflush (stderr);
1898 _exit (0); /* NOTREACHED */
1899
1900 default:
1901 rc2peer (RC_EOF, 0, NULL);
1902 return NOTOK;
1903 }
1904
1905 case UNKWNSW:
1906 fmt2peer (RC_ERR,
1907 "say what: ``%s'' -- type ? (or help) for help",
1908 cmdp->args[0]);
1909 return NOTOK;
1910
1911 default:
1912 return i;
1913 }
1914 }
1915
1916
1917 static int
1918 pFIN (void)
1919 {
1920 int status;
1921
1922 switch (m_setjmp (peerenv)) {
1923 case OK:
1924 SIGNAL (SIGALRM, alrmser);
1925 alarm (ALARM);
1926
1927 status = peerwait ();
1928
1929 alarm (0);
1930 return status;
1931
1932 default:
1933 return NOTOK;
1934 }
1935 }
1936
1937
1938 static int
1939 peerwait (void)
1940 {
1941 struct record rcs, *rc;
1942
1943 rc = &rcs;
1944 initrc (rc);
1945
1946 switch (peer2rc (rc)) {
1947 case RC_QRY:
1948 case RC_CMD:
1949 rc2peer (RC_FIN, 0, NULL);
1950 return OK;
1951
1952 case RC_XXX:
1953 advise (NULL, "%s", rc->rc_data);
1954 return NOTOK;
1955
1956 default:
1957 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
1958 return NOTOK;
1959 }
1960 }
1961
1962
1963 static int
1964 ttyNaux (struct Cmd *cmdp, char *s)
1965 {
1966 struct record rcs, *rc;
1967
1968 rc = &rcs;
1969 initrc (rc);
1970
1971 if (cmdp && init_io (cmdp, vmh) == NOTOK)
1972 return NOTOK;
1973
1974 /* XXX: fseek() too tricky for our own good */
1975 if (!fmsh)
1976 fseek (fp, 0L, SEEK_SET);
1977
1978 vmhtty = NOTOK;
1979 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
1980 case RC_ACK:
1981 vmhtty = OK; /* fall */
1982 case RC_ERR:
1983 break;
1984
1985 case RC_XXX:
1986 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
1987
1988 default:
1989 fmt2peer (RC_ERR, "pTTY protocol screw-up");
1990 done (1); /* NOTREACHED */
1991 }
1992
1993 #ifdef SIGTSTP
1994 SIGNAL (SIGTSTP, tstat);
1995 #endif
1996 return vmhtty;
1997 }
1998
1999
2000 static int
2001 ttyR (struct Cmd *cmdp)
2002 {
2003 struct record rcs, *rc;
2004
2005 rc = &rcs;
2006
2007 #ifdef SIGTSTP
2008 SIGNAL (SIGTSTP, SIG_IGN);
2009 #endif
2010
2011 if (vmhtty != OK)
2012 return NOTOK;
2013
2014 initrc (rc);
2015
2016 if (cmdp)
2017 fin_io (cmdp, 0);
2018
2019 vmhtty = NOTOK;
2020 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2021 case RC_ACK:
2022 rc2peer (RC_EOF, 0, NULL);
2023 return OK;
2024
2025 case RC_XXX:
2026 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2027
2028 default:
2029 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2030 done (1); /* NOTREACHED */
2031 }
2032
2033 return 1; /* dead code to satisfy compiler */
2034 }
2035
2036
2037 static int
2038 winN (struct Cmd *cmdp, int n, int eof)
2039 {
2040 int i, pd[2];
2041 char buffer[BUFSIZ];
2042 struct record rcs, *rc;
2043
2044 rc = &rcs;
2045 if (vmhpid == NOTOK)
2046 return OK;
2047
2048 initrc (rc);
2049
2050 /* XXX: fseek() too tricky for our own good */
2051 if (!fmsh)
2052 fseek (fp, 0L, SEEK_SET);
2053
2054 vmhpid = OK;
2055
2056 snprintf (buffer, sizeof(buffer), "%d", n);
2057 switch (str2rc (RC_WIN, buffer, rc)) {
2058 case RC_ACK:
2059 break;
2060
2061 case RC_ERR:
2062 return NOTOK;
2063
2064 case RC_XXX:
2065 padios (NULL, "%s", rc->rc_data);
2066
2067 default:
2068 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2069 done (1);
2070 }
2071
2072 if (pipe (pd) == NOTOK) {
2073 err2peer (RC_ERR, "pipe", "unable to");
2074 return NOTOK;
2075 }
2076
2077 switch (vmhpid = fork()) {
2078 case NOTOK:
2079 err2peer (RC_ERR, "fork", "unable to");
2080 close (pd[0]);
2081 close (pd[1]);
2082 return NOTOK;
2083
2084 case OK:
2085 close (pd[1]);
2086 SIGNAL (SIGPIPE, SIG_IGN);
2087 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2088 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2089 case RC_ACK:
2090 break;
2091
2092 case RC_ERR:
2093 _exit (1);
2094
2095 case RC_XXX:
2096 advise (NULL, "%s", rc->rc_data);
2097 _exit (2);
2098
2099 default:
2100 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2101 _exit (2);
2102 }
2103 if (i == OK)
2104 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2105 case RC_ACK:
2106 if (eof)
2107 rc2peer (RC_EOF, 0, NULL);
2108 i = 0;
2109 break;
2110
2111 case RC_XXX:
2112 advise (NULL, "%s", rc->rc_data);
2113 i = 2;
2114 break;
2115
2116 default:
2117 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2118 i = 2;
2119 break;
2120 }
2121 if (i == NOTOK)
2122 err2peer (RC_ERR, "pipe", "error reading from");
2123 close (pd[0]);
2124 _exit (i != NOTOK ? i : 1);
2125
2126 default:
2127 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2128 padios ("standard input", "unable to dup");
2129 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2130 padios ("standard output", "unable to dup");
2131 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2132 padios ("diagnostic output", "unable to dup");
2133
2134 close (0);
2135 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2136 dup2 (i, fileno (stdin));
2137 close (i);
2138 }
2139
2140 fflush (stdout);
2141 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2142 padios ("standard output", "unable to dup2");
2143 clearerr (stdout);
2144
2145 fflush (stderr);
2146 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2147 padios ("diagnostic output", "unable to dup2");
2148 clearerr (stderr);
2149
2150 if (cmdp && init_io (cmdp, 0) == NOTOK)
2151 return NOTOK;
2152 pstat = SIGNAL (SIGPIPE, pipeser);
2153 broken_pipe = 1;
2154
2155 close (pd[0]);
2156 close (pd[1]);
2157
2158 return vmhpid;
2159 }
2160 }
2161
2162
2163 static int
2164 winR (struct Cmd *cmdp)
2165 {
2166 int status;
2167
2168 if (vmhpid <= OK)
2169 return NOTOK;
2170
2171 if (cmdp)
2172 fin_io (cmdp, 0);
2173
2174 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2175 padios ("standard input", "unable to dup2");
2176 clearerr (stdin);
2177 close (vmhfd0);
2178
2179 fflush (stdout);
2180 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2181 padios ("standard output", "unable to dup2");
2182 clearerr (stdout);
2183 close (vmhfd1);
2184
2185 fflush (stderr);
2186 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2187 padios ("diagnostic output", "unable to dup2");
2188 clearerr (stderr);
2189 close (vmhfd2);
2190
2191 SIGNAL (SIGPIPE, pstat);
2192
2193 if ((status = pidwait (vmhpid, OK)) == 2)
2194 done (1);
2195
2196 vmhpid = OK;
2197 return (status == 0 ? OK : NOTOK);
2198 }
2199
2200
2201 static int
2202 winX (int n)
2203 {
2204 int i, pid, pd[2];
2205 char buffer[BUFSIZ];
2206 struct record rcs, *rc;
2207
2208 rc = &rcs;
2209 initrc (rc);
2210
2211 /* XXX: fseek() too tricky for our own good */
2212 if (!fmsh)
2213 fseek (fp, 0L, SEEK_SET);
2214
2215 snprintf (buffer, sizeof(buffer), "%d", n);
2216 switch (str2rc (RC_WIN, buffer, rc)) {
2217 case RC_ACK:
2218 break;
2219
2220 case RC_ERR:
2221 return NOTOK;
2222
2223 case RC_XXX:
2224 padios (NULL, "%s", rc->rc_data);
2225
2226 default:
2227 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2228 done (1);
2229 }
2230
2231 if (pipe (pd) == NOTOK) {
2232 err2peer (RC_ERR, "pipe", "unable to");
2233 return NOTOK;
2234 }
2235
2236 switch (pid = fork ()) {
2237 case NOTOK:
2238 err2peer (RC_ERR, "fork", "unable to");
2239 close (pd[0]);
2240 close (pd[1]);
2241 return NOTOK;
2242
2243 case OK:
2244 close (fileno (stdin));
2245 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2246 dup2 (i, fileno (stdin));
2247 close (i);
2248 }
2249 dup2 (pd[1], fileno (stdout));
2250 dup2 (pd[1], fileno (stderr));
2251 close (pd[0]);
2252 close (pd[1]);
2253 vmhpid = NOTOK;
2254 return OK;
2255
2256 default:
2257 close (pd[1]);
2258 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2259 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2260 case RC_ACK:
2261 break;
2262
2263 case RC_ERR:
2264 close (pd[0]);
2265 pidwait (pid, OK);
2266 return NOTOK;
2267
2268 case RC_XXX:
2269 padios (NULL, "%s", rc->rc_data);
2270
2271 default:
2272 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2273 done (1);
2274 }
2275 if (i == OK)
2276 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2277 case RC_ACK:
2278 break;
2279
2280 case RC_XXX:
2281 padios (NULL, "%s", rc->rc_data);
2282
2283 default:
2284 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2285 done (1);
2286 }
2287 if (i == NOTOK)
2288 err2peer (RC_ERR, "pipe", "error reading from");
2289
2290 close (pd[0]);
2291 pidwait (pid, OK);
2292 return (i != NOTOK ? pid : NOTOK);
2293 }
2294 }
2295
2296
2297 void
2298 padios (char *what, char *fmt, ...)
2299 {
2300 va_list ap;
2301
2302 va_start(ap, fmt);
2303 if (vmh) {
2304 verr2peer (RC_FIN, what, fmt, ap);
2305 rcdone ();
2306 } else {
2307 advertise (what, NULL, fmt, ap);
2308 }
2309 va_end(ap);
2310
2311 done (1);
2312 }
2313
2314
2315 void
2316 padvise (char *what, char *fmt, ...)
2317 {
2318 va_list ap;
2319
2320 va_start(ap, fmt);
2321 if (vmh) {
2322 verr2peer (RC_ERR, what, fmt, ap);
2323 } else {
2324 advertise (what, NULL, fmt, ap);
2325 }
2326 va_end(ap);
2327 }