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