]> diplodocus.org Git - nmh/blob - uip/msh.c
Fix this man page for the New World Order.
[nmh] / uip / msh.c
1
2 /*
3 * msh.c -- The nmh shell
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 /*
11 * TODO:
12 * Keep more status information in maildrop map
13 */
14
15 #include <h/mh.h>
16 #include <fcntl.h>
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
21 #include <h/tws.h>
22 #include <h/mts.h>
23 #include <h/utils.h>
24
25 #include <termios.h>
26
27 #include <pwd.h>
28 #include <h/m_setjmp.h>
29 #include <signal.h>
30 #include <h/msh.h>
31 #include <h/vmhsbr.h>
32
33 #define QUOTE '\\' /* sigh */
34
35 static struct swit switches[] = {
36 #define IDSW 0
37 { "idstart number", -7 }, /* interface from bbc */
38 #define FDSW 1
39 { "idstop number", -6 }, /* .. */
40 #define QDSW 2
41 { "idquit number", -6 }, /* .. */
42 #define NMSW 3
43 { "idname BBoard", -6 }, /* .. */
44 #define PRMPTSW 4
45 { "prompt string", 0 },
46 #define SCANSW 5
47 { "scan", 0 },
48 #define NSCANSW 6
49 { "noscan", 0 },
50 #define READSW 7
51 { "vmhread fd", -7 },
52 #define WRITESW 8
53 { "vmhwrite fd", -8 },
54 #define PREADSW 9
55 { "popread fd", -7 },
56 #define PWRITSW 10
57 { "popwrite fd", -8 },
58 #define TCURSW 11
59 { "topcur", 0 },
60 #define NTCURSW 12
61 { "notopcur", 0 },
62 #define VERSIONSW 13
63 { "version", 0 },
64 #define HELPSW 14
65 { "help", 0 },
66 { NULL, 0 }
67 };
68
69 static int mbx_style = MMDF_FORMAT;
70
71 /*
72 * FOLDER
73 */
74 char*fmsh = NULL; /* folder instead of file */
75 int modified; /* command modified folder */
76 struct msgs *mp; /* used a lot */
77 static int nMsgs = 0;
78 struct Msg *Msgs = NULL; /* Msgs[0] not used */
79 static FILE *fp; /* input file */
80 static FILE *yp = NULL; /* temporary file */
81 static int mode; /* mode of file */
82 static int numfds = 0; /* number of files cached */
83 static int maxfds = 0; /* number of files cached to be cached */
84 static time_t mtime = (time_t) 0; /* mtime of file */
85
86 /*
87 * VMH
88 */
89 #define ALARM ((unsigned int) 10)
90 #define ttyN(c) ttyNaux ((c), NULL)
91
92 static int vmh = 0;
93
94 static int vmhpid = OK;
95 static int vmhfd0;
96 static int vmhfd1;
97 static int vmhfd2;
98
99 static int vmhtty = NOTOK;
100
101 #define SCAN 1
102 #define STATUS 2
103 #define DISPLAY 3
104 #define NWIN DISPLAY
105
106 static int topcur = 0;
107
108 static int numwins = 0;
109 static int windows[NWIN + 1];
110
111 static jmp_buf peerenv;
112
113 /*
114 * PARENT
115 */
116 static int pfd = NOTOK; /* fd parent is reading from */
117 static int ppid = 0; /* pid of parent */
118
119 /*
120 * COMMAND
121 */
122 int interactive; /* running from a /dev/tty */
123 int redirected; /* re-directing output */
124 FILE *sp = NULL; /* original stdout */
125
126 char *cmd_name; /* command being run */
127 char myfilter[BUFSIZ]; /* path to mhl.forward */
128
129 static char *myprompt = "(%s) ";/* prompting string */
130
131 /*
132 * BBOARDS
133 */
134 static int gap; /* gap in BBoard-ID:s */
135 static char *myname = NULL; /* BBoard name */
136 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
137
138 /*
139 * SIGNALS
140 */
141 SIGNAL_HANDLER istat; /* original SIGINT */
142 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
143 SIGNAL_HANDLER qstat; /* original SIGQUIT */
144
145 #ifdef SIGTSTP
146 SIGNAL_HANDLER tstat; /* original SIGTSTP */
147 #endif
148
149 int interrupted; /* SIGINT detected */
150 int broken_pipe; /* SIGPIPE detected */
151 int told_to_quit; /* SIGQUIT detected */
152
153 /*
154 * prototypes
155 */
156 void fsetup (char *);
157 void setup (char *);
158 FILE *msh_ready (int, int);
159 void readids (int);
160 int readid (int);
161 void display_info (int);
162 int expand (char *);
163 void m_reset (void);
164 void seq_setcur (struct msgs *, int);
165 void padios (char *, char *, ...);
166 void padvise (char *, char *, ...);
167
168
169 /*
170 * static prototypes
171 */
172 static void msh (int);
173 static int read_map (char *, long);
174 static int read_file (long, int);
175
176 static void m_gMsgs (int);
177 FILE *msh_ready (int, int);
178 static int check_folder (int);
179 static void scanrange (int, int);
180 static void scanstring (char *);
181 static void write_ids (void);
182 static void quit (void);
183 static int getargs (char *, struct swit *, struct Cmd *);
184 static int getcmds (struct swit *, struct Cmd *, int);
185 static int parse (char *, struct Cmd *);
186 static int init_io (struct Cmd *, int);
187 static int initaux_io (struct Cmd *);
188 static void fin_io (struct Cmd *, int);
189 static void finaux_io (struct Cmd *);
190 static void m_init (void);
191 static void intrser (int);
192 static void pipeser (int);
193 static void quitser (int);
194 static void alrmser (int);
195 static int pINI (void);
196 static int pQRY (char *, int);
197 static int pQRY1 (int);
198 static int pQRY2 (void);
199 static int pCMD (char *, struct swit *, struct Cmd *);
200 static int pFIN (void);
201 static int peerwait (void);
202 static int ttyNaux (struct Cmd *, char *);
203 static int ttyR (struct Cmd *);
204 static int winN (struct Cmd *, int, int);
205 static int winR (struct Cmd *);
206 static int winX (int);
207
208
209 int
210 main (int argc, char **argv)
211 {
212 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
213 char *cp, *file = NULL, *folder = NULL;
214 char **argp, **arguments, buf[BUFSIZ];
215
216 #ifdef LOCALE
217 setlocale(LC_ALL, "");
218 #endif
219 invo_name = r1bindex (argv[0], '/');
220
221 /* read user profile/context */
222 context_read();
223
224 mts_init (invo_name);
225 arguments = getarguments (invo_name, argc,argv, 1);
226 argp = arguments;
227
228 while ((cp = *argp++)) {
229 if (*cp == '-')
230 switch (smatch (++cp, switches)) {
231 case AMBIGSW:
232 ambigsw (cp, switches);
233 done (1);
234 case UNKWNSW:
235 adios (NULL, "-%s unknown", cp);
236
237 case HELPSW:
238 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
239 print_help (buf, switches, 1);
240 done (1);
241 case VERSIONSW:
242 print_version(invo_name);
243 done (1);
244
245 case IDSW:
246 if (!(cp = *argp++) || *cp == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
248 if ((id = atoi (cp)) < 1)
249 adios (NULL, "bad argument %s %s", argp[-2], cp);
250 continue;
251 case FDSW:
252 if (!(cp = *argp++) || *cp == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
254 if ((pfd = atoi (cp)) <= 1)
255 adios (NULL, "bad argument %s %s", argp[-2], cp);
256 continue;
257 case QDSW:
258 if (!(cp = *argp++) || *cp == '-')
259 adios (NULL, "missing argument to %s", argp[-2]);
260 if ((ppid = atoi (cp)) <= 1)
261 adios (NULL, "bad argument %s %s", argp[-2], cp);
262 continue;
263 case NMSW:
264 if (!(myname = *argp++) || *myname == '-')
265 adios (NULL, "missing argument to %s", argp[-2]);
266 continue;
267
268 case SCANSW:
269 scansw++;
270 continue;
271 case NSCANSW:
272 scansw = 0;
273 continue;
274
275 case PRMPTSW:
276 if (!(myprompt = *argp++) || *myprompt == '-')
277 adios (NULL, "missing argument to %s", argp[-2]);
278 continue;
279
280 case READSW:
281 if (!(cp = *argp++) || *cp == '-')
282 adios (NULL, "missing argument to %s", argp[-2]);
283 if ((vmh1 = atoi (cp)) < 1)
284 adios (NULL, "bad argument %s %s", argp[-2], cp);
285 continue;
286 case WRITESW:
287 if (!(cp = *argp++) || *cp == '-')
288 adios (NULL, "missing argument to %s", argp[-2]);
289 if ((vmh2 = atoi (cp)) < 1)
290 adios (NULL, "bad argument %s %s", argp[-2], cp);
291 continue;
292
293 case PREADSW:
294 if (!(cp = *argp++) || *cp == '-')
295 adios (NULL, "missing argument to %s", argp[-2]);
296 continue;
297 case PWRITSW:
298 if (!(cp = *argp++) || *cp == '-')
299 adios (NULL, "missing argument to %s", argp[-2]);
300 continue;
301
302 case TCURSW:
303 topcur++;
304 continue;
305 case NTCURSW:
306 topcur = 0;
307 continue;
308 }
309 if (*cp == '+' || *cp == '@') {
310 if (folder)
311 adios (NULL, "only one folder at a time!");
312 else
313 folder = pluspath (cp);
314 }
315 else
316 if (file)
317 adios (NULL, "only one file at a time!");
318 else
319 file = cp;
320 }
321
322 if (!file && !folder)
323 file = "./msgbox";
324 if (file && folder)
325 adios (NULL, "use a file or a folder, not both");
326 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
327 #ifdef FIOCLEX
328 if (pfd > 1)
329 ioctl (pfd, FIOCLEX, NULL);
330 #endif /* FIOCLEX */
331
332 istat = SIGNAL2 (SIGINT, intrser);
333 qstat = SIGNAL2 (SIGQUIT, quitser);
334
335 sc_width (); /* MAGIC... */
336
337 if ((vmh = vmh1 && vmh2)) {
338 rcinit (vmh1, vmh2);
339 pINI ();
340 SIGNAL (SIGINT, SIG_IGN);
341 SIGNAL (SIGQUIT, SIG_IGN);
342 #ifdef SIGTSTP
343 tstat = SIGNAL (SIGTSTP, SIG_IGN);
344 #endif /* SIGTSTP */
345 }
346
347 if (folder)
348 fsetup (folder);
349 else
350 setup (file);
351 readids (id);
352 display_info (id > 0 ? scansw : 0);
353
354 msh (id > 0 ? scansw : 0);
355
356 m_reset ();
357
358 done (0);
359 return 1;
360 }
361
362
363 static struct swit mshcmds[] = {
364 #define ADVCMD 0
365 { "advance", -7 },
366 #define ALICMD 1
367 { "ali", 0 },
368 #define EXPLCMD 2
369 { "burst", 0 },
370 #define COMPCMD 3
371 { "comp", 0 },
372 #define DISTCMD 4
373 { "dist", 0 },
374 #define EXITCMD 5
375 { "exit", 0 },
376 #define FOLDCMD 6
377 { "folder", 0 },
378 #define FORWCMD 7
379 { "forw", 0 },
380 #define HELPCMD 8
381 { "help", 0 },
382 #define INCMD 9
383 { "inc", 0 },
384 #define MARKCMD 10
385 { "mark", 0 },
386 #define MAILCMD 11
387 { "mhmail", 0 },
388 #define MHNCMD 12
389 { "mhn", 0 },
390 #define MSGKCMD 13
391 { "msgchk", 0 },
392 #define NEXTCMD 14
393 { "next", 0 },
394 #define PACKCMD 15
395 { "packf", 0 },
396 #define PICKCMD 16
397 { "pick", 0 },
398 #define PREVCMD 17
399 { "prev", 0 },
400 #define QUITCMD 18
401 { "quit", 0 },
402 #define FILECMD 19
403 { "refile", 0 },
404 #define REPLCMD 20
405 { "repl", 0 },
406 #define RMMCMD 21
407 { "rmm", 0 },
408 #define SCANCMD 22
409 { "scan", 0 },
410 #define SENDCMD 23
411 { "send", 0 },
412 #define SHOWCMD 24
413 { "show", 0 },
414 #define SORTCMD 25
415 { "sortm", 0 },
416 #define WHATCMD 26
417 { "whatnow", 0 },
418 #define WHOMCMD 27
419 { "whom", 0 },
420 { NULL, 0 }
421 };
422
423
424 static void
425 msh (int scansw)
426 {
427 int i;
428 register char *cp, **ap;
429 char prompt[BUFSIZ], *vec[MAXARGS];
430 struct Cmd typein;
431 register struct Cmd *cmdp;
432 static int once_only = ADVCMD;
433
434 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
435 cmdp = &typein;
436
437 for (;;) {
438 if (yp) {
439 fclose (yp);
440 yp = NULL;
441 }
442 if (vmh) {
443 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
444 rcdone ();
445 return;
446 }
447 } else {
448 check_folder (scansw);
449 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
450 putchar ('\n');
451 return;
452 }
453 }
454 cmd_name = mshcmds[i].sw;
455
456 switch (i) {
457 case QUITCMD:
458 quit ();
459 return;
460
461 case ADVCMD:
462 if (once_only == ADVCMD)
463 once_only = i = SHOWCMD;
464 else
465 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
466 cmd_name = mshcmds[i].sw;
467 /* and fall... */
468
469 case EXITCMD:
470 case EXPLCMD:
471 case FOLDCMD:
472 case FORWCMD: /* sigh */
473 case MARKCMD:
474 case NEXTCMD:
475 case PACKCMD:
476 case PICKCMD:
477 case PREVCMD:
478 case RMMCMD:
479 case SHOWCMD:
480 case SCANCMD:
481 case SORTCMD:
482 if ((cp = context_find (cmd_name))) {
483 cp = getcpy (cp);
484 ap = brkstring (cp, " ", "\n");
485 ap = copyip (ap, vec, MAXARGS);
486 } else {
487 ap = vec;
488 }
489 break;
490
491 default:
492 cp = NULL;
493 ap = vec;
494 break;
495 }
496 copyip (cmdp->args + 1, ap, MAXARGS);
497
498 m_init ();
499
500 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
501 if (cp != NULL)
502 free (cp);
503 continue;
504 }
505 modified = 0;
506 redirected = vmh || cmdp->direction != STDIO;
507
508 switch (i) {
509 case ALICMD:
510 case COMPCMD:
511 case INCMD:
512 case MAILCMD:
513 case MSGKCMD:
514 case SENDCMD:
515 case WHATCMD:
516 case WHOMCMD:
517 if (!vmh || ttyN (cmdp) != NOTOK)
518 forkcmd (vec, cmd_name);
519 break;
520
521 case DISTCMD:
522 if (!vmh || ttyN (cmdp) != NOTOK)
523 distcmd (vec);
524 break;
525
526 case EXPLCMD:
527 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
528 explcmd (vec);
529 break;
530
531 case FILECMD:
532 if (!vmh
533 || (filehak (vec) == OK ? ttyN (cmdp)
534 : winN (cmdp, DISPLAY, 1)) != NOTOK)
535 filecmd (vec);
536 break;
537
538 case FOLDCMD:
539 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
540 foldcmd (vec);
541 break;
542
543 case FORWCMD:
544 if (!vmh || ttyN (cmdp) != NOTOK)
545 forwcmd (vec);
546 break;
547
548 case HELPCMD:
549 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
550 helpcmd (vec);
551 break;
552
553 case EXITCMD:
554 case MARKCMD:
555 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
556 markcmd (vec);
557 break;
558
559 case MHNCMD:
560 if (!vmh || ttyN (cmdp) != NOTOK)
561 mhncmd (vec);
562 break;
563
564 case NEXTCMD:
565 case PREVCMD:
566 case SHOWCMD:
567 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
568 showcmd (vec);
569 break;
570
571 case PACKCMD:
572 if (!vmh
573 || (packhak (vec) == OK ? ttyN (cmdp)
574 : winN (cmdp, DISPLAY, 1)) != NOTOK)
575 packcmd (vec);
576 break;
577
578 case PICKCMD:
579 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
580 pickcmd (vec);
581 break;
582
583 case REPLCMD:
584 if (!vmh || ttyN (cmdp) != NOTOK)
585 replcmd (vec);
586 break;
587
588 case RMMCMD:
589 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
590 rmmcmd (vec);
591 break;
592
593 case SCANCMD:
594 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
595 scancmd (vec);
596 break;
597
598 case SORTCMD:
599 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
600 sortcmd (vec);
601 break;
602
603 default:
604 padios (NULL, "no dispatch for %s", cmd_name);
605 }
606
607 if (vmh) {
608 if (vmhtty != NOTOK)
609 ttyR (cmdp);
610 if (vmhpid > OK)
611 winR (cmdp);
612 }
613 else
614 fin_io (cmdp, vmh);
615 if (cp != NULL)
616 free (cp);
617 if (i == EXITCMD) {
618 quit ();
619 return;
620 }
621 }
622 }
623
624
625 void
626 fsetup (char *folder)
627 {
628 register int msgnum;
629 char *maildir;
630 struct stat st;
631
632 maildir = m_maildir (folder);
633 if (chdir (maildir) == NOTOK)
634 padios (maildir, "unable to change directory to");
635
636 /* read folder and create message structure */
637 if (!(mp = folder_read (folder)))
638 padios (NULL, "unable to read folder %s", folder);
639
640 /* check for empty folder */
641 if (mp->nummsg == 0)
642 padios (NULL, "no messages in %s", folder);
643
644 mode = m_gmprot ();
645 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
646
647 m_gMsgs (mp->hghmsg);
648
649 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
650 Msgs[msgnum].m_bboard_id = 0;
651 Msgs[msgnum].m_top = NOTOK;
652 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
653 Msgs[msgnum].m_scanl = NULL;
654 }
655
656 m_init ();
657
658 fmsh = getcpy (folder);
659
660 maxfds = OPEN_MAX / 2;
661
662 if ((maxfds -= 2) < 1)
663 maxfds = 1;
664 }
665
666
667 void
668 setup (char *file)
669 {
670 int i, msgp;
671 struct stat st;
672 if ((fp = fopen (file, "r")) == NULL)
673 padios (file, "unable to read");
674 #ifdef FIOCLEX
675 ioctl (fileno (fp), FIOCLEX, NULL);
676 #endif /* FIOCLEX */
677 if (fstat (fileno (fp), &st) != NOTOK) {
678 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
679 msgp = read_map (file, (long) st.st_size);
680 }
681 else {
682 mode = m_gmprot (), mtime = 0;
683 msgp = 0;
684 }
685
686 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
687 padios (NULL, "no messages in %s", myname ? myname : file);
688
689 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
690 padios (NULL, "unable to allocate folder storage");
691
692 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
693 padios (NULL, "unable to allocate message status storage");
694
695 mp->hghmsg = msgp;
696 mp->nummsg = msgp;
697 mp->lowmsg = 1;
698 mp->curmsg = 0;
699 mp->foldpath = getcpy (myname ? myname : file);
700 clear_folder_flags (mp);
701
702 stat (file, &st);
703 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
704 set_readonly (mp);
705
706 mp->lowoff = 1;
707 mp->hghoff = mp->hghmsg + 1;
708
709 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
710 clear_msg_flags (mp, i);
711 set_exists (mp, i);
712 }
713 m_init ();
714
715 mp->msgattrs[0] = getcpy ("unseen");
716 mp->msgattrs[1] = NULL;
717
718 m_unknown (fp); /* the MAGIC invocation */
719 if (fmsh) {
720 free (fmsh);
721 fmsh = NULL;
722 }
723 }
724
725
726 static int
727 read_map (char *file, long size)
728 {
729 register int i, msgp;
730 register struct drop *dp, *mp;
731 struct drop *rp;
732
733 if ((i = map_read (file, size, &rp, 1)) == 0)
734 return 0;
735
736 m_gMsgs (i);
737
738 msgp = 1;
739 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
740 mp = &Msgs[msgp].m_drop;
741 mp->d_id = dp->d_id;
742 mp->d_size = dp->d_size;
743 mp->d_start = dp->d_start;
744 mp->d_stop = dp->d_stop;
745 Msgs[msgp].m_scanl = NULL;
746 }
747 free ((char *) rp);
748
749 return (msgp - 1);
750 }
751
752
753 static int
754 read_file (long pos, int msgp)
755 {
756 register int i;
757 register struct drop *dp, *mp;
758 struct drop *rp;
759
760 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
761 return (msgp - 1);
762
763 m_gMsgs ((msgp - 1) + i);
764
765 for (dp = rp; i-- > 0; msgp++, dp++) {
766 mp = &Msgs[msgp].m_drop;
767 mp->d_id = 0;
768 mp->d_size = dp->d_size;
769 mp->d_start = dp->d_start;
770 mp->d_stop = dp->d_stop;
771 Msgs[msgp].m_scanl = NULL;
772 }
773 free ((char *) rp);
774
775 return (msgp - 1);
776 }
777
778
779 static void
780 m_gMsgs (int n)
781 {
782 int nmsgs;
783
784 if (Msgs == NULL) {
785 nMsgs = n + MAXFOLDER / 2;
786 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
787 if (Msgs == NULL)
788 padios (NULL, "unable to allocate Msgs structure");
789 return;
790 }
791
792 if (nMsgs >= n)
793 return;
794
795 nmsgs = nMsgs + n + MAXFOLDER / 2;
796 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
797 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
798
799 nMsgs = nmsgs;
800 }
801
802
803 FILE *
804 msh_ready (int msgnum, int full)
805 {
806 NMH_UNUSED (full);
807 register int msgp;
808 int fd;
809 char *cp;
810
811 if (yp) {
812 fclose (yp);
813 yp = NULL;
814 }
815
816 if (fmsh) {
817 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
818 if (numfds >= maxfds)
819 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
820 if (Msgs[msgp].m_top != NOTOK) {
821 close (Msgs[msgp].m_top);
822 Msgs[msgp].m_top = NOTOK;
823 numfds--;
824 break;
825 }
826
827 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
828 padios (cp, "unable to open message");
829 Msgs[msgnum].m_top = fd;
830 numfds++;
831 }
832
833 if ((fd = dup (fd)) == NOTOK)
834 padios ("cached message", "unable to dup");
835 if ((yp = fdopen (fd, "r")) == NULL)
836 padios (NULL, "unable to fdopen cached message");
837 fseek (yp, 0L, SEEK_SET);
838 return yp;
839 }
840
841 m_eomsbr ((int (*)()) 0); /* XXX */
842 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
843 return fp;
844 }
845
846
847 static int
848 check_folder (int scansw)
849 {
850 int seqnum, i, low, hgh, msgp;
851 struct stat st;
852
853 if (fmsh) {
854 if (stat (mp->foldpath, &st) == NOTOK)
855 padios (mp->foldpath, "unable to stat");
856 if (mtime == st.st_mtime)
857 return 0;
858 mtime = st.st_mtime;
859
860 low = mp->hghmsg + 1;
861 folder_free (mp); /* free folder/message structure */
862
863 if (!(mp = folder_read (fmsh)))
864 padios (NULL, "unable to re-read folder %s", fmsh);
865
866 hgh = mp->hghmsg;
867
868 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
869 if (Msgs[msgp].m_top != NOTOK) {
870 close (Msgs[msgp].m_top);
871 Msgs[msgp].m_top = NOTOK;
872 numfds--;
873 }
874 if (Msgs[msgp].m_scanl) {
875 free (Msgs[msgp].m_scanl);
876 Msgs[msgp].m_scanl = NULL;
877 }
878 }
879
880 m_init ();
881
882 if (modified || low > hgh)
883 return 1;
884 goto check_vmh;
885 }
886 if (fstat (fileno (fp), &st) == NOTOK)
887 padios (mp->foldpath, "unable to fstat");
888 if (mtime == st.st_mtime)
889 return 0;
890 mode = (int) (st.st_mode & 0777);
891 mtime = st.st_mtime;
892
893 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
894 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
895 if (msgp >= MAXFOLDER)
896 padios (NULL, "more than %d messages in %s", MAXFOLDER,
897 mp->foldpath);
898 if (msgp <= mp->hghmsg)
899 return 0; /* XXX */
900
901 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
902 padios (NULL, "unable to allocate folder storage");
903
904 low = mp->hghmsg + 1, hgh = msgp;
905 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
906 for (i = mp->hghmsg + 1; i <= msgp; i++) {
907 set_exists(mp, i);
908 if (seqnum != -1)
909 add_sequence(mp, seqnum, i);
910 mp->nummsg++;
911 }
912 mp->hghmsg = msgp;
913 m_init ();
914
915 check_vmh: ;
916 if (vmh)
917 return 1;
918
919 advise (NULL, "new messages have arrived!\007");
920 if (scansw)
921 scanrange (low, hgh);
922
923 return 1;
924 }
925
926
927 static void
928 scanrange (int low, int hgh)
929 {
930 char buffer[BUFSIZ];
931
932 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
933 scanstring (buffer);
934 }
935
936
937 static void
938 scanstring (char *arg)
939 {
940 char *cp, **ap, *vec[MAXARGS];
941
942 /*
943 * This should be replace with a call to getarguments()
944 */
945 if ((cp = context_find (cmd_name = "scan"))) {
946 cp = getcpy (cp);
947 ap = brkstring (cp, " ", "\n");
948 ap = copyip (ap, vec, MAXARGS);
949 } else {
950 ap = vec;
951 }
952 *ap++ = arg;
953 *ap = NULL;
954 m_init ();
955 scancmd (vec);
956 if (cp != NULL)
957 free (cp);
958 }
959
960
961 void
962 readids (int id)
963 {
964 register int cur, seqnum, i=0, msgnum;
965
966 if (mp->curmsg == 0)
967 seq_setcur (mp, mp->lowmsg);
968 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
969 return;
970
971 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
972 add_sequence(mp, seqnum, msgnum);
973
974 if (id != 1) {
975 cur = mp->curmsg;
976
977 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
978 if (does_exist(mp, msgnum)) /* FIX */
979 if ((i = readid (msgnum)) > 0 && i < id) {
980 cur = msgnum + 1;
981 clear_sequence(mp, seqnum, msgnum);
982 break;
983 }
984 for (i = mp->lowmsg; i < msgnum; i++)
985 clear_sequence(mp, seqnum, i);
986
987 if (cur > mp->hghmsg)
988 cur = mp->hghmsg;
989
990 seq_setcur (mp, cur);
991 }
992
993 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
994 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
995 id - 1, i);
996 }
997
998
999 int
1000 readid (int msgnum)
1001 {
1002 int i, state;
1003 char *bp, buf[BUFSIZ], name[NAMESZ];
1004 register FILE *zp;
1005
1006 if (Msgs[msgnum].m_bboard_id)
1007 return Msgs[msgnum].m_bboard_id;
1008
1009 zp = msh_ready (msgnum, 0);
1010 for (state = FLD;;)
1011 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1012 case FLD:
1013 case FLDEOF:
1014 case FLDPLUS:
1015 if (!mh_strcasecmp (name, BBoard_ID)) {
1016 bp = getcpy (buf);
1017 while (state == FLDPLUS) {
1018 state = m_getfld (state, name, buf, sizeof(buf), zp);
1019 bp = add (buf, bp);
1020 }
1021 i = atoi (bp);
1022 free (bp);
1023 if (i > 0)
1024 return (Msgs[msgnum].m_bboard_id = i);
1025 else
1026 continue;
1027 }
1028 while (state == FLDPLUS)
1029 state = m_getfld (state, name, buf, sizeof(buf), zp);
1030 if (state != FLDEOF)
1031 continue;
1032
1033 default:
1034 return 0;
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 (m_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 }