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