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