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