3 * msh.c -- The nmh shell
10 * Keep more status information in maildrop map
15 #include <h/signals.h>
16 #include <h/dropsbr.h>
17 #include <h/fmt_scan.h>
18 #include <h/scansbr.h>
19 #include <zotnet/tws/tws.h>
20 #include <zotnet/mts/mts.h>
38 #define QUOTE '\\' /* sigh */
40 static struct swit switches[] = {
42 { "idstart number", -7 }, /* interface from bbc */
44 { "idstop number", -6 }, /* .. */
46 { "idquit number", -6 }, /* .. */
48 { "idname BBoard", -6 }, /* .. */
50 { "prompt string", 0 },
58 { "vmhwrite fd", -8 },
62 { "popwrite fd", -8 },
74 static int mbx_style = MMDF_FORMAT;
79 char*fmsh = NULL; /* folder instead of file */
80 int modified; /* command modified folder */
81 struct msgs *mp; /* used a lot */
83 struct Msg *Msgs = NULL; /* Msgs[0] not used */
84 static FILE *fp; /* input file */
85 static FILE *yp = NULL; /* temporary file */
86 static int mode; /* mode of file */
87 static int numfds = 0; /* number of files cached */
88 static int maxfds = 0; /* number of files cached to be cached */
89 static time_t mtime = (time_t) 0; /* mtime of file */
94 #define ALARM ((unsigned int) 10)
95 #define ttyN(c) ttyNaux ((c), NULL)
99 static int vmhpid = OK;
104 static int vmhtty = NOTOK;
111 static int topcur = 0;
113 static int numwins = 0;
114 static int windows[NWIN + 1];
116 static jmp_buf peerenv;
119 int pmsh = 0; /* BPOP enabled */
120 extern char response[];
126 static int pfd = NOTOK; /* fd parent is reading from */
127 static int ppid = 0; /* pid of parent */
132 int interactive; /* running from a /dev/tty */
133 int redirected; /* re-directing output */
134 FILE *sp = NULL; /* original stdout */
136 char *cmd_name; /* command being run */
137 char myfilter[BUFSIZ]; /* path to mhl.forward */
139 static char *myprompt = "(%s) ";/* prompting string */
144 static int gap; /* gap in BBoard-ID:s */
145 static char *myname = NULL; /* BBoard name */
146 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
151 SIGNAL_HANDLER istat; /* original SIGINT */
152 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
153 SIGNAL_HANDLER qstat; /* original SIGQUIT */
156 SIGNAL_HANDLER tstat; /* original SIGTSTP */
159 int interrupted; /* SIGINT detected */
160 int broken_pipe; /* SIGPIPE detected */
161 int told_to_quit; /* SIGQUIT detected */
164 int should_intr; /* signal handler should interrupt call */
165 jmp_buf sigenv; /* the environment pointer */
171 int SOprintf (char *, ...); /* from termsbr.c */
172 int sc_width (void); /* from termsbr.c */
173 void fsetup (char *);
175 FILE *msh_ready (int, int);
178 void display_info (int);
181 void seq_setcur (struct msgs *, int);
182 void padios (char *, char *, ...);
183 void padvise (char *, char *, ...);
189 static void msh (int);
190 static int read_map (char *, long);
191 static int read_file (long, int);
195 static int pop_statmsg (char *);
197 static int read_pop (void);
198 static int pop_action (char *);
201 static void m_gMsgs (int);
202 FILE *msh_ready (int, int);
203 static int check_folder (int);
204 static void scanrange (int, int);
205 static void scanstring (char *);
206 static void write_ids (void);
207 static void quit (void);
208 static int getargs (char *, struct swit *, struct Cmd *);
209 static int getcmds (struct swit *, struct Cmd *, int);
210 static int parse (char *, struct Cmd *);
211 static int init_io (struct Cmd *, int);
212 static int initaux_io (struct Cmd *);
213 static void fin_io (struct Cmd *, int);
214 static void finaux_io (struct Cmd *);
215 static void m_init (void);
216 static RETSIGTYPE intrser (int);
217 static RETSIGTYPE pipeser (int);
218 static RETSIGTYPE quitser (int);
219 static RETSIGTYPE alrmser (int);
220 static int pINI (void);
221 static int pQRY (char *, int);
222 static int pQRY1 (int);
223 static int pQRY2 (void);
224 static int pCMD (char *, struct swit *, struct Cmd *);
225 static int pFIN (void);
226 static int peerwait (void);
227 static int ttyNaux (struct Cmd *, char *);
228 static int ttyR (struct Cmd *);
229 static int winN (struct Cmd *, int, int);
230 static int winR (struct Cmd *);
231 static int winX (int);
235 main (int argc, char **argv)
237 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
238 char *cp, *file = NULL, *folder = NULL;
239 char **argp, **arguments, buf[BUFSIZ];
241 int pmsh1 = 0, pmsh2 = 0;
245 setlocale(LC_ALL, "");
247 invo_name = r1bindex (argv[0], '/');
249 /* read user profile/context */
252 mts_init (invo_name);
253 arguments = getarguments (invo_name, argc,argv, 1);
256 while ((cp = *argp++)) {
258 switch (smatch (++cp, switches)) {
260 ambigsw (cp, switches);
263 adios (NULL, "-%s unknown", cp);
266 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
267 print_help (buf, switches, 1);
270 print_version(invo_name);
274 if (!(cp = *argp++) || *cp == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
276 if ((id = atoi (cp)) < 1)
277 adios (NULL, "bad argument %s %s", argp[-2], cp);
280 if (!(cp = *argp++) || *cp == '-')
281 adios (NULL, "missing argument to %s", argp[-2]);
282 if ((pfd = atoi (cp)) <= 1)
283 adios (NULL, "bad argument %s %s", argp[-2], cp);
286 if (!(cp = *argp++) || *cp == '-')
287 adios (NULL, "missing argument to %s", argp[-2]);
288 if ((ppid = atoi (cp)) <= 1)
289 adios (NULL, "bad argument %s %s", argp[-2], cp);
292 if (!(myname = *argp++) || *myname == '-')
293 adios (NULL, "missing argument to %s", argp[-2]);
304 if (!(myprompt = *argp++) || *myprompt == '-')
305 adios (NULL, "missing argument to %s", argp[-2]);
309 if (!(cp = *argp++) || *cp == '-')
310 adios (NULL, "missing argument to %s", argp[-2]);
311 if ((vmh1 = atoi (cp)) < 1)
312 adios (NULL, "bad argument %s %s", argp[-2], cp);
315 if (!(cp = *argp++) || *cp == '-')
316 adios (NULL, "missing argument to %s", argp[-2]);
317 if ((vmh2 = atoi (cp)) < 1)
318 adios (NULL, "bad argument %s %s", argp[-2], cp);
322 if (!(cp = *argp++) || *cp == '-')
323 adios (NULL, "missing argument to %s", argp[-2]);
325 if ((pmsh1 = atoi (cp)) < 1)
326 adios (NULL, "bad argument %s %s", argp[-2], cp);
330 if (!(cp = *argp++) || *cp == '-')
331 adios (NULL, "missing argument to %s", argp[-2]);
333 if ((pmsh2 = atoi (cp)) < 1)
334 adios (NULL, "bad argument %s %s", argp[-2], cp);
345 if (*cp == '+' || *cp == '@') {
347 adios (NULL, "only one folder at a time!");
349 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
353 adios (NULL, "only one file at a time!");
358 if (!file && !folder)
361 adios (NULL, "use a file or a folder, not both");
362 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
365 ioctl (pfd, FIOCLEX, NULL);
371 istat = SIGNAL2 (SIGINT, intrser);
372 qstat = SIGNAL2 (SIGQUIT, quitser);
374 sc_width (); /* MAGIC... */
376 if ((vmh = vmh1 && vmh2)) {
379 SIGNAL (SIGINT, SIG_IGN);
380 SIGNAL (SIGQUIT, SIG_IGN);
382 tstat = SIGNAL (SIGTSTP, SIG_IGN);
387 if (pmsh = pmsh1 && pmsh2) {
388 cp = getenv ("MHPOPDEBUG");
390 if (pop_set (pmsh1, pmsh2, cp && *cp, myname) == NOTOK)
392 if (pop_set (pmsh1, pmsh2, cp && *cp) == NOTOK)
394 padios (NULL, "%s", response);
396 file = folder, folder = NULL;
405 display_info (id > 0 ? scansw : 0);
407 msh (id > 0 ? scansw : 0);
415 static struct swit mshcmds[] = {
480 register char *cp, **ap;
481 char prompt[BUFSIZ], *vec[MAXARGS];
483 register struct Cmd *cmdp;
484 static int once_only = ADVCMD;
486 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
495 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
500 check_folder (scansw);
501 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
506 cmd_name = mshcmds[i].sw;
514 if (once_only == ADVCMD)
515 once_only = i = SHOWCMD;
517 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
518 cmd_name = mshcmds[i].sw;
524 case FORWCMD: /* sigh */
534 if ((cp = context_find (cmd_name))) {
536 ap = brkstring (cp, " ", "\n");
537 ap = copyip (ap, vec, MAXARGS);
548 copyip (cmdp->args + 1, ap, MAXARGS);
552 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
558 redirected = vmh || cmdp->direction != STDIO;
569 if (!vmh || ttyN (cmdp) != NOTOK)
570 forkcmd (vec, cmd_name);
574 if (!vmh || ttyN (cmdp) != NOTOK)
579 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
585 || (filehak (vec) == OK ? ttyN (cmdp)
586 : winN (cmdp, DISPLAY, 1)) != NOTOK)
591 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
596 if (!vmh || ttyN (cmdp) != NOTOK)
601 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
607 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
612 if (!vmh || ttyN (cmdp) != NOTOK)
619 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
625 || (packhak (vec) == OK ? ttyN (cmdp)
626 : winN (cmdp, DISPLAY, 1)) != NOTOK)
631 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
636 if (!vmh || ttyN (cmdp) != NOTOK)
641 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
646 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
651 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
656 padios (NULL, "no dispatch for %s", cmd_name);
678 fsetup (char *folder)
684 maildir = m_maildir (folder);
685 if (chdir (maildir) == NOTOK)
686 padios (maildir, "unable to change directory to");
688 /* read folder and create message structure */
689 if (!(mp = folder_read (folder)))
690 padios (NULL, "unable to read folder %s", folder);
692 /* check for empty folder */
694 padios (NULL, "no messages in %s", folder);
697 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
699 m_gMsgs (mp->hghmsg);
701 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
702 Msgs[msgnum].m_bboard_id = 0;
703 Msgs[msgnum].m_top = NOTOK;
704 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
705 Msgs[msgnum].m_scanl = NULL;
710 fmsh = getcpy (folder);
712 maxfds = OPEN_MAX / 2;
714 if ((maxfds -= 2) < 1)
730 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
731 if ((fp = fopen (tmpfil, "w+")) == NULL)
732 padios (tmpfil, "unable to create");
737 if ((fp = fopen (file, "r")) == NULL)
738 padios (file, "unable to read");
740 ioctl (fileno (fp), FIOCLEX, NULL);
742 if (fstat (fileno (fp), &st) != NOTOK) {
743 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
744 msgp = read_map (file, (long) st.st_size);
747 mode = m_gmprot (), mtime = 0;
751 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
752 padios (NULL, "no messages in %s", myname ? myname : file);
754 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
755 padios (NULL, "unable to allocate folder storage");
757 if (!(mp->msgstats = calloc ((size_t) 1, msgp + 3)))
758 padios (NULL, "unable to allocate message status storage");
764 mp->foldpath = getcpy (myname ? myname : file);
765 clear_folder_flags (mp);
773 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
780 mp->hghoff = mp->hghmsg + 1;
785 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
787 clear_msg_flags (mp, i);
792 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
793 if (Msgs[i].m_top) /* set in read_pop() */
794 clear_msg_flags (mp, i);
802 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
803 clear_msg_flags (mp, i);
808 mp->msgattrs[0] = getcpy ("unseen");
809 mp->msgattrs[1] = NULL;
811 m_unknown (fp); /* the MAGIC invocation */
820 read_map (char *file, long size)
822 register int i, msgp;
823 register struct drop *dp, *mp;
831 if ((i = map_read (file, size, &rp, 1)) == 0)
837 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
838 mp = &Msgs[msgp].m_drop;
840 mp->d_size = dp->d_size;
841 mp->d_start = dp->d_start;
842 mp->d_stop = dp->d_stop;
843 Msgs[msgp].m_scanl = NULL;
852 read_file (long pos, int msgp)
855 register struct drop *dp, *mp;
863 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
866 m_gMsgs ((msgp - 1) + i);
868 for (dp = rp; i-- > 0; msgp++, dp++) {
869 mp = &Msgs[msgp].m_drop;
871 mp->d_size = dp->d_size;
872 mp->d_start = dp->d_start;
873 mp->d_stop = dp->d_stop;
874 Msgs[msgp].m_scanl = NULL;
884 static int pop_base = 0;
887 pop_statmsg (char *s)
891 n = (i = atoi (s)) - pop_base; /* s="nnn header-line..." */
892 Msgs[n].m_top = Msgs[n].m_bboard_id = i;
902 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
903 padios (NULL, "%s", response);
907 #ifdef NNTP /* this makes read_pop() do some real work... */
908 pop_base = nbytes - 1; /* nmsgs=last-first+1, nbytes=first */
909 pop_exists (pop_statmsg);
918 fprintf (yp, "%s\n", s);
929 nMsgs = n + MAXFOLDER / 2;
930 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
932 padios (NULL, "unable to allocate Msgs structure");
939 nmsgs = nMsgs + n + MAXFOLDER / 2;
940 Msgs = (struct Msg *) realloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
942 padios (NULL, "unable to reallocate Msgs structure");
943 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
950 msh_ready (int msgnum, int full)
966 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
967 if (numfds >= maxfds)
968 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
969 if (Msgs[msgp].m_top != NOTOK) {
970 close (Msgs[msgp].m_top);
971 Msgs[msgp].m_top = NOTOK;
976 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
977 padios (cp, "unable to open message");
978 Msgs[msgnum].m_top = fd;
982 if ((fd = dup (fd)) == NOTOK)
983 padios ("cached message", "unable to dup");
984 if ((yp = fdopen (fd, "r")) == NULL)
985 padios (NULL, "unable to fdopen cached message");
986 fseek (yp, 0L, SEEK_SET);
991 if (pmsh && is_virtual (mp, msgnum)) {
992 if (Msgs[msgnum].m_top == 0)
993 padios (NULL, "msh_ready (%d, %d) botch", msgnum, full);
995 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
996 if ((yp = fopen (tmpfil, "w+")) == NULL)
997 padios (tmpfil, "unable to create");
1000 if (pop_top (Msgs[msgnum].m_top, 4, pop_action) == NOTOK)
1001 padios (NULL, "%s", response);
1003 m_eomsbr ((int (*)()) 0); /* XXX */
1004 msg_style = MS_DEFAULT; /* .. */
1005 fseek (yp, 0L, SEEK_SET);
1009 fseek (fp, 0L, SEEK_END);
1010 fwrite (mmdlm1, 1, strlen (mmdlm1), fp);
1012 padios ("temporary file", "write error on");
1013 fseek (fp, 0L, SEEK_END);
1017 if (pop_retr (Msgs[msgnum].m_top, pop_action) == NOTOK)
1018 padios (NULL, "%s", response);
1021 fseek (fp, 0L, SEEK_END);
1023 fwrite (mmdlm2, 1, strlen (mmdlm2), fp);
1025 padios ("temporary file", "write error on");
1027 Msgs[msgnum].m_start = pos1;
1028 Msgs[msgnum].m_stop = pos2;
1030 unset_virtual (mp, msgnum);
1034 m_eomsbr ((int (*)()) 0); /* XXX */
1035 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
1041 check_folder (int scansw)
1043 int seqnum, i, low, hgh, msgp;
1052 if (stat (mp->foldpath, &st) == NOTOK)
1053 padios (mp->foldpath, "unable to stat");
1054 if (mtime == st.st_mtime)
1056 mtime = st.st_mtime;
1058 low = mp->hghmsg + 1;
1059 folder_free (mp); /* free folder/message structure */
1061 if (!(mp = folder_read (fmsh)))
1062 padios (NULL, "unable to re-read folder %s", fmsh);
1066 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
1067 if (Msgs[msgp].m_top != NOTOK) {
1068 close (Msgs[msgp].m_top);
1069 Msgs[msgp].m_top = NOTOK;
1072 if (Msgs[msgp].m_scanl) {
1073 free (Msgs[msgp].m_scanl);
1074 Msgs[msgp].m_scanl = NULL;
1080 if (modified || low > hgh)
1084 if (fstat (fileno (fp), &st) == NOTOK)
1085 padios (mp->foldpath, "unable to fstat");
1086 if (mtime == st.st_mtime)
1088 mode = (int) (st.st_mode & 0777);
1089 mtime = st.st_mtime;
1091 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
1092 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
1093 if (msgp >= MAXFOLDER)
1094 padios (NULL, "more than %d messages in %s", MAXFOLDER,
1096 if (msgp <= mp->hghmsg)
1099 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
1100 padios (NULL, "unable to allocate folder storage");
1102 low = mp->hghmsg + 1, hgh = msgp;
1103 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
1104 for (i = mp->hghmsg + 1; i <= msgp; i++) {
1107 add_sequence(mp, seqnum, i);
1117 advise (NULL, "new messages have arrived!\007");
1119 scanrange (low, hgh);
1126 scanrange (int low, int hgh)
1128 char buffer[BUFSIZ];
1130 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
1131 scanstring (buffer);
1136 scanstring (char *arg)
1138 char *cp, **ap, *vec[MAXARGS];
1141 * This should be replace with a call to getarguments()
1143 if ((cp = context_find (cmd_name = "scan"))) {
1145 ap = brkstring (cp, " ", "\n");
1146 ap = copyip (ap, vec, MAXARGS);
1162 register int cur, seqnum, i, msgnum;
1164 if (mp->curmsg == 0)
1165 seq_setcur (mp, mp->lowmsg);
1166 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
1169 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1170 add_sequence(mp, seqnum, msgnum);
1175 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1176 if (does_exist(mp, msgnum)) /* FIX */
1177 if ((i = readid (msgnum)) > 0 && i < id) {
1179 clear_sequence(mp, seqnum, msgnum);
1182 for (i = mp->lowmsg; i < msgnum; i++)
1183 clear_sequence(mp, seqnum, i);
1185 if (cur > mp->hghmsg)
1188 seq_setcur (mp, cur);
1191 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
1192 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1201 char *bp, buf[BUFSIZ], name[NAMESZ];
1204 int arg1, arg2, arg3;
1207 if (Msgs[msgnum].m_bboard_id)
1208 return Msgs[msgnum].m_bboard_id;
1211 if (Msgs[msgnum].m_top == 0)
1212 padios (NULL, "readid (%d) botch", msgnum);
1213 if (pop_list (Msgs[msgnum].m_top, (int *) 0, &arg1, &arg2, &arg3) == OK
1215 return (Msgs[msgnum].m_bboard_id = arg3);
1219 zp = msh_ready (msgnum, 0);
1221 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1225 if (!strcasecmp (name, BBoard_ID)) {
1227 while (state == FLDPLUS) {
1228 state = m_getfld (state, name, buf, sizeof(buf), zp);
1234 return (Msgs[msgnum].m_bboard_id = i);
1238 while (state == FLDPLUS)
1239 state = m_getfld (state, name, buf, sizeof(buf), zp);
1240 if (state != FLDEOF)
1250 display_info (int scansw)
1254 interactive = isatty (fileno (stdout));
1256 if ((sd = dup (fileno (stdout))) == NOTOK)
1257 padios ("standard output", "unable to dup");
1258 #ifndef BSD42 /* XXX */
1260 ioctl (sd, FIOCLEX, NULL);
1261 #endif /* FIOCLEX */
1262 #endif /* not BSD42 */
1263 if ((sp = fdopen (sd, "w")) == NULL)
1264 padios ("standard output", "unable to fdopen");
1267 m_putenv ("mhfolder", mp->foldpath);
1272 printf ("Reading ");
1273 if (SOprintf ("%s", myname))
1274 printf ("%s", myname);
1275 printf (", currently at message %d of %d\n",
1276 mp->curmsg, mp->hghmsg);
1279 printf ("Reading ");
1281 printf ("+%s", fmsh);
1283 printf ("%s", mp->foldpath);
1284 printf (", currently at message %d of %d\n",
1285 mp->curmsg, mp->hghmsg);
1288 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1290 && in_sequence(mp, seqnum, mp->hghmsg))
1291 scanstring ("unseen");
1298 int i = 0, seqnum, msgnum;
1304 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1305 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1306 if (!in_sequence(mp, seqnum, msgnum)) {
1307 if (Msgs[msgnum].m_bboard_id == 0)
1309 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1313 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1314 write (pfd, buffer, sizeof(buffer));
1324 char *cp, tmpfil[BUFSIZ];
1325 char map1[BUFSIZ], map2[BUFSIZ];
1329 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1331 rc2peer (RC_FIN, 0, NULL);
1336 ttyNaux (NULLCMD, "FAST");
1338 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1339 advise (mp->foldpath, "unable to lock");
1346 if (fstat (fileno (dp), &st) == NOTOK) {
1347 advise (mp->foldpath, "unable to stat");
1350 if (mtime != st.st_mtime) {
1351 advise (NULL, "new messages have arrived, no update");
1354 mode = (int) (st.st_mode & 0777);
1356 if (mp->nummsg == 0) {
1357 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1358 if (getanswer (cp)) {
1359 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1362 advise (mp->foldpath, "error zero'ing");
1363 unlink (map_name (mp->foldpath));/* XXX */
1368 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1369 if (!getanswer (cp))
1371 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1372 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1373 advise (tmpfil, "unable to open");
1377 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1378 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1379 mbx_close (tmpfil, md);
1381 unlink (map_name (tmpfil));
1384 mbx_close (tmpfil, md);
1386 if (rename (tmpfil, mp->foldpath) == NOTOK)
1387 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1389 strncpy (map1, map_name (tmpfil), sizeof(map1));
1390 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1392 if (rename (map1, map2) == NOTOK) {
1393 admonish (map2, "unable to rename %s to", map1);
1402 lkfclose (dp, mp->foldpath);
1411 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1415 static char buffer[BUFSIZ];
1421 switch (setjmp (sigenv)) {
1428 if (interrupted && !told_to_quit) {
1434 kill (ppid, SIGEMT);
1436 kill (ppid, SIGTERM);
1442 printf ("%s", prompt);
1445 for (cp = buffer; (i = getchar ()) != '\n';) {
1447 if (interrupted && !told_to_quit) {
1452 if (told_to_quit || i == EOF) {
1455 kill (ppid, SIGEMT);
1457 kill (ppid, SIGTERM);
1463 longjmp (sigenv, DONE);
1465 if (cp < &buffer[sizeof buffer - 2])
1472 if (buffer[0] == '?') {
1473 printf ("commands:\n");
1474 print_sw (ALL, sw, "");
1475 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1480 if (parse (buffer, cmdp) == NOTOK)
1483 switch (i = smatch (cmdp->args[0], sw)) {
1485 ambigsw (cmdp->args[0], sw);
1488 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1502 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1505 struct record rcs, *rc;
1511 switch (peer2rc (rc)) {
1513 pQRY (rc->rc_data, scansw);
1517 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1524 kill (ppid, SIGEMT);
1526 kill (ppid, SIGTERM);
1531 padios (NULL, "%s", rc->rc_data);
1534 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1541 parse (char *buffer, struct Cmd *cmdp)
1547 pp = cmdp->args[argp++] = cmdp->line;
1548 cmdp->redirect = NULL;
1549 cmdp->direction = STDIO;
1550 cmdp->stream = NULL;
1552 for (cp = buffer; (c = *cp); cp++) {
1558 fmt2peer (RC_EOF, "null command");
1562 while ((c = *cp++)) {
1569 cmdp->args[argp++] = pp;
1576 switch (c = *cp++) {
1578 padvise (NULL, "unmatched \"");
1583 if ((c = *cp++) == 0)
1594 if ((c = *cp++) == 0) {
1596 padvise (NULL, "the newline character can not be quoted");
1606 if (pp == cmdp->line) {
1607 padvise (NULL, "invalid null command");
1610 if (*cmdp->args[argp - 1] == 0)
1612 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1613 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1614 cmdp->direction = APPIO;
1617 cmdp->redirect = pp + 1;/* sigh */
1618 for (; (c = *cp); cp++)
1622 padvise (NULL, cmdp->direction != PIPIO
1623 ? "missing name for redirect"
1624 : "invalid null command");
1627 strcpy (cmdp->redirect, cp);
1628 if (cmdp->direction != PIPIO) {
1630 if (isspace (*cp)) {
1631 padvise (NULL, "bad name for redirect");
1634 if (expand (cmdp->redirect) == NOTOK)
1643 cmdp->args[argp] = NULL;
1650 expand (char *redirect)
1656 if (*redirect != '~')
1659 if ((cp = strchr(pp = redirect + 1, '/')))
1664 if ((pw = getpwnam (pp)))
1667 padvise (NULL, "unknown user: %s", pp);
1671 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1672 strcpy (redirect, path);
1678 init_io (struct Cmd *cmdp, int vio)
1685 result = initaux_io (cmdp);
1693 initaux_io (struct Cmd *cmdp)
1697 switch (cmdp->direction) {
1703 mode = cmdp->direction == CRTIO ? "write" : "append";
1704 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1705 padvise (cmdp->redirect, "unable to %s ", mode);
1706 cmdp->direction = STDIO;
1712 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1713 padvise (cmdp->redirect, "unable to pipe");
1714 cmdp->direction = STDIO;
1717 SIGNAL (SIGPIPE, pipeser);
1722 padios (NULL, "unknown redirection for command");
1726 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1727 padios ("standard output", "unable to dup2");
1735 fin_io (struct Cmd *cmdp, int vio)
1747 finaux_io (struct Cmd *cmdp)
1749 switch (cmdp->direction) {
1756 close (fileno (stdout));
1757 if (ferror (stdout))
1758 padvise (NULL, "problems writing %s", cmdp->redirect);
1759 fclose (cmdp->stream);
1764 close (fileno (stdout));
1765 pclose (cmdp->stream);
1766 SIGNAL (SIGPIPE, SIG_DFL);
1770 padios (NULL, "unknown redirection for command");
1773 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1774 padios ("standard output", "unable to dup2");
1777 cmdp->direction = STDIO;
1786 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1787 unset_selected (mp, msgnum);
1788 mp->lowsel = mp->hghsel = mp->numsel = 0;
1796 folder_free (mp); /* free folder/message structure */
1808 seq_setcur (struct msgs *mp, int msgnum)
1810 if (mp->curmsg == msgnum)
1813 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1814 free (Msgs[mp->curmsg].m_scanl);
1815 Msgs[mp->curmsg].m_scanl = NULL;
1817 if (Msgs[msgnum].m_scanl) {
1818 free (Msgs[msgnum].m_scanl);
1819 Msgs[msgnum].m_scanl = NULL;
1822 mp->curmsg = msgnum;
1830 #ifndef RELIABLE_SIGNALS
1831 SIGNAL (SIGINT, intrser);
1839 longjmp (sigenv, NOTOK);
1847 #ifndef RELIABLE_SIGNALS
1848 SIGNAL (SIGPIPE, pipeser);
1851 if (broken_pipe++ == 0)
1852 fprintf (stderr, "broken pipe\n");
1858 longjmp (sigenv, NOTOK);
1866 #ifndef RELIABLE_SIGNALS
1867 SIGNAL (SIGQUIT, quitser);
1875 longjmp (sigenv, NOTOK);
1883 longjmp (peerenv, DONE);
1892 struct record rcs, *rc;
1897 switch (peer2rc (rc)) {
1900 while (isspace (*bp))
1902 if (sscanf (bp, "%d", &vrsn) != 1) {
1904 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1907 if (vrsn != RC_VRSN) {
1908 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1912 while (*bp && !isspace (*bp))
1914 while (isspace (*bp))
1916 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1921 for (i = 1; i <= numwins; i++) {
1922 while (*bp && !isspace (*bp))
1924 while (isspace (*bp))
1926 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1929 rc2peer (RC_ACK, 0, NULL);
1933 padios (NULL, "%s", rc->rc_data);
1936 fmt2peer (RC_ERR, "pINI protocol screw-up");
1937 done (1); /* NOTREACHED */
1940 return 1; /* dead code to satisfy the compiler */
1945 pQRY (char *str, int scansw)
1947 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1950 rc2peer (RC_EOF, 0, NULL);
1959 static int lastlow = 0,
1964 oldhgh = mp->hghmsg;
1965 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1966 switch (winX (STATUS)) {
1971 printf ("new messages have arrived!");
1974 _exit (0); /* NOTREACHED */
1977 lastlow = lastcur = lasthgh = lastnum = 0;
1981 switch (winX (DISPLAY)) {
1986 scanrange (oldhgh + 1, mp->hghmsg);
1989 _exit (0); /* NOTREACHED */
1998 switch (winX (STATUS)) {
2003 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
2004 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
2005 readid (mp->lowmsg));
2008 _exit (0); /* NOTREACHED */
2015 if (mp->lowmsg != lastlow
2016 || mp->curmsg != lastcur
2017 || mp->hghmsg != lasthgh
2018 || mp->nummsg != lastnum)
2019 switch (winX (STATUS)) {
2027 _exit (0); /* NOTREACHED */
2030 lastlow = mp->lowmsg;
2031 lastcur = mp->curmsg;
2032 lasthgh = mp->hghmsg;
2033 lastnum = mp->nummsg;
2044 int i, j, k, msgnum, n;
2050 if (mp->nummsg == 0 && mp->nummsg != num)
2051 switch (winX (SCAN)) {
2059 _exit (0); /* NOTREACHED */
2068 j = (k = windows[SCAN]) / 2;
2069 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
2070 if (does_exist (mp, msgnum))
2074 k = i >= k ? 1 : k - i;
2081 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
2082 if (does_exist (mp, msgnum)) {
2089 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
2090 if (does_exist (mp, msgnum)) {
2094 if (n++ >= windows[SCAN])
2100 && does_exist (mp, lo)
2101 && does_exist (mp, hi)
2103 || (lo == mp->curmsg && lo == mp->lowmsg))
2105 || (hi == mp->curmsg && hi == mp->hghmsg))
2106 && hi - lo == j - i)
2109 if (mp->curmsg != cur || modified)
2110 switch (winN (NULLCMD, SCAN, 0)) {
2118 scanrange (lo = i, hi = j);
2129 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
2134 switch (winX (DISPLAY)) {
2139 printf ("commands:\n");
2140 print_sw (ALL, sw, "");
2141 printf ("type ``quit'' to leave %s\n", invo_name);
2144 _exit (0); /* NOTREACHED */
2147 rc2peer (RC_EOF, 0, NULL);
2151 if (parse (str, cmdp) == NOTOK)
2154 switch (i = smatch (cmdp->args[0], sw)) {
2156 switch (winX (DISPLAY)) {
2161 ambigsw (cmdp->args[0], sw);
2164 _exit (0); /* NOTREACHED */
2167 rc2peer (RC_EOF, 0, NULL);
2173 "say what: ``%s'' -- type ? (or help) for help",
2188 switch (setjmp (peerenv)) {
2190 SIGNAL (SIGALRM, alrmser);
2193 status = peerwait ();
2207 struct record rcs, *rc;
2212 switch (peer2rc (rc)) {
2215 rc2peer (RC_FIN, 0, NULL);
2219 advise (NULL, "%s", rc->rc_data);
2223 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
2230 ttyNaux (struct Cmd *cmdp, char *s)
2232 struct record rcs, *rc;
2237 if (cmdp && init_io (cmdp, vmh) == NOTOK)
2240 /* XXX: fseek() too tricky for our own good */
2242 fseek (fp, 0L, SEEK_SET);
2245 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
2247 vmhtty = OK; /* fall */
2252 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2255 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2256 done (1); /* NOTREACHED */
2260 SIGNAL (SIGTSTP, tstat);
2267 ttyR (struct Cmd *cmdp)
2269 struct record rcs, *rc;
2274 SIGNAL (SIGTSTP, SIG_IGN);
2286 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2288 rc2peer (RC_EOF, 0, NULL);
2292 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2295 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2296 done (1); /* NOTREACHED */
2299 return 1; /* dead code to satisfy compiler */
2304 winN (struct Cmd *cmdp, int n, int eof)
2307 char buffer[BUFSIZ];
2308 struct record rcs, *rc;
2311 if (vmhpid == NOTOK)
2316 /* XXX: fseek() too tricky for our own good */
2318 fseek (fp, 0L, SEEK_SET);
2322 snprintf (buffer, sizeof(buffer), "%d", n);
2323 switch (str2rc (RC_WIN, buffer, rc)) {
2331 padios (NULL, "%s", rc->rc_data);
2334 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2338 if (pipe (pd) == NOTOK) {
2339 err2peer (RC_ERR, "pipe", "unable to");
2343 switch (vmhpid = fork()) {
2345 err2peer (RC_ERR, "fork", "unable to");
2352 SIGNAL (SIGPIPE, SIG_IGN);
2353 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2354 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2362 advise (NULL, "%s", rc->rc_data);
2366 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2370 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2373 rc2peer (RC_EOF, 0, NULL);
2378 advise (NULL, "%s", rc->rc_data);
2383 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2388 err2peer (RC_ERR, "pipe", "error reading from");
2390 _exit (i != NOTOK ? i : 1);
2393 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2394 padios ("standard input", "unable to dup");
2395 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2396 padios ("standard output", "unable to dup");
2397 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2398 padios ("diagnostic output", "unable to dup");
2401 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2402 dup2 (i, fileno (stdin));
2407 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2408 padios ("standard output", "unable to dup2");
2412 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2413 padios ("diagnostic output", "unable to dup2");
2416 if (cmdp && init_io (cmdp, 0) == NOTOK)
2418 pstat = SIGNAL (SIGPIPE, pipeser);
2430 winR (struct Cmd *cmdp)
2440 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2441 padios ("standard input", "unable to dup2");
2446 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2447 padios ("standard output", "unable to dup2");
2452 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2453 padios ("diagnostic output", "unable to dup2");
2457 SIGNAL (SIGPIPE, pstat);
2459 if ((status = pidwait (vmhpid, OK)) == 2)
2463 return (status == 0 ? OK : NOTOK);
2471 char buffer[BUFSIZ];
2472 struct record rcs, *rc;
2477 /* XXX: fseek() too tricky for our own good */
2479 fseek (fp, 0L, SEEK_SET);
2481 snprintf (buffer, sizeof(buffer), "%d", n);
2482 switch (str2rc (RC_WIN, buffer, rc)) {
2490 padios (NULL, "%s", rc->rc_data);
2493 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2497 if (pipe (pd) == NOTOK) {
2498 err2peer (RC_ERR, "pipe", "unable to");
2502 switch (pid = fork ()) {
2504 err2peer (RC_ERR, "fork", "unable to");
2510 close (fileno (stdin));
2511 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2512 dup2 (i, fileno (stdin));
2515 dup2 (pd[1], fileno (stdout));
2516 dup2 (pd[1], fileno (stderr));
2524 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2525 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2535 padios (NULL, "%s", rc->rc_data);
2538 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2542 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2547 padios (NULL, "%s", rc->rc_data);
2550 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2554 err2peer (RC_ERR, "pipe", "error reading from");
2558 return (i != NOTOK ? pid : NOTOK);
2564 padios (char *what, char *fmt, ...)
2570 verr2peer (RC_FIN, what, fmt, ap);
2573 advertise (what, NULL, fmt, ap);
2582 padvise (char *what, char *fmt, ...)
2588 verr2peer (RC_ERR, what, fmt, ap);
2590 advertise (what, NULL, fmt, ap);