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