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