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