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